Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
-
import datetime
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
posts = [{"title": "Welcome Post", "date": "2026-02-26", "content": "Welcome to my blog!"}]
|
| 11 |
-
with open(POSTS_FILE, 'w') as f:
|
| 12 |
-
json.dump(posts, f)
|
| 13 |
-
return posts
|
| 14 |
-
else:
|
| 15 |
-
with open(POSTS_FILE, 'r') as f:
|
| 16 |
-
return json.load(f)
|
| 17 |
|
| 18 |
def show_post(title):
|
| 19 |
-
posts = load_posts()
|
| 20 |
for post in posts:
|
| 21 |
if post["title"] == title:
|
| 22 |
-
return
|
| 23 |
-
return "
|
| 24 |
|
| 25 |
def add_post(title, content):
|
| 26 |
-
if
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
new_post = {"title": title, "date": datetime.datetime.now().strftime("%Y-%m-%d"), "content": content}
|
| 31 |
-
posts.append(new_post)
|
| 32 |
-
|
| 33 |
-
with open(POSTS_FILE, 'w') as f:
|
| 34 |
-
json.dump(posts, f)
|
| 35 |
-
|
| 36 |
-
titles = [p["title"] for p in posts]
|
| 37 |
-
return f"Added: {title}", "", "", gr.Dropdown.update(choices=titles)
|
| 38 |
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
gr.Markdown("# My Blog")
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
gr.Markdown("### Add New Post")
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
status_text = gr.Textbox(label="Status", interactive=False)
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# 简单的文章存储
|
| 5 |
+
posts = [
|
| 6 |
+
{"title": "Test Post", "content": "This is a test post", "date": "2026-02-26"}
|
| 7 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def show_post(title):
|
|
|
|
| 10 |
for post in posts:
|
| 11 |
if post["title"] == title:
|
| 12 |
+
return post["content"]
|
| 13 |
+
return "Not found"
|
| 14 |
|
| 15 |
def add_post(title, content):
|
| 16 |
+
if title and content:
|
| 17 |
+
posts.append({"title": title, "content": content, "date": "2026-02-26"})
|
| 18 |
+
return "Added successfully", gr.Dropdown.update(choices=[p["title"] for p in posts])
|
| 19 |
+
return "Please fill all fields", gr.Dropdown.update(choices=[p["title"] for p in posts])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
with gr.Blocks() as demo:
|
| 22 |
gr.Markdown("# My Blog")
|
| 23 |
|
| 24 |
+
dropdown = gr.Dropdown(choices=[p["title"] for p in posts], label="Select post")
|
| 25 |
+
show_btn = gr.Button("Show Post")
|
| 26 |
+
display = gr.Markdown()
|
| 27 |
|
| 28 |
+
show_btn.click(show_post, dropdown, display)
|
| 29 |
|
| 30 |
gr.Markdown("### Add New Post")
|
| 31 |
+
title_in = gr.Textbox(label="Title")
|
| 32 |
+
content_in = gr.Textbox(label="Content", lines=5)
|
| 33 |
+
add_btn = gr.Button("Add Post")
|
|
|
|
| 34 |
|
| 35 |
+
add_btn.click(add_post, [title_in, content_in], gr.Textbox(value="Status", interactive=False))
|
| 36 |
|
| 37 |
demo.launch()
|