簡體   English   中英

AWS Lambda, Python, and Plotly: Generating SVG via plotly.offline.plot() doesn't seem to work

[英]AWS Lambda, Python, and Plotly: Generating SVG via plotly.offline.plot() doesn't seem to work

我正在使用 plotly 的plot()方法從 AWS Lambda 嘗試 output 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!"

My understanding of the above code, which I did not write, is that plotly generates SVG by first creating an html with executable JS, which then downloads the html as SVG. 我們嘗試打開 html 文件下載 SVG,但必須等待並重試幾次,以防需要時間。 (如果你知道更好的方法,請告訴我。)

print(os.listdir)行上,您會注意到 html 存在,但要么 A) 沒有生成 SVG,要么 B) 如果生成了,它是由 Z26C592956DC26CA2AEF5DBACC204A 寫入的一些奇怪的目錄。

問題:

1)我如何控制目錄plotly.offline.plot()寫入 SVG 到?

2) 默認情況下,它試圖在 CentOS 上寫入哪里? 由於 Lambda 權限,它可能正在嘗試並未能寫入那里。

3) 在/tmp/之外的 Lambda 上是否有我可以檢查的“下載”文件夾?

4) 是否有其他原因 Lambda 可能無法導出 SVG - 例如,無法打開 HTML 文件或無法加載嵌入式 JS。

5) 我在哪里可以進一步調試這個問題?

感謝您在這里可能擁有的任何見解。

你試過plotly.io.write_image嗎? 或類似fig.write_image("images/fig1.svg")

Plotly 文檔在此處描述 Static 圖像導出: 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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM