我有一个玩具REST应用程序,其结构如下:
- client/
- css/
- js/
- index.html
- server/
- app.py
... some other files which I use in app.py
app.py
是一个烧瓶restful端点,如下所示:
app = flask.Flask('some-name')
# a lot of API end points, which look in this way
@app.route('/api/something', methods=['GET'])
def func_1():
...
现在我想提供我的静态索引html。因此,在查看this,this和this之后,我认为我可以通过添加以下行来轻松地提供服务:
@app.route('/')
def home():
# but neither with this line
return app.send_static_file('../client/index.html')
# nor with this
return flask.render_template(flask.url_for('static', filename='../client/index.html'))
我看不到我的html文件(日志告诉我:127.0.0.1 - - [some day] "GET / HTTP/1.1" 404 -
)。我知道我可以在服务器文件夹中移动index.html,但有没有办法从现在的位置提供服务?
PS 当我添加app._static_folder = "../client/"
并将我的家庭功能更改为return app.send_static_file('index.html')
时,我终于开始接收我的html文件了,但是html里面的所有css / js文件以404返回。
答案 0 :(得分:8)
创建烧瓶应用时,将 static_folder 配置到客户端文件夹。
from flask import Flask
app = Flask(__name__, static_folder="client")
然后您可以访问网址http://localhost:5000/static/css/main.css
关于您的初步问题。你可以像这样用烧瓶提供静态html页面:
@app.route('/<path:path>')
def serve_page(path):
return send_from_directory('client', path)