Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
CORS(app) # Enable CORS for all routes
|
| 6 |
+
|
| 7 |
+
# Sample data
|
| 8 |
+
users = [
|
| 9 |
+
{"id": 1, "name": "John Doe", "email": "john@example.com"},
|
| 10 |
+
{"id": 2, "name": "Jane Smith", "email": "jane@example.com"},
|
| 11 |
+
{"id": 3, "name": "Bob Johnson", "email": "bob@example.com"}
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
# Route to get all users
|
| 15 |
+
@app.route('/api/users', methods=['GET'])
|
| 16 |
+
def get_users():
|
| 17 |
+
return jsonify(users)
|
| 18 |
+
|
| 19 |
+
# Route to get a specific user by ID
|
| 20 |
+
@app.route('/api/users/<int:user_id>', methods=['GET'])
|
| 21 |
+
def get_user(user_id):
|
| 22 |
+
user = [user for user in users if user['id'] == user_id]
|
| 23 |
+
if user:
|
| 24 |
+
return jsonify(user[0])
|
| 25 |
+
else:
|
| 26 |
+
return jsonify({"error": "User not found"}), 404
|
| 27 |
+
|
| 28 |
+
if __name__ == '__main__':
|
| 29 |
+
app.run(debug=True)
|