-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (38 loc) · 1.45 KB
/
app.py
File metadata and controls
51 lines (38 loc) · 1.45 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
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app) # Allow cross-origin requests (needed for Streamlit frontend)
# Simulated "Database"
data_store = [
{"id": 1, "name": "Alice", "email": "alice@example.com"},
{"id": 2, "name": "Bob", "email": "bob@example.com"}
]
# Auto-increment ID
next_id = 3
@app.route('/get_data', methods=['GET'])
def get_data():
return jsonify(data_store), 200
@app.route('/add_data', methods=['POST'])
def add_data():
global next_id
item = request.get_json()
item["id"] = next_id
data_store.append(item)
next_id += 1
return jsonify({"message": "Data added successfully"}), 201
@app.route('/update_data/<int:item_id>', methods=['PUT'])
def update_data(item_id):
updated_item = request.get_json()
for item in data_store:
if item["id"] == item_id:
item["name"] = updated_item.get("name", item["name"])
item["email"] = updated_item.get("email", item["email"])
return jsonify({"message": "Data updated"}), 200
return jsonify({"message": "Item not found"}), 404
@app.route('/delete_data/<int:item_id>', methods=['DELETE'])
def delete_data(item_id):
global data_store
data_store = [item for item in data_store if item["id"] != item_id]
return jsonify({"message": "Item deleted"}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)