Flask 创建网页

Python程序创建网页有服务端和前端。

网页源码

创建一个名为 templates 的文件夹,并在其中创建一个名为 index.html 的文件

<!-- templates/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Frontend</title>
</head>
<body>
<h1>Welcome to the Flask & HTML Demo</h1>
</body>
</html>

服务端脚本

# app.py
# pip install Flask

from flask import Flask, render_template, jsonify

app = Flask(__name__)

@app.route('/')
def index():
return render_template('index.html')

@app.route('/get_data')
def get_data():
return jsonify({"message": "Hello from Flask!"})

if __name__ == '__main__':
app.run(debug=True)
python app.py

开始运行

打开浏览器并访问 http://127.0.0.1:5000/