Jane says the flask first experience Python

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__.pyThe 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.pyRouting 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.pyAs the outer layer of the script, used to complete the application.

from app import app

3, the results

By setting FLASK_APPenvironment variables tell Flask how to import it:

G:\Py\Zblog>set FLASK_APP=Zblog.py

linux environment with exportcommand.

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.

Guess you like

Origin www.cnblogs.com/zhangshengdong/p/12498002.html