Using GPT to quickly build Python websites saves time and effort


Preparation

Python is a very popular system development language. Its three most famous frameworks: flask, tonado, and django are widely used.

Here's how to use GPT to quickly build a website with a flask framework.

First understand the basic knowledge of the website

First of all, the basic content required by the general flask website includes templates, static files, and applications.

  • Template: It is the page style of the web page, that is, the html page.
  • Static files: js script files, pictures, style files, etc.
  • Application: It is a python language script.

1. Preparation

1.1 Create a directory structure as shown in the figure below, create:

  • static folder, store static files;
  • The templates folder stores html page files;
  • The web folder stores python script files.
    Flask directory structure

1.2 Prepare the template

You can go to Baidu to search for website building templates, or you can write your own. Of course, you can also let GPT write for you.

Put the html template in a text file and save it with the suffix changed to .html.

1.3 Prepare static files

Static files such as required pictures, jquery.js files, css style files, bootstrap files, etc. The use of these files requires knowledge of the corresponding knowledge. Of course, if none of these files are available, it will not affect you to make a web page that can be opened.

Store them in their own folders.

1.4 Create a python script

The file of the flask framework creates a py file in the web directory, and the app.py file can also use other names.

2. Install the Python environment

(The content here has low relevance to this article, 1000 words are ignored)

3. Build the Flask framework

3.1 Build the basic framework of Flask

Ask GPT: Write a flask creation script
insert image description here
Copy the script in the answer to app.py and save it.

# -*- utf-8 -*- 
from flask import Flask

app=Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'
    
if __name_ == '_main__':
    app.run()

In this way, the basic framework of flask has been created.

It can be executed in PythonIDE. When you open the page with the address, you can see that the page outputs "hello world!"
Flask Hello world execution result

3.2 Using template files

Ask GPT: set template folder

Special note: Because GPT has been consulted before to build the flask framework, GPT has already remembered the scope of knowledge you want to ask him. So, just ask directly and simply.

Compared with the previous script, we can see one more line of script:

app.template_folder = 'templates'

added to the corresponding page. This way we can call the template file. Then what to do? Of course, continue to ask GPT.

Ask GPT: Backend, how to write render_template to render a page under the templates folderinsert image description here

In a moment I got the answer:

# -*- utf-8 -*-
from flask import Flask, render_template

app=Flask(__name__)

@app.route('/')
def index():
	return render_template(index.html)
	
if __name__ == '__main__':
	app.run()

3.3 Using static files

Continue to ask GPT: the script for the front-end page to obtain the static folder
insert image description here

# -*- utf-8 -*-
from flask import Flask, render_template

app=Flask(__name__)
app.template_folder = 'templates' #设置模板文件夹路径
app.static_folder = 'static' #设置静态文件夹路径

@app.route('/')
def index():
	return render_template(index.html)
	
if __name__ == '__main__':
	app.run()

insert image description here

<!DOCTYPE html>
<html>
	<head>
		<title>My Website</title>
		<link rel="stylesheet" href="{
     
     {url_for('static’,filename='style.css') }}">
	</head>
	<body>
		<h1>Welcome to mywebsite</h1>
		<img src="{
     
     { url_for('static', filename='imagejpg') }}" alt="My lmage" >
		<script src="{
     
     { url_for('static', filename='script.js') }}"> 		
		</script>
	</body>
</html>

The answer given by GPT has two parts: the back end and the front end.

  • The method of referencing the static folder in the backend
app.static_folder = 'static'
  • The method of using the static folder in the front end
"{
   
   {url_for('static', filename='css/bootstrap.css')}}"

4. Complete the website

At this time, I can finally appreciate the work.
The editor here used a free template file template downloaded from the Internet to realize it.
Start the website, and after seeing the website address, copy the address to the browser address bar.
insert image description here
Seeing that the first page of a website is opened, a website is built.
insert image description here
Please use the following address to experience ChatGPT
https://www.zhiyidata.cn/chatgpt/play
Let's play AI

Intelligently control the future billion points of innovation

Guess you like

Origin blog.csdn.net/all_night_in/article/details/130507282