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

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +74 -106
app.py CHANGED
@@ -1,19 +1,23 @@
1
  # -*- coding: utf-8 -*-
2
  import os
3
  import sqlite3
4
- import gradio as gr
5
 
6
- DB_PATH = os.path.join(os.path.dirname(__file__), "tasks.db")
7
 
8
- def get_connection():
9
- conn = sqlite3.connect(DB_PATH)
10
- conn.row_factory = sqlite3.Row
11
- return conn
 
 
 
 
12
 
13
  def init_db():
14
- conn = get_connection()
15
- cur = conn.cursor()
16
- cur.execute(
17
  """
18
  CREATE TABLE IF NOT EXISTS tasks (
19
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -25,104 +29,68 @@ def init_db():
25
  conn.commit()
26
  conn.close()
27
 
28
- def fetch_all():
29
- conn = get_connection()
30
- cur = conn.cursor()
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))
51
- conn.commit()
52
- conn.close()
53
- return refresh_tasks()
54
-
55
- def update_task(task_id, new_title, completed):
56
- if not task_id:
57
- return refresh_tasks()
58
- conn = get_connection()
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()
69
-
70
- def delete_task(task_id):
71
- if not task_id:
72
- return refresh_tasks()
73
- conn = get_connection()
74
- cur = conn.cursor()
75
- cur.execute("DELETE FROM tasks WHERE id = ?", (int(task_id),))
76
- conn.commit()
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
-
84
- with gr.Blocks() as demo:
85
- gr.Markdown("# Docker Task Manager")
86
- with gr.Row():
87
- task_table = gr.Dataframe(
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")
95
- with gr.Row():
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()
 
 
 
 
1
  # -*- coding: utf-8 -*-
2
  import os
3
  import sqlite3
4
+ from flask import Flask, request, jsonify, g
5
 
6
+ app = Flask(__name__)
7
 
8
+ DATABASE = os.path.join(os.path.dirname(__file__), "tasks.db")
9
+
10
+ def get_db():
11
+ db = getattr(g, "_database", None)
12
+ if db is None:
13
+ db = g._database = sqlite3.connect(DATABASE)
14
+ db.row_factory = sqlite3.Row
15
+ return db
16
 
17
  def init_db():
18
+ conn = sqlite3.connect(DATABASE)
19
+ cursor = conn.cursor()
20
+ cursor.execute(
21
  """
22
  CREATE TABLE IF NOT EXISTS tasks (
23
  id INTEGER PRIMARY KEY AUTOINCREMENT,
 
29
  conn.commit()
30
  conn.close()
31
 
32
+ @app.teardown_appcontext
33
+ def close_connection(exception):
34
+ db = getattr(g, "_database", None)
35
+ if db is not None:
36
+ db.close()
37
+
38
+ @app.route("/tasks", methods=["GET"])
39
+ def list_tasks():
40
+ db = get_db()
41
+ cursor = db.execute("SELECT id, title, completed FROM tasks")
42
+ tasks = [
43
+ {"id": row["id"], "title": row["title"], "completed": bool(row["completed"])}
44
+ for row in cursor.fetchall()
45
+ ]
46
+ return jsonify(tasks)
47
+
48
+ @app.route("/tasks", methods=["POST"])
49
+ def create_task():
50
+ data = request.get_json()
51
+ title = data.get("title")
52
  if not title:
53
+ return jsonify({"error": "title is required"}), 400
54
+ db = get_db()
55
+ cursor = db.execute(
56
+ "INSERT INTO tasks (title, completed) VALUES (?, ?)",
57
+ (title, 0),
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  )
59
+ db.commit()
60
+ task_id = cursor.lastrowid
61
+ return jsonify({"id": task_id, "title": title, "completed": False}), 201
62
+
63
+ @app.route("/tasks/<int:task_id>", methods=["PUT"])
64
+ def update_task(task_id):
65
+ data = request.get_json()
66
+ title = data.get("title")
67
+ completed = data.get("completed")
68
+ if title is None and completed is None:
69
+ return jsonify({"error": "nothing to update"}), 400
70
+ db = get_db()
71
+ if title is not None:
72
+ db.execute("UPDATE tasks SET title = ? WHERE id = ?", (title, task_id))
73
+ if completed is not None:
74
+ db.execute(
75
+ "UPDATE tasks SET completed = ? WHERE id = ?",
76
+ (1 if completed else 0, task_id),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  )
78
+ db.commit()
79
+ return jsonify({"id": task_id, "title": title, "completed": completed})
 
 
 
 
 
 
 
 
 
 
80
 
81
+ @app.route("/tasks/<int:task_id>", methods=["DELETE"])
82
+ def delete_task(task_id):
83
+ db = get_db()
84
+ db.execute("DELETE FROM tasks WHERE id = ?", (task_id,))
85
+ db.commit()
86
+ return "", 204
87
+
88
+ @app.route("/", methods=["GET"])
89
+ def index():
90
+ return "Task API is running."
91
+
92
+ if __name__ == "__main__":
93
+ # Ensure the database exists before the first request
94
+ if not os.path.isfile(DATABASE):
95
+ init_db()
96
+ app.run(host="0.0.0.0", port=7860)