-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_data.py
More file actions
34 lines (27 loc) · 904 Bytes
/
Copy pathload_data.py
File metadata and controls
34 lines (27 loc) · 904 Bytes
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
from flask import Flask, jsonify
from flask_cors import CORS
import mysql.connector
app = Flask(__name__)
CORS(app) # Enable CORS so your HTML can fetch data
# MySQL connection config
db_config = {
'host': 'localhost',
'user': 'root',
'password': '',
'database': 'chatbot_db'
}
@app.route('/counts')
def get_counts():
try:
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor(dictionary=True)
cursor.execute("SELECT district, value FROM district_counts") # Adjust table/columns
results = cursor.fetchall()
counts = {row['district']: float(row['value']) for row in results}
cursor.close()
conn.close()
return jsonify(counts)
except mysql.connector.Error as err:
return jsonify({"error": str(err)}), 500
if __name__ == '__main__':
app.run(debug=True)