preface
The crawled data cannot intuitively know what data is obtained, and what method can be used to analyze and visualize the data?
Tip: the following is the main content of this article. The following cases can be used for reference
1, What is Flask?
Flask is a lightweight Web application framework written in Python. Its WSGI toolbox uses Werkzeug, and the template engine uses Jinja2. Flask uses BSD authorization.
Flask is also called "microframework" because it uses a simple core and uses extension to add other functions. Flash has no default database and form verification tools.
Flash itself is equivalent to a kernel, which mainly realizes the functions of routing distribution and template rendering. It is integrated from Werkzeug and Jinja2 module packages respectively, which are also the core of flash framework.
Although the core is simplified, flash provides a very good extension mechanism. All kinds of requirements in development basically have corresponding official / third-party extensions that can be realized, and even it is very simple to realize them by yourself.
-Common expansion packs
Flask Sqlalchemy: ORM operation database;
Flash restful: a tool for developing REST API;
Flash migrate: manage the migration database;
Flash caching: caching;
Flask WTF: form;
Flash mail: email;
Flask login: authentication user status;
Flask openid: authentication;
Flask admin: a framework of simple and extensible management interface
Flask bable: provide internationalization and localization support and translation;
Flash bootstrap: integrated front-end Twitter Bootstrap framework;
Flash moment: localization date and time;
-Basic mode
The basic mode of Flask is to assign a view function to a URL in the program. Whenever the user accesses the URL, the system will execute the view function assigned to the URL, obtain the return value of the function and display it on the browser. Its working process is shown in the figure below:
The basic point of IT operation and maintenance is security, stability and efficiency. The purpose of operation and maintenance automation is to improve operation and maintenance efficiency. The fast development of flash [9] just meets the efficiency requirements of operation and maintenance. In the process of iterative development of the project, the operation and maintenance functions and extensions that need to be realized will gradually increase. In view of this feature, IT is necessary to use the easily extensible flash framework.
2, Basic use of flash
1. Import and storage
The code is as follows:
from flask import Flask, render_template
2. Route analysis
The routing path cannot be repeated. Users can access specific functions through a unique path
The default code of a new project is as follows:
@app.route('/') def hello_world(): return 'hello!'
Pass the routing path to @ app Route() method, return returns the content displayed on the route web page
Flash default http://127.0.0.1:5000/ , where the page whose access route is' / 'displays hello on the web page
result
Modify Debug mode
Open the Debug mode. After modifying the code, it can be displayed on the web page without recompiling
Obtain the user's parameters through the access path
# Gets the string parameter of the user @app.route('/user/<name>') def welcome(name): return "Hello,{}".format(name)
result
# Get the integer parameters of the user. In addition, there is the float type @app.route('/user/<int:id>') def welcome2(id): return "Hello,{}Member No".format(int(id))
result
Returns the rendered web page file to the user
Write a simple test index html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Flask_test</title> </head> <body> hello,world </body> </html>
Access index.com via route html
@app.route("/") def index2(): return render_template("index.html")
result
Pass parameters to page
@app.route("/") def index2(): time = datetime.date.today() # Common variable name = ["Zhang San", "Lao Wang", "Li Si"] # List type task = {"task": "cleaning", "time": "3 hour"} # Dictionary type return render_template("index.html", var=time, list=name, task=task)
Pass the obtained data into index HTML
index.html content:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Flask_test</title> </head> <body> Today is{{ var }},Welcome<br/> On duty today are:<br/> {% for data in list %} <!--Control structure enclosed by braces and percent sign, and if--> <li>{{ data }}</li> {% endfor %} task:<br/> <!--Learn how to print a form on a page and how to iterate--> <table border="1"> {% for key,value in task.items() %} <tr> <td>{{ key }}</td> <td>{{ value }}</td> </tr> {% endfor %} </table> </body> </html>
The loop needs to be terminated with {% endfor%}. It is judged that {% endif%} needs to be terminated
result
form
# Form submission @app.route('/test/register') def register(): return render_template("test/register.html") # To accept the route submitted by the form, you need to specify methods as post @app.route('/result', methods=['POST', 'GET']) def result(): if request.method == 'POST': result = request.form return render_template("test/result.html", result=result)
register.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Flask_test</title> </head> <body> <form action="{{ url_for('result') }}" method="post"> <p>full name:<input type="text" name = "full name"></p> <p>Age:<input type="text" name = "Age"></p> <p>Gender:<input type="text" name = "Gender"></p> <p>Address:<input type="text" name = "address"></p> <p><input type="submit" value="Submit"></p> </form> </body> </html>
result.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Flask_test</title> </head> <body> <table border="1"> {% for key,value in result.items() %} <tr> <th>{{ key }}</th> <td>{{ value }}</td> </tr> {% endfor %} </table> </body> </html>
result
Enter various parameters and click Submit
summary
The above is what I want to talk about today. This article only briefly introduces the simple use of flash.