tpwang199655 commited on
Commit
d108c8a
·
verified ·
1 Parent(s): edc4bc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -37
app.py CHANGED
@@ -1,56 +1,37 @@
1
  import gradio as gr
2
  import json
3
- import datetime
4
- import os
5
 
6
- POSTS_FILE = "blog_posts.json"
7
-
8
- def load_posts():
9
- if not os.path.exists(POSTS_FILE):
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 f"**{post['title']}**\n\n*{post['date']}*\n\n{post['content']}"
23
- return "Post not found"
24
 
25
  def add_post(title, content):
26
- if not title or not content:
27
- return "Please fill all fields", "", ""
28
-
29
- posts = load_posts()
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
- post_dropdown = gr.Dropdown(choices=[p["title"] for p in load_posts()], label="Select a post")
43
- show_button = gr.Button("Show Post")
44
- output = gr.Markdown()
45
 
46
- show_button.click(show_post, post_dropdown, output)
47
 
48
  gr.Markdown("### Add New Post")
49
- title_box = gr.Textbox(label="Post Title")
50
- content_box = gr.Textbox(label="Post Content", lines=5, placeholder="Paste your content here...")
51
- add_button = gr.Button("Add Post")
52
- status_text = gr.Textbox(label="Status", interactive=False)
53
 
54
- add_button.click(add_post, [title_box, content_box], [status_text, title_box, content_box, post_dropdown])
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()