要用 Python Flask 搭建一个简单的爱心表白网页,可以按照以下步骤操作:
1. 安装 Flask
首先,确保你已经安装了 Flask。如果没有安装,可以使用 pip
安装:
pip install Flask
2. 创建 Flask 应用
接下来,创建一个 Flask 应用,并设置一个简单的路由来渲染网页。你可以创建一个名为 app.py
的 Python 文件。
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
3. 创建 HTML 页面
在同一目录下创建一个 templates
文件夹,并在其中创建一个名为 index.html
的文件。这个文件将包含 HTML 代码,用来显示表白内容。
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>爱心表白</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
text-align: center;
background-color: #ffffff;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
padding: 50px;
width: 300px;
}
h1 {
color: #ff6666;
}
.heart {
font-size: 60px;
color: #ff3333;
animation: heartAnimation 1s infinite alternate;
}
@keyframes heartAnimation {
0% {
transform: scale(1);
}
100% {
transform: scale(1.2);
}
}
p {
font-size: 18px;
color: #555;
}
</style>
</head>
<body>
<div class="container">
<h1>亲爱的,</h1>
<p>我想对你说:</p>
<div class="heart">❤️</div>
<p>我喜欢你!</p>
</div>
</body>
</html>
4. 启动 Flask 应用
运行 app.py
文件,启动 Flask 服务:
python app.py
5. 访问网页
在浏览器中访问 http://127.0.0.1:5000/
,你应该能看到一个简单的爱心表白网页,显示一个动态的爱心和表白文字。