flask(一)

setting

Create a project and a venv folder within

mkdir 01_project
cd 01_project
sudo apt-get install python3-venv
sudo apt-get install python3-wheel
python3 -m venv venv

Activate the environment

. venv/bin/activate

Install flask

pip install Flask

Living on the edge(latest flask)

pip install -U https://github.com/pallets/flask/archive/master.tar.gz

first program

Coding

# hello.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
	return 'hello flask'

Run

sudo apt-get install python3-flask

$ export FLASK_APP=hello.py
$ flask run
or python -m flask run
or flask run --host=0.0.0.0 (this can tells your system to listen on all public IPs)
 * Running on http://127.0.0.1:5000/

Debug mode

change you code and needn’t to restart

$ export FLASK_APP=***.py
$ export FLASK_ENV=development
$ flask run

Variable Rules

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'Index Page'

@app.route('/hello')
def hello_world():
    return 'Hello, world!'

@app.route('/user/<username>')
def show_user_profile(username):
    # show the user profile for that user
    return 'User %s' % username

@app.route('/post/<int:post_id>')
def show_post(post_id):
    # show the post with the given id , the id is a integer
    return 'Post %s' % post_id

@app.route('/path/<path:subpath>')
def show_subpath(subpath):
    # show the subpath after /path/
    return 'Subpath %s' % subpath

'''
string: (default) access any text with a slash 削减/斜线
int:    accept positive integers
float:  accepts positive floating point values
path:   like string but also accepts slashs
uuid:   accepts UUID strings
'''

Unique URLs/Redirection Behavior

The following two rules differ in their use of a trailing slash.

@app.route('/projects/')
def projects():
    return 'The project page'

@app.route('/about')
def about():
    return 'The about page'
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值