Falsk micro framework
By default, Flask does not contain a database abstraction layer, form validation, or other things that existing libraries can handle. However, Flask adds these functions to the application by extending.
Flask will always provide a very simple and excellent glue layer, just like the python language. You can freely use SQLAlchemy to execute advanced schema, or use other database tools, introduce non relational data model, and even use non framework tools for Python Network Interface WSGI.
Templates and static files are stored in the subdirectory of the Python source tree of the application, with the names of templates and static respectively.
Werkzeug (WSGI) and Jinja (template) are two widely used tools, and Flask originated
It is used to show how to create your own framework based on these two tools.
The main code of flask is in the two libraries of Werkzeug and Jinja2. These two libraries play a major role. Flask just glues them together.
Flash installation
$ pip install Flask
Create files and directories independently
1. The webapp package directory stores the "ask" code. There are__ init__.py file
2. The templates directory stores template files
3.static directory, which stores static files such as js and css. Create a js directory under it and put the js files of jquery and echarts into it
4.app.py, the basic composition of the entry file
#It is recommended to write in the webapp package directory #Inheritance of flash from flask import Flask #Create application app = Flask(__name__) app = Flask('web')
Flask adds web page routing rules and uses flask inheritance and generation object (app) as decorator
Flask uses the Jinja2 template.
For the application app, its template is templates under the root directory, and a new index html
Support jianjia2 syntax in html
ABCD syntax you can view:
https://www.jianshu.com/p/83f5c3fd264c?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
#add_url_rule(rule, endpoint=None, view_func=None, provide_automatic_options=None, **options) from flask import render_template , Flask app = Flask('web') #Routing and view functions @app.route('/') def index(): userlist =list(range(100,110)) return render_template('index.html',userlist=userlist)
Application: an instance created to provide WEB services, which is also the entrance of wsgi
View function: execute internal code and output the contents of the response
Route: create the mapping relationship between path and view function through route decorator
View more class object API methods:
https://dormousehole.readthedocs.io/en/latest/api.html#flask.Flask
Start mode
# app.py from webapp import app if __name__ == '__main__': app.run('0.0.0.0',80,True)
Flash blueprint
A blueprint is a way to organize a set of related view functions and other code. Register them using blueprints. Then, when the blueprint is available in the factory function, the blueprint is registered with the application.
Flask has two blueprints, one for authentication and the other for blog posts. The code for each blueprint will be placed in a separate module.
Because blogs need to know about authentication, you will write authentication first.
In Flask, it is basically the mapping of route decorator and view function. If there are many functions, the code organization structure will be very chaotic.
Blueprint is the modular technology in Flask.
# webapp/books.py # Create blueprint from flask import Blueprint , jsonify # Built in dump #This is equivalent to an application #You can specify the template path separately. If it is a relative path, it means starting from the project root directory books = Blueprint('book',__name__,template_folder='ttt',url_prefix='/aaa') #The first parameter is the registered blueprint key ('book ') #Like application objects, the blueprint needs to know where it is defined, so it will__ name__ Passed as the second parameter. url_prefix will be added to all URLs associated with the blueprint. @books.route('/') def all(): books = [ (0,'pipe',90.0), (1,'shanhaiching',90.0), (2,'Mozi',90) ] return jsonify(books)
#app.register_blueprint(books) #All URLs of the default blueprint are linked to the site root path app.register_blueprint(books,url_prefix='/bbb')#url_prefix='/bbb'
For more methods of blueprints, search "Blueprint Objects" on the official website of flash
https://flask.palletsprojects.com/en/2.0.x/api/#blueprint-objects