用烧瓶提供图像

Sha*_*n-x 7 html python flask

我有一个生成图像的程序.现在我想使用Flask让其他用户可以访问此图片,但我无法使用以下代码显示此图片:

#!/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')
Run Code Online (Sandbox Code Playgroud)

我的模板hello.html是:

<!doctype html>
<title>Hello from Flask</title>
<h1>Hello World!</h1>
<img src="./weather-plot.png">
Run Code Online (Sandbox Code Playgroud)

当我运行此程序并访问该页面时,我看到:

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 -
Run Code Online (Sandbox Code Playgroud)

在我的浏览器中,我看到标题,但不是图片.怎么了?

顺便说一下,有没有更好的方法来显示图片而没有其他任何东西?也许我不必使用模板?

Lud*_*udo 7

您确定图像确实位于该位置./,即项目的根目录中吗?在任何情况下,最好使用Flask的url_for()方法来确定URL(请参阅http://flask.pocoo.org/docs/api/#flask.url_for)这可以确保当您移动时,URL不会打破.