0

I have a Flask app with a form which takes a file name and a value, passes it to a python function which uses them for some processing and then stores an image file in the 'static folder'.

Upon clicking the 'Submit' button in the form, I want the image to be displayed in a new webpage. How do I go about doing 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) burndown_gen(file_path,sprint_num) #Does some processing and store a file in 'static' return render_template('burndown_form.html') 

burndown_form.html

<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> 

Route to display the chart

@app.route('/burndown/chart', methods=['GET','POST']) def output_chart(): return render_template('test.html') 

test.html

<body> <h1>BurnDown chart</h1> </body> <img src="../static/plot.png"> </body> 

Note: Have omitted parts of code for brevity

1
  • check your test.html body part first. May be it causes problem. Commented Feb 3, 2021 at 8:44

1 Answer 1

1

try this solution if your folder structure looks like this

app.py static ->plot.png ->test.png templates ->index.html ->test.html 
<body> <h1>BurnDown chart</h1> <img src="{{url_for('static', filename='plot.png')}}"> </body> 
Sign up to request clarification or add additional context in comments.

Comments