python - Matplotlib 图像显示在 Heroku 中托管的 Flask 应用程序上
问题描述
我正在尝试制作一个 Flask 应用程序并将其托管在 Heroku 上。
该应用程序获取一些文本和数字数据,执行计算并在单击“生成”按钮时显示图表。它将 Matplotlib 生成的图存储在名为“/static”的文件夹中。现在,在 Heroku 上,不允许使用名为“/static”的文件夹名称,并且不起作用。但是在阅读了几个地方之后,我尝试使用'/tmp',但这也不起作用。
下图是我得到的。
这是处理文件生成的代码:
if not os.path.isdir('tmp'):
os.mkdir('tmp')
else:
# Remove old plot files
for filename in glob.glob(os.path.join('tmp', '*.png')):
os.remove(filename)
# Use time since Jan 1, 1970 in filename in order make
# a unique filename that the browser has not chached
plotfile = os.path.join('tmp', 'matgenimage'+'.png')
plt.savefig(plotfile)
return plotfile
完整的 Flask 应用程序的代码在这里:
解决方案
完毕!
这个视频 -> https://www.youtube.com/watch?v=vgSFm3wM6g4 和这个存储库 -> https://github.com/marquitobb/flask_graficas 让我很清楚。需要使用 BytesIO 和 base64 在同一请求中传递图像。
推荐阅读
- c# - 正则表达式模式无数次,除了最后一个不同
- ios - SpriteKit 绘制渐变矩形
- python - 如何保存 tensorflow.compat.v1.placeholder 对象?
- python - 如何将kafka消息中的键序列化为字符串
- node.js - gulp build 引发此错误 - [tslint] 错误:找不到模块'@microsoft/rush-stack-compiler-3.2'
- postgresql - PostgreSQL - 如何匹配列具有逗号分隔值的表中的值
- python - 如果表单在python中不包含标签(。)如何显示错误页面
- python - 更改 GeoJson 中点的图标
- javascript - 更改爬虫的元描述长度限制
- python-3.x - 我正在尝试使用 python urllib 和美丽的汤从网站获取表数据,但它返回脚本