Flask Image Service

I have a program that generates an image. Now I want to use Flask to make this snapshot available to other users, but I cannot display this image with the following code:

#!/usr/bin/python2 #coding: utf-8 from flask import * app = Flask(__name__) #app.run(host='0.0.0.0') @app.route('/') def index(): return render_template('hello.html') if __name__ == '__main__': app.run(debug=True,host='0.0.0.0') 

My hello.html template:

 <!doctype html> <title>Hello from Flask</title> <h1>Hello World!</h1> <img src="./weather-plot.png"> 

When I run this program and visit the page, I see the following:

 192.168.0.61 - - [10/Jul/2013 10:22:09] "GET / HTTP/1.1" 200 - 192.168.0.61 - - [10/Jul/2013 10:22:09] "GET /weather-plot.png HTTP/1.1" 200 - 

And in my browser I see the name, but not the image. What's wrong?

By the way, is there a better way to display a picture without any others? Maybe I do not need to use a template?

+7
python html flask
source share
1 answer

Are you sure that the image really is in ./ place, that is, at the root of your project? In any case, it is better to use the Flask url_for() method to determine the URLs (see http://flask.pocoo.org/docs/api/#flask.url_for ). This ensures that when you move things around, URLs are not interrupted.

+7
source share

All Articles