flask 学习一

from flask import Flask	# imorted the Flask class. An instance of this class will be our WSGI application.
app = Flask(__name__)  # we create an instance of this class. The first argument is the name of the application's module or package

@app.route('/')		#网页要访问入口,/指的是网页根目录你也可以设置成/index
def hello_world():  #根目录的显示内容
    return 'Hello, World!'  #打印到网页到内容
__init__.py 和 __main__.py
__init__.py #程序实例化入,导入一个项目,实例化到时候运行__init__.py
__main__ #程序到入口,想运行一个python文件夹,首先运行的是__main__.py文件
$ export FLASK_APP=hello.py		#默认是自动,运行app.py
$ flask run
 * Running on http://127.0.0.1:5000/

from flask import Flask

app = Flask(__name__)

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

@app.route('/user/<username>')		#增加,<converter:variable_name> <int: post_id> <path:subpath>
def show(username):
 	return 'User %s' % username
 	
if __name__ == '__main__':
    app.run(127.0.0.1, 8000, debug=True)

>>>python3 name.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
	return '<h1>Hello World!</h1>'
	if __name__ == '__main__':
		app.run(127.0.0.1, 5000, debug=True)

猜你喜欢

转载自blog.csdn.net/weixin_42262889/article/details/89589572
今日推荐