These days to see the group of big guy basically all forced blind date, lovely small juanjuan also a day four times (simply), our programmers are also very handsome very beautiful, in fact, our hair is also quite a lot of ha ha ha (at least my side of the programmer quite a lot of hair, of course, MY Linux teacher hair a little little), Just checked stackOverflow and saw an interesting image saved hahaha, of course it’s just a funny image hahaha.
Blog.csdn.net/hanhanwangh… Treasure Girl welcomes your attention! Welcome to pay attention to wechat public number: Treasure girl’s growth diary let this lovely treasure girl walk with you on the road of hard work! If reproduced, please indicate the source (if not indicated, the thief will investigate)
Here’s the main point:
Test development – Build a simple Web service (Flask Framework foundation) project in action
flask
- Request and response
- Two, combined with the front end! [insert picture description here] (HTTP: / / https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d833b9784b634b7490aefb62c57f8db0~tplv-k3u1fbpfcp-zoom-1.im age)
-
- Return the HTML file
- CSS styles
- Three, the combination of back-end
- The Run method
-
- host
- port
- debug
- Flask configuration item config
- Six, routing,
-
- Introduce a decorator that goes inside @app.route()
Request and response
# coding:utf-8Flask request and response from flaskimport Flask, request, render_template
# 1.Initialize application app =Flask(__name__)
# 2.Add route view function @app.route("/")
def indexLittledata = request.args name = littledata.get('username')
print(name)
return "Hello,thi s is index!"The response is in HTML format#return "<p style ='color:green'>Hello,thi s is index!</p>"# if return to load a file#return render_template('login.html')
if __name__ == "__main__":
# 2.Run the server, default port if no port is specified5000
app.run(port=8003)
Copy the code
Running results:
Pay attention toIf we go back to loading an HTML file, create a templates folder directly outside the file, because I can click on Flask to see how it’s used.
Two, combined with the front end
Return the HTML file
Write the Html file you want to jump to, such as file directory
Source: login. HTML
<! DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8"> <title> You are really great! </title> </head> <body> <a href="https://blog.csdn.net/hanhanwanghaha"</a> </body> </ HTML >Copy the code
flask_one.py
# coding:utf-8Flask request and response from flaskimport Flask, request, render_template
# 1.Initialize application app =Flask(__name__)
# 2.Add route view function @app.route("/")
def indexLittledata = request.args name = littledata.get('username')
print(name)
#return "Hello,thi s is index!"The response is in HTML format#return "<p style ='color:green'>Hello,thi s is index!</p>"# if return to load a filereturn render_template('login.html')
if __name__ == "__main__":
# 3.Run the server, default port if no port is specified5000
app.run(port=8003)
Copy the code
The results
CSS styles
Click on the Flask function and there is a definition like this:
So I’ll create a new folder under my project called static and write a style
I’m going to introduce CSS styles into my Html
Finally, let’s run it
Note:In fact, the name of this file can be changed, such as
Blog.csdn.net/hanhanwangh… My official source address, welcome to search and pay attention to! Article thief garbage person is also, the thief will investigate!
Three, the combination of back-end
The static_folder is different from the static_url_path, which tells the program where to locate my static files on the hard drive
eg:
# coding:utf-8Flask request and response from flaskimport Flask, request, render_template
# 1.Initialize application app =Flask(__name__,
template_folder="bbb",
static_url_path="/ooo",
static_folder="ooo"
)
# 2.Add route view function @app.route("/")
def indexLittledata = request.args name = littledata.get('username')
print(name)
#return "Hello,thi s is index!"The response is in HTML format#return "<p style ='color:green'>Hello,thi s is index!</p>"# if return to load a filereturn render_template('login.html')
if __name__ == "__main__":
# 3.Run the server, default port if no port is specified5000
app.run(port=8003)
Copy the code
Running results:
The Run method
Click Run to see host Host Debug
host
port
debug
- Set debug=True to restart the service for every change
app.run(debug=True)
Copy the code
- Set debug=True to show specific errors to the front end (of course, it is best not to use this, because you have told others about your vulnerability, others will analyze your vulnerability, so it is easy to attack!)
Flask configuration item config
We can obtain our configured options through config
# coding:utf-8Flask request and response from flaskimport Flask, request, render_template
# 1.Initialize application app =Flask(__name__,
template_folder="bbb",
static_url_path="/ooo",
static_folder="ooo"
)
app.config["debug"]=True
app.config["port"] =8003
# 2.Add route view function @app.route("/")
def indexLittledata = request.args name = littledata.get('username')
print(name)
#return "Hello,thi s is index!"The response is in HTML format#return "<p style ='color:green'>Hello,thi s is index!</p>"# if return to load a filereturn render_template('login.html')
# 3.Run the server, default port if no port is specified5000
if __name__ == "__main__":
app.run(port=app.config["port"],debug=app.config["port"])
Copy the code
Six, routing,
What if we wanted to display the login.html page using multiple paths? For example,
# coding:utf-8Flask request and response from flaskimport Flask, request, render_template
# 1.Initialize application app =Flask(__name__,
template_folder="bbb",
static_url_path="/ooo",
static_folder="ooo"
)
# 2.Add route view function @app.route("/")
def indexLittledata = request.args name = littledata.get('username')
print(name)
#return "Hello,thi s is index!"The response is in HTML format#return "<p style ='color:green'>Hello,thi s is index!</p>"# if return to load a filereturn render_template('login.html')
@app.route("/login")
def loginLittledata = request.args name = littledata.get('username')
print(name)
#return "Hello,thi s is index!"The response is in HTML format#return "<p style ='color:green'>Hello,thi s is index!</p>"# if return to load a filereturn render_template('login.html')
@app.route("/cute")
def cuteLittledata = request.args name = littledata.get('username')
print(name)
#return "Hello,thi s is index!"The response is in HTML format#return "<p style ='color:green'>Hello,thi s is index!</p>"# if return to load a filereturn render_template('login.html')
# 3.Run the server, default port if no port is specified5000
app.run(port=8003)
Copy the code
But it can be simplified as:
# coding:utf-8Flask request and response from flaskimport Flask, request, render_template
# 1.Initialize application app =Flask(__name__,
template_folder="bbb",
static_url_path="/ooo",
static_folder="ooo"
)
# 2.Add route view function @app.route("/login")
@app.route("/cute")
@app.route("/")
def indexLittledata = request.args name = littledata.get('username')
print(name)
# return "Hello,thi s is index!"The response is in HTML format# return "<p style ='color:green'>Hello,thi s is index!</p>"# if return to load a filereturn render_template('login.html')
# 3.Run the server, default port if no port is specified5000
app.run(port=8003)
Copy the code
Introduce a decorator that goes inside @app.route()
# coding:utf-8
importFlask time # Request and response from flaskimport Flask, request, render_template
# 1.Initialize application app =Flask(__name__,
template_folder="bbb",
static_url_path="/ooo",
static_folder="ooo") # decorator print time deflog_time(f):
def decorator(*args, **kw):
print(f'{time.time()}') return f(*args, **kw) return decorator() # 2. @app.route("/cute") @app.route("/cute") @app.route("/cute") @log_time def index(): Littledata = request.args name = littledata.get('username')
print(name)
# return "Hello,thi s is index!"The response is in HTML format# return "<p style ='color:green'>Hello,thi s is index!</p>"# if return to load a filereturn render_template('login.html')
# 3.Run the server, default port if no port is specified5000
app.run(port=8003)
Copy the code
Flask routing will be documented in detail, and more later!
Blog.csdn.net/hanhanwangh… Treasure Girl welcomes your attention! Welcome to wechat public account: Treasure girl’s growing diary if reproduced, please indicate the source (if not indicated, the thief will investigate)
Finally, wish everyone a happy New Year, in the New Year to be a better programmer, with higher salary programmer!