Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

python - Return webpage having a plot in Flask

I have this route in my flask app which takes a file name and sprint number from the user through a form (burndown_form.html). It calls a function "burndown_gen" using these params which returns a python data frame. I need to display a plot based on this dataframe on a web page after the user clicks the submit button on "burndown_form.html". How do I do this?

@app.route('/burndown', methods=['GET','POST'])
def output_burndown_chart():
    if request.method == "POST":
        sprint_num = request.form['sprint_number']
        file_name = request.form['file_name']
        dir_name = "./files"
        file_path = os.path.join(dir_name, file_name)
        df_b = burndown_gen(file_path,sprint_num)
        df_b.plot.line(x='Date',y='Story Points Left',figsize=(10,5))
    return render_template('burndown_form.html')

burndown_form.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Sprint Velocity</h1>
<form action="{{ url_for('output_burndown_chart') }}" method="post">
    <label for="file_name">File Name:</label><br>
    <input type="text" name="file_name"><br>
    <label for="sprint_number">Sprint Number:</label><br>
    <input type="text" name="sprint_number"><br>
    <input type="submit">
</form>
</body>
</body>
</html>
question from:https://stackoverflow.com/questions/65915427/return-webpage-having-a-plot-in-flask

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One way to do it would be to store the plot as a figure in the static folder of your Flask app folder structure and then display it on using an image reference in your html template. Something like this:

fig = df_b.plot.line(x='Date',y='Story Points Left',figsize=(10,5)).get_figure()
fig.savefig('static/plot.png')

However, this limits your flexibility to customize your chart as you need to convert it into an image file. Therefore, the better way to do it would be to use something like chart.js in your html template by passing the data frame.

You'd need to convert your data frame to something that's JSON serializable, since python data frames can't be serializable into JSON format. Since you have two columns, you could split them into two lists like this.

date_list = df_b['Date'].tolist() 
sp_list = df_b['SPs_Left'].tolist()

And then pass it like this.

return render_template('output_burndown.html', date_list=date_list, sp_list=sp_list)

You'd then need to convert your lists to JSON using the tojson command and use those as the data for your graph.

<canvas id="myChart"></canvas>

        <script type="text/javascript">
            var ctx = document.getElementById('myChart').getContext('2d');
            var chart = new Chart(ctx, {
                type: 'line',
                data: {
                    labels: {{ date_list|tojson }},
                    datasets: [{
                        label: "burndown",
                        data: {{ sp_list|tojson }},
                        borderColor: "#3e95cd",
                    }]
                },
                options: {}
            });
        </script>

More on chart.js can be found here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...