Cristobal299 commited on
Commit
4831f3a
·
verified ·
1 Parent(s): cc14ebd

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +34 -19
app.py CHANGED
@@ -31,20 +31,20 @@ def fetch_all():
31
  cur.execute("SELECT id, title, completed FROM tasks")
32
  rows = cur.fetchall()
33
  conn.close()
34
- data = [[r["id"], r["title"], bool(r["completed"])] for r in rows]
35
- return data
36
 
37
  def get_task_options():
38
- data = fetch_all()
39
- return [str(row[0]) for row in data]
 
40
 
41
  def refresh_tasks():
42
- data = fetch_all()
43
- return data, ["id", "title", "completed"]
44
 
45
  def add_task(title):
46
  if not title:
47
- return fetch_all(), ["id", "title", "completed"]
48
  conn = get_connection()
49
  cur = conn.cursor()
50
  cur.execute("INSERT INTO tasks (title, completed) VALUES (?, ?)", (title, 0))
@@ -59,7 +59,10 @@ def update_task(task_id, new_title, completed):
59
  cur = conn.cursor()
60
  if new_title:
61
  cur.execute("UPDATE tasks SET title = ? WHERE id = ?", (new_title, int(task_id)))
62
- cur.execute("UPDATE tasks SET completed = ? WHERE id = ?", (1 if completed else 0, int(task_id)))
 
 
 
63
  conn.commit()
64
  conn.close()
65
  return refresh_tasks()
@@ -74,7 +77,7 @@ def delete_task(task_id):
74
  conn.close()
75
  return refresh_tasks()
76
 
77
- # Initialize database on first run
78
  if not os.path.isfile(DB_PATH):
79
  init_db()
80
 
@@ -85,6 +88,7 @@ with gr.Blocks() as demo:
85
  label="Tasks",
86
  headers=["id", "title", "completed"],
87
  datatype=["number", "str", "bool"],
 
88
  interactive=False,
89
  )
90
  refresh_btn = gr.Button("Refresh")
@@ -92,22 +96,33 @@ with gr.Blocks() as demo:
92
  add_title = gr.Textbox(label="New Task Title", placeholder="Enter task title")
93
  add_btn = gr.Button("Add Task")
94
  with gr.Row():
95
- upd_id = gr.Dropdown(label="Task ID to Update", choices=get_task_options())
 
 
 
96
  upd_title = gr.Textbox(label="New Title (optional)", placeholder="Leave empty to keep current")
97
  upd_completed = gr.Checkbox(label="Completed")
98
  upd_btn = gr.Button("Update Task")
99
  with gr.Row():
100
- del_id = gr.Dropdown(label="Task ID to Delete", choices=get_task_options())
 
 
 
101
  del_btn = gr.Button("Delete Task")
102
 
103
  # Bind actions
104
- refresh_btn.click(fn=refresh_tasks, inputs=None, outputs=[task_table])
105
- add_btn.click(fn=add_task, inputs=add_title, outputs=[task_table])
106
- upd_btn.click(fn=update_task, inputs=[upd_id, upd_title, upd_completed], outputs=[task_table])
107
- del_btn.click(fn=delete_task, inputs=del_id, outputs=[task_table])
108
-
109
- # Update dropdown options after any change
110
- for btn in [add_btn, upd_btn, del_btn]:
111
- btn.click(fn=get_task_options, inputs=None, outputs=[upd_id, del_id])
 
 
 
 
 
112
 
113
  demo.launch()
 
31
  cur.execute("SELECT id, title, completed FROM tasks")
32
  rows = cur.fetchall()
33
  conn.close()
34
+ return [[r["id"], r["title"], bool(r["completed"])] for r in rows]
 
35
 
36
  def get_task_options():
37
+ """Return list of task ids as strings for dropdowns."""
38
+ tasks = fetch_all()
39
+ return [str(row[0]) for row in tasks]
40
 
41
  def refresh_tasks():
42
+ """Return only the data for the Dataframe component."""
43
+ return fetch_all()
44
 
45
  def add_task(title):
46
  if not title:
47
+ return refresh_tasks()
48
  conn = get_connection()
49
  cur = conn.cursor()
50
  cur.execute("INSERT INTO tasks (title, completed) VALUES (?, ?)", (title, 0))
 
59
  cur = conn.cursor()
60
  if new_title:
61
  cur.execute("UPDATE tasks SET title = ? WHERE id = ?", (new_title, int(task_id)))
62
+ cur.execute(
63
+ "UPDATE tasks SET completed = ? WHERE id = ?",
64
+ (1 if completed else 0, int(task_id)),
65
+ )
66
  conn.commit()
67
  conn.close()
68
  return refresh_tasks()
 
77
  conn.close()
78
  return refresh_tasks()
79
 
80
+ # Initialise DB on first run
81
  if not os.path.isfile(DB_PATH):
82
  init_db()
83
 
 
88
  label="Tasks",
89
  headers=["id", "title", "completed"],
90
  datatype=["number", "str", "bool"],
91
+ value=[],
92
  interactive=False,
93
  )
94
  refresh_btn = gr.Button("Refresh")
 
96
  add_title = gr.Textbox(label="New Task Title", placeholder="Enter task title")
97
  add_btn = gr.Button("Add Task")
98
  with gr.Row():
99
+ upd_id = gr.Dropdown(
100
+ label="Task ID to Update",
101
+ choices=get_task_options(),
102
+ )
103
  upd_title = gr.Textbox(label="New Title (optional)", placeholder="Leave empty to keep current")
104
  upd_completed = gr.Checkbox(label="Completed")
105
  upd_btn = gr.Button("Update Task")
106
  with gr.Row():
107
+ del_id = gr.Dropdown(
108
+ label="Task ID to Delete",
109
+ choices=get_task_options(),
110
+ )
111
  del_btn = gr.Button("Delete Task")
112
 
113
  # Bind actions
114
+ refresh_btn.click(fn=refresh_tasks, inputs=None, outputs=task_table)
115
+
116
+ add_btn.click(fn=add_task, inputs=add_title, outputs=task_table).then(
117
+ fn=get_task_options, inputs=None, outputs=[upd_id, del_id]
118
+ )
119
+
120
+ upd_btn.click(fn=update_task, inputs=[upd_id, upd_title, upd_completed], outputs=task_table).then(
121
+ fn=get_task_options, inputs=None, outputs=[upd_id, del_id]
122
+ )
123
+
124
+ del_btn.click(fn=delete_task, inputs=del_id, outputs=task_table).then(
125
+ fn=get_task_options, inputs=None, outputs=[upd_id, del_id]
126
+ )
127
 
128
  demo.launch()