table of Contents
flask is lightweight python web framework, a few simple commands you can create a Web application.
flask early experience
1, the installation Flask
G:\Py\web>pip install flask
2. Create a "Hello, World" Flask application
We should establish a Zblog directory, and then build the app directory. Related python code placed inside. Used to create a simple flask web application. Directory structure is as follows:
Zblog/
app/
__init__.py
routes.py
Zblog.py
Here a total of three Python source code files.
__init__.py
The script is only imported from the flask in class Flask
, and to create such an application object. as follows:
from flask import Flask
app = Flask(__name__)
from app import routes
routes.py
Routing settings pages of the script on behalf of, the route used to achieve different url as follows: complete index, returns a "hello world" statement.
from app import app
@app.route('/')
@app.route('/index')
def index():
return "Hello, World!"
Zblog.py
As the outer layer of the script, used to complete the application.
from app import app
3, the results
By setting FLASK_APP
environment variables tell Flask how to import it:
G:\Py\Zblog>set FLASK_APP=Zblog.py
linux environment with export
command.
Finally, first run a web application.
G:\Py\Zblog>flask run
* Serving Flask app "Zblog.py"
* Environment: production
WARNING: This is a development server. Do not use it in a production deployme
nt.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
By visiting http://127.0.0.1:5000/
this address. You look at the first application of it.