Python3, the Flask project for website building, will take you to start the web service! !

Followed by the previous article "Python3, database table design and data storage for website construction!
This article will continue to build our website.
What I will share today is to build the Flask project.

1. Initialize the app

Because you want to build the flask project,
the first step is to install flask~

pip install flask

After the installation is complete, we follow the old rules and upload the code:

Servers folder under the init .py:

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-9-02
"""

'''
初始化app,
主要放置前端内容,例如:js,css等

'''
from flask import Flask

#初始化app实例对象
app  = Flask(__name__,
			#指定前端文件夹templates
			template_folder = '../Services/templates',
			#前端css,js等代码放置在static文件夹下
			static_folder='../Services/templates/static'
			)


Take a look at the directory structure

Insert picture description here
Here is also marked out for everyone:

Templates : front-end code is placed
the main.py : Start app
the init .py : initialization app

2. Start the app

1. Write the code of the main.py file

main.py

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-9-02
"""
from Servers import app

#启动app
if  __name__ == "__mian__":
	'''
	设置host,
	设置 port
	开启debug模式,如果报错,在前端直接显示
	'''
	app.run(host = 0.0.0.0,
			port = 8889,
			debug = True)

2. Run the code of the main.py file

If the screenshot message appears, it means no problem.
Insert picture description here
3. Click on the URL http://0.0.0.0:8889

There are screenshots, indicating that there is no problem:
Insert picture description here
At this time,
Xiao Diaosi asked in confusion : Uncle Yu, it has appeared that this page cannot be accessed. Do you still say there is no problem? ?
Xiaoyu : Don't panic, don't panic, this phenomenon is correct, because we didn't write an api interface to get data!
Xiao Diaosi : Then, how do you write the api interface for data acquisition? After writing the interface, it can be displayed normally?
**Xiaoyu:** You wrote the get api interface. If the database has no data, the data cannot be displayed.
Only the database has data and the api interface is written, and there is no problem, in order to show the desired effect~ ~ ~
Xiao Diaosi : Then hurry up, I want to see the effect...
Xiaoyu : Don't worry, see you in the next article "Python3, Website Building API Interface".

3. References

If you don’t know much about Flask, you can refer to this one:
>> "Flask Tutorial" provided by w3cschool

Guess you like

Origin blog.csdn.net/wuyoudeyuer/article/details/108363207