flask文档笔记

从最简单的Hello World开始:

1
2
3
4
5
6
7
8
9
10
from flask import Flask

app = Flask(__name__)

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

if __name__ == '__main__':
app.run()

1

1
from flask import Flask

Flask是个什么鬼?

1
class flask.Flask(import_name, static_path=None, static_url_path=None, static_folder='static', template_folder='templates', instance_path=None, instance_relative_config=False)

The flask object implements a WSGI application and acts as the central object.

2

1
app = Flask(__name__)

The idea of the first parameter is to give Flask an idea what belongs to your application.
If you are using a single module, __name__ is always the correct value. If you however are using a package, it’s usually recommended to hardcode the name of your package there.
For example if your application is defined in yourapplication/app.py you should create it with one of the two versions below:

1
2
app = Flask('yourapplication')
app = Flask(__name__.split('.')[0])

第二种看不懂?

split(…)
S.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.

__name__.split('.')[0]就是以.为分隔符把字符串’name‘变成为一个列表,然后取其第一个元素;这里的’name‘应该为yourapplication.app这样的。