Wiki source code of How to serve a webpage using python flask
Last modified by Leon Poon on 2021/03/23 13:17
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | First, [[set up a virtualenv>>doc:Computing Newbies.Python Newbies.How to Create a VirtualEnv.WebHome]]. | ||
| 2 | |||
| 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. | ||
| 4 | |||
| 5 | {{code language="python"}} | ||
| 6 | from flask import Flask | ||
| 7 | |||
| 8 | app = Flask(__name__) | ||
| 9 | |||
| 10 | @app.route('/', methods=['GET']) | ||
| 11 | def index(): | ||
| 12 | return 'Hi' | ||
| 13 | |||
| 14 | app.run() | ||
| 15 | {{/code}} | ||
| 16 | |||
| 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. | ||
| 18 | |||
| 19 | Now you should be able to access [[http:~~/~~/localhost:5000/>>url:http://localhost:5000/]] in your web browser. |