我正在尝试使用plotly的plot()
方法从AWS Lambda输出SVG。这在本地环境中有效,但在Lambda中似乎无法使用。
代码块:
downloadRootFolder = "/tmp/" # Lambda's write directory is /tmp. Other directories will deny access
aFilename = "py_chart_" + uuid.uuid1().hex
plotly.offline.plot(chart,
output_type = "file",
filename = downloadRootFolder + aFilename + ".html", # /tmp/py_chart_UUID.html
image_filename = downloadRootFolder + aFilename, # /tmp/py_chart_UUID
image = "svg", image_width = w, # defined earlier
image_height = h)
myFilename = aFilename + ".svg"
svgText = "<svg>could not find the svg file!</svg>"
maxAttempts = 20
millisecsToWait = 250
print(os.listdir("/tmp/")) # output: ['py_chart_380ac7b4fcf411e9b6c0d273169078fd.html'] (no .svg file)
for c in range(0,maxAttempts):
if os.path.exists(downloadRootFolder + myFilename):
f = open(downloadRootFolder + myFilename,"r")
svgText = f.read()
f.close()
else:
time.sleep(millisecsToWait/1000)
print("look for saved svg file attempt # " + str(c))
return(svgText) # returns "could not find the svg file!"
我对上面的代码(不是我写的)的理解是,首先通过使用可执行JS创建一个html,然后将该html下载为SVG,从而密谋生成SVG。我们尝试打开html文件以下载SVG,但必须等待几次,然后重试,以免花费时间。 (如果您知道更好的方法,请告诉我。)
在print(os.listdir)
行上,您会注意到html存在,但要么A)SVG未生成,要么B)如果生成,则将它以密谋方式写入一些奇怪的目录。 / p>
问题:
1)如何控制目录plotly.offline.plot()
将SVG写入其中?
2)默认情况下,它在哪里尝试在CentOS上写入?可能由于Lambda权限而无法在此写入。
3)我可以检查的/tmp/
之外的Lambda上是否有一个“下载”文件夹?
4)Lambda是否有其他原因可能无法导出SVG,例如,无法打开HTML文件或无法加载嵌入式JS。
5)我应该在哪里寻找进一步调试该问题的地方?
感谢您在这里可能有的见识。
答案 0 :(得分:1)
您尝试过plotly.io.write_image
吗?或类似fig.write_image("images/fig1.svg")
Plotly文档在此处描述了静态图像导出:https://plot.ly/python/v3/offline/
基本上,将图形转换为图像字节,然后写入文件。
import plotly.io as pio
static_image_bytes = pio.to_image(fig, format='png')
pio.write_image(fig, file='plotly_static_image.png', format='png')