7

I have a program which generates an image. Now I want to use Flask to make this picture accessible to other users, but I can’t 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 template hello.html is:

<!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 this:

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 title, but not the picture. What’s wrong?

By the way, is there a better method to display a picture without anything else? Maybe I don’t have to use a template?

2
  • Uhm, I think the title tag is missplaced. It is a part of the header and not of the body section.
    – Reporter
    Commented Jul 10, 2013 at 8:29
  • @reporter: The tags for the head and body elements are optional, so there's nothing wrong with the HTML above. Commented Mar 30, 2016 at 3:13

1 Answer 1

7

Are you sure the image is indeed in the location ./, i.e. in the root of your project? In any case, it is better to use Flask's url_for() method to determine URLs (see http://flask.pocoo.org/docs/api/#flask.url_for) This makes sure that when you move things around, the URLs don't break.

1
  • Ok, thanks. I’ve used the url_for method and it has help me.
    – Shan-x
    Commented Jul 10, 2013 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.