Wiki source code of How to serve a HTML file using python flask
Last modified by Leon Poon on 2021/03/23 13:17
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
3.1 | 1 | First, make sure that your flask app is working like inĀ [[How to serve a webpage using python flask>>doc:Computing Newbies.Python Newbies.How to serve a webpage using python flask.WebHome]]. |
![]() |
1.1 | 2 | |
3 | Then, create your HTML file. Your HTML file must be in the same directory as your flask app python file. | ||
4 | |||
5 | Change your flask app python file to do send_from_directory instead of just saying hi. Use the name of the HTML file if it is not test.html: | ||
6 | |||
![]() |
2.1 | 7 | {{code language="python"}} |
8 | from flask import Flask, send_from_directory | ||
![]() |
1.1 | 9 | |
10 | app = Flask(__name__) | ||
11 | |||
12 | @app.route('/', methods=['GET']) | ||
13 | def m(): | ||
14 | return send_from_directory('', 'test.html') | ||
15 | |||
16 | app.run() | ||
![]() |
2.1 | 17 | {{/code}} |