从flask,如何将matplotlib图图像发送到JSON对象? [英] From flask, how to send the matplotlib plot image into JSON object?

查看:312
本文介绍了从flask,如何将matplotlib图图像发送到JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试创建一个与前端的React和后端的Flask一起使用的API.作为此过程的一部分,需要显示的输出包括四个实体:

We are trying to create an API that works with React on the frontend and Flask on the backend. As a part of this, the output that needs to be displayed includes four entities:

2张图像(使用matplotlib生成) 2弦. 我已经将两个字符串都嵌入到JSON对象中,但是事实证明图像很困难.

2 images (generated using matplotlib) 2 strings. I have embedded both strings into a JSON object but the images are proving to be difficult.

有没有一种方法不存储这些图像,而直接将生成的matplotlib图像嵌入到JSON对象中?大多数方法都要求我们指定图像文件名,但是是否可以跳过保存文件的方法? sendfile()无效,因为我们有多个输出,而我们的最终目标是JSON响应.

Is there a way to not store these images and directly embed the generated matplotlib images into the JSON object? Most methods require us to specify the image file name but is there a way to skip saving the files? The sendfile() wouldn't work because we have multiple outputs and our final objective is the JSON response.

试图创建一个临时文件并读取它,这会生成一个缓冲流吗?我认为Base64不适用于此吗?

Tried creating a temporary file and reading it and this is generating a Buffered Streams ? Base64 would not apply on this I think?

预先感谢您的帮助.

推荐答案

https://github.com/mpld3 /mpld3 mpld3提供了一个基于D3的自定义独立javascript库,该库可解析绘图的JSON表示形式. mpld3 python模块提供了一组例程(使用mplexporter框架)解析matplotlib图并输出mpld3.js可读的JSON描述.

https://github.com/mpld3/mpld3 mpld3 provides a custom stand-alone javascript library built on D3, which parses JSON representations of plots. The mpld3 python module provides a set of routines which parses matplotlib plots (using the mplexporter framework) and outputs the JSON description readable by mpld3.js.

这篇关于从flask,如何将matplotlib图图像发送到JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆