Sync sites/site_1fe2ceb1af66.py 2025-06-22 16:05:09
Browse files- sites/site_1fe2ceb1af66.py +70 -0
sites/site_1fe2ceb1af66.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import flask
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
app = flask.Flask(__name__)
|
| 6 |
+
|
| 7 |
+
POSTS_FILE = "data.json"
|
| 8 |
+
|
| 9 |
+
def load_posts():
|
| 10 |
+
if not os.path.exists(POSTS_FILE):
|
| 11 |
+
return []
|
| 12 |
+
with open(POSTS_FILE, "r", encoding="utf-8") as f:
|
| 13 |
+
try:
|
| 14 |
+
return json.load(f)
|
| 15 |
+
except json.JSONDecodeError:
|
| 16 |
+
return []
|
| 17 |
+
|
| 18 |
+
def save_posts(posts):
|
| 19 |
+
with open(POSTS_FILE, "w", encoding="utf-8") as f:
|
| 20 |
+
json.dump(posts, f, indent=4, ensure_ascii=False)
|
| 21 |
+
|
| 22 |
+
posts = load_posts()
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@app.route("/", methods=["GET", "POST"])
|
| 26 |
+
def index():
|
| 27 |
+
if flask.request.method == "POST":
|
| 28 |
+
title = flask.request.form["title"]
|
| 29 |
+
content = flask.request.form["content"]
|
| 30 |
+
new_post = {"title": title, "content": content}
|
| 31 |
+
posts.append(new_post)
|
| 32 |
+
save_posts(posts)
|
| 33 |
+
|
| 34 |
+
html = flask.render_template_string("""
|
| 35 |
+
<!DOCTYPE html>
|
| 36 |
+
<html lang="ru">
|
| 37 |
+
<head>
|
| 38 |
+
<meta charset="UTF-8">
|
| 39 |
+
<title>Блог</title>
|
| 40 |
+
<style>
|
| 41 |
+
body { font-family: sans-serif; }
|
| 42 |
+
.post { margin-bottom: 20px; padding: 10px; border: 1px solid #ccc; }
|
| 43 |
+
.post h2 { margin-top: 0; }
|
| 44 |
+
</style>
|
| 45 |
+
</head>
|
| 46 |
+
<body>
|
| 47 |
+
<h1>Добавить пост</h1>
|
| 48 |
+
<form method="POST">
|
| 49 |
+
<label for="title">Заголовок:</label><br>
|
| 50 |
+
<input type="text" id="title" name="title" required><br>
|
| 51 |
+
<label for="content">Текст:</label><br>
|
| 52 |
+
<textarea id="content" name="content" rows="4" cols="50" required></textarea><br><br>
|
| 53 |
+
<input type="submit" value="Добавить">
|
| 54 |
+
</form>
|
| 55 |
+
|
| 56 |
+
<h1>Посты</h1>
|
| 57 |
+
{% for post in posts %}
|
| 58 |
+
<div class="post">
|
| 59 |
+
<h2>{{ post.title }}</h2>
|
| 60 |
+
<p>{{ post.content }}</p>
|
| 61 |
+
</div>
|
| 62 |
+
{% endfor %}
|
| 63 |
+
</body>
|
| 64 |
+
</html>
|
| 65 |
+
""", posts=posts)
|
| 66 |
+
return html
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if __name__ == '__main__':
|
| 70 |
+
app.run(host='0.0.0.0', port={{GENERATED_APP_PORT}})
|