使用Flask将Biokit库生成的图像作为后期响应发送

时间:2017-10-30 20:03:38

标签: python matplotlib flask flask-restful

我目前正在实施Flask restful web服务。基本上,用户提交包含POST正文中的数据的JSON。我在POST正文中收到JSON有效负载,然后运行我的分析(在这种情况下,我正在执行关联)。我生成了一个相关矩阵。使用Biokit库,可以将相关矩阵转换为此链接中显示的图像:

http://thomas-cokelaer.info/blog/2014/10/corrplot-function-in-python/

我想将此图像作为回复返回给用户。我尝试过类似以下链接的代码:

https://gist.github.com/rduplain/1641344

https://gist.github.com/wilsaj/862153/119c6fc8ba2b0f3ffcd285a6852acb028660395b

我的代码示例如下:

#c.plot function generates the image that i want to send
#c.plot function comes from biokit library

fig = Figure(c.plot(colorbar=True,method='circle', shrink=.8, 
upper='circle'))

canvas = FigureCanvas(fig)

png_output = StringIO.StringIO()

canvas.print_png(png_output)

resp = make_response(png_output.getvalue())

resp.headers['Content-Type']='image/png'

resp.status_code = 200

return resp

当我运行我的烧瓶应用程序时,它显示某些内容正在作为回复发回,但是,我不确定它是什么。内容长度小于我的实际图像大小(以字节为单位)。

在两个链接中,他们使用ax = fig.subplot(111),并设置轴等,但是,我的情节不是xy的简单情节,而我不能只从图中获取x值和y值并将它们相互绘制。

非常感谢任何帮助

0 个答案:

没有答案