AWS Lambda、Python和Plotly:通过plotly.offline.plot绘图()似乎没有

2024-09-29 06:29:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用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)如果生成了SVG,它会被绘成某种奇怪的目录。在

问题:

1)如何控制plotly.offline.plot()写入SVG的目录?在

2)默认情况下,它试图在CentOS上写入到哪里?可能是由于Lambda权限的原因,它试图在那里写入,但失败了。在

3)在/tmp/之外的Lambda上是否有一个“downloads”文件夹可以检查?在

4)Lambda是否还有其他原因导致无法导出SVG?例如,无法打开HTML文件或无法加载嵌入式JS。在

5)在哪里可以进一步调试此问题?在

感谢你在这里的任何见解。在


Tags: lambdapysvgimageplothtmlchartplotly
1条回答
网友
1楼 · 发布于 2024-09-29 06:29:46

你试过plotly.io.write_image吗?或者类似fig.write_image("images/fig1.svg")

下面的文档描述静态图像导出: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')

相关问题 更多 >