python - 将绘图发送到 flask 中的 html 时出错

标签 python html plot flask flask-wtforms

我正在使用 Flask 网络服务器并希望实现以下功能。

  • 将表单数据从 home.html 传递到 python func plot_perf_metric()。
  • 保存表单数据并调用 plot.html。
  • plot.html 调用 output_plot(),它根据保存的表单数据和从数据库 (mongodb) 访问的值生成绘图。
  • plot.html 打印此图。

我目前没有显示图像。

我只得到图像的断开链接。

有人可以帮助我吗?

主页.html

<!DOCTYPE html>
<html lang="en">
<body>
    <form action="./plot" method="POST">
        <input type="radio" name="perf_metric" value="disk"> Disk <br><br>
        <input type="radio" name="perf_metric" value="memory"> Memory <br><br>
        <input type="radio" name="perf_metric" value="cpu"> CPU <br><br>
        <input type="radio" name="perf_metric" value="egress"> Egress <br><br>
        <input type="radio" name="perf_metric" value="flit"> Flit <br><br>
        <input type="submit" name="submit_plot" value="Send">
    </form>
</body>
</html>

路线.py

from flask import Flask, render_template, send_file
from flask import request,make_response
from pymongo import MongoClient
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
import StringIO
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import random

app = Flask(__name__)

@app.route('/')
def home():
     return render_template('home.html')


def connect_mongo(text):
    client = MongoClient()
    db = client['mydb']
    collection = db['vload']
    b = list()
    a = list(collection.find())
    for i in range(len(a)):
        b.append(a[i][text])
        return b

@app.route('/output')
def output_plot():
    fig = Figure()
    axis = fig.add_subplot(1, 1, 1)
    b=connect_mongo(plot_perf_metric.text)
    fig = axis.plot(b)
    output = StringIO.StringIO()
    response = make_response(output.getvaluest())
    response.mimetype = 'image/png'
    fig.savefig(output)
    output.seek(0)
    return send_file(output, mimetype='image/png')

@app.route('/plot',methods=['POST'])
def plot_perf_metric():

if(request.form['submit_plot'] == "Send"):
    text = request.form['perf_metric']
    return render_template('plot.html')

if __name__ == '__main__':
    app.run(debug=True)

绘图.html

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Plot</title>
</head>
<body>
     <img src="{{ url_for('output_plot') }}" alt="Image Placeholder" height="100">
</body>
</html>

最佳答案

您需要为 output_plot 添加一条路线。

@app.route('/output')
def output_plot():
    fig = Figure()
    axis = fig.add_subplot(1, 1, 1)
    b=connect_mongo(plot_perf_metric.text)
    fig = axis.plot(b)
    output = StringIO.StringIO()
    response = make_response(output.getvaluest())
    response.mimetype = 'image/png'
    fig.savefig(output)
    output.seek(0)
    return send_file(output, mimetype='image/png')

关于python - 将绘图发送到 flask 中的 html 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28066684/

相关文章:

plot - GNUPLOT:从平滑累积中保存数据

python - 在Python数据框中划分几列,其中分子和分母列将根据选择列表而变化

python - 如何根据列的值和该列中的下一个值过滤数据帧的行

python - 有哪些最快的处理方法?

html - 调整窗口大小时将 SVG 元素固定到位

html - 在 Rails link_to 中添加 span 标签

python - Python 中的快速傅立叶图

r - 制作带有行和列标题的图矩阵

python - 如何使用 Pandas 对数据帧索引进行重新排序/排序?

html - Bootstrap 中的列在带有 float : none 的 Firefox 中没有响应