-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
53 lines (38 loc) · 1.09 KB
/
app.py
File metadata and controls
53 lines (38 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import Flask, render_template, request, jsonify
import json
import os
app = Flask(__name__)
NOTES_FILE = "/app/data/notes.json"
def load_notes():
if os.path.exists(NOTES_FILE):
with open(NOTES_FILE, "r") as f:
return json.load(f)
return []
def save_notes(notes):
with open(NOTES_FILE, "w") as f:
json.dump(notes, f)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/api/notes", methods=["GET"])
def get_notes():
return jsonify(load_notes())
@app.route("/api/notes", methods=["POST"])
def add_note():
notes = load_notes()
note = {
"id": len(notes) + 1,
"text": request.json["text"],
"category": request.json.get("category", "General")
}
notes.append(note)
save_notes(notes)
return jsonify(note), 201
@app.route("/api/notes/<int:note_id>", methods=["DELETE"])
def delete_note(note_id):
notes = load_notes()
notes = [n for n in notes if n["id"] != note_id]
save_notes(notes)
return "", 204
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)