Cristobal299 commited on
Commit
d81bb6a
·
verified ·
1 Parent(s): c990caa

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,
24
+ title TEXT NOT NULL,
25
+ completed INTEGER NOT NULL DEFAULT 0
26
+ )
27
+ """
28
+ )
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
+ if __name__ == "__main__":
89
+ # Ensure the database exists before the first request
90
+ if not os.path.isfile(DATABASE):
91
+ init_db()
92
+ app.run(host="0.0.0.0", port=7860)