Wiki source code of How to serve a webpage using python flask
Last modified by Leon Poon on 2021/03/23 13:17
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
| |
4.1 | 1 | First, [[set up a virtualenv>>doc:Computing Newbies.Python Newbies.How to Create a VirtualEnv.WebHome]]. |
| |
1.1 | 2 | |
| |
3.1 | 3 | [[In the virtualenv, install flask>>doc:Computing Newbies.Python Newbies.How to add libraries to a virtualenv.WebHome]], then try out this .py file. |
| |
1.1 | 4 | |
| |
2.1 | 5 | {{code language="python"}} |
| 6 | from flask import Flask | ||
| |
1.1 | 7 | |
| 8 | app = Flask(__name__) | ||
| 9 | |||
| 10 | @app.route('/', methods=['GET']) | ||
| 11 | def index(): | ||
| 12 | return 'Hi' | ||
| 13 | |||
| 14 | app.run() | ||
| |
2.1 | 15 | {{/code}} |
| |
1.1 | 16 | |
| |
5.1 | 17 | Run this script [[in the virtualenv>>doc:Computing Newbies.Python Newbies.How to Run a Python Script in a VirtualEnv.WebHome]]. It will not exit. |
| |
1.1 | 18 | |
| 19 | Now you should be able to access [[http:~~/~~/localhost:5000/>>url:http://localhost:5000/]] in your web browser. |