-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
洪宇轩
committed
Aug 14, 2023
1 parent
14b90c2
commit 1422887
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import os | ||
import yaml | ||
from datetime import datetime | ||
from flask import Flask, render_template, request, redirect, url_for, send_from_directory | ||
import uuid | ||
|
||
app = Flask(__name__) | ||
|
||
DATA_FILE = "data.yaml" | ||
|
||
def load_data(): | ||
if not os.path.exists(DATA_FILE): | ||
return [] | ||
with open(DATA_FILE, "r") as file: | ||
return yaml.load(file, Loader=yaml.FullLoader) | ||
|
||
def save_data(data): | ||
with open(DATA_FILE, "w") as file: | ||
yaml.dump(data, file) | ||
|
||
@app.route("/", methods=["GET", "POST"]) | ||
def submit(): | ||
if request.method == "POST": | ||
name = request.form.get("name") | ||
book = request.form.get("book") | ||
duration = request.form.get("duration") | ||
image = request.files.get("image") | ||
if name and book and duration and image: | ||
filename = uuid.uuid4() | ||
data = load_data() | ||
data.append({ | ||
"name": name, | ||
"book": book, | ||
"duration": duration, | ||
"date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), | ||
"image_filename": str(filename) + ".jpg" | ||
}) | ||
save_data(data) | ||
if not os.path.exists("img"): | ||
os.makedirs("img") | ||
image.save(os.path.join("img", str(filename) + ".jpg")) | ||
return redirect(url_for("submit")) | ||
return render_template("index.html") | ||
|
||
@app.route("/admin") | ||
def admin(): | ||
data = load_data() | ||
return render_template("admin.html", data=data) | ||
|
||
|
||
@app.route('/img/<path:filename>') | ||
def img(filename): | ||
file_path = os.path.join('img', filename) | ||
return send_from_directory('img', filename) | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(debug=False, port=9099, host="0.0.0.0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>阅读打卡管理后台</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
margin: 0; | ||
padding: 0; | ||
background-color: #f5f5f5; | ||
} | ||
h1 { | ||
text-align: center; | ||
padding: 20px; | ||
background-color: #007bff; | ||
color: #ffffff; | ||
} | ||
table { | ||
width: 80%; | ||
margin: 20px auto; | ||
border-collapse: collapse; | ||
} | ||
th, td { | ||
border: 1px solid #ccc; | ||
padding: 10px; | ||
text-align: center; | ||
} | ||
th { | ||
background-color: #f5f5f5; | ||
} | ||
img { | ||
max-width: 100px; | ||
max-height: 100px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>阅读打卡数据</h1> | ||
<table> | ||
<tr> | ||
<th>姓名</th> | ||
<th>书目</th> | ||
<th>阅读时长(分钟)</th> | ||
<th>提交日期</th> | ||
<th>图片</th> | ||
</tr> | ||
{% for entry in data %} | ||
<tr> | ||
<td>{{ entry.name }}</td> | ||
<td>{{ entry.book }}</td> | ||
<td>{{ entry.duration }}</td> | ||
<td>{{ entry.date }}</td> | ||
<td><img src="{{ url_for('img', filename= entry.image_filename) }}" alt="图片未找到" width="100"></td> | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>阅读打卡</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
margin: 0; | ||
padding: 0; | ||
background-color: #f3f3f3; | ||
} | ||
h1 { | ||
text-align: center; | ||
padding: 20px; | ||
background-color: #007bff; | ||
color: #ffffff; | ||
} | ||
form { | ||
max-width: 400px; | ||
margin: 0 auto; | ||
padding: 20px; | ||
background-color: #ffffff; | ||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | ||
} | ||
label { | ||
display: block; | ||
margin-bottom: 5px; | ||
font-weight: bold; | ||
} | ||
input[type="text"], | ||
input[type="number"], | ||
input[type="file"] { | ||
width: 70%; | ||
padding: 10px; | ||
margin-bottom: 15px; | ||
border: 1px solid #ccc; | ||
border-radius: 5px; | ||
} | ||
input[type="submit"] { | ||
display: block; | ||
width: 100%; | ||
padding: 10px; | ||
background-color: #007bff; | ||
color: #ffffff; | ||
border: none; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
} | ||
input[type="submit"]:hover { | ||
background-color: #0056b3; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>每日阅读打卡</h1> | ||
<form method="post" enctype="multipart/form-data"> | ||
<label for="name">姓名:</label> | ||
<input type="text" id="name" name="name" required><br><br> | ||
<label for="book">书目:</label> | ||
<input type="text" id="book" name="book" required><br><br> | ||
<label for="duration">阅读时长(分钟):</label> | ||
<input type="number" id="duration" name="duration" min="1" required><br><br> | ||
<label for="image">上传图片:</label> | ||
<input type="file" id="image" name="image" accept="image/*" required><br><br> | ||
<input type="submit" value="提交"> | ||
</form> | ||
</body> | ||
</html> |