from flask import Flask, render_template, jsonify
import pymysql
import os
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
# 配置数据库连接
def get_db_connection():
return pymysql.connect(
host="Hadoop21",
user="root",
password="123456789",
database="my_restaurant_db",
cursorclass=pymysql.cursors.DictCursor
)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/data')
def get_data():
db = get_db_connection()
try:
with db.cursor() as cursor:
cursor.execute("SELECT * FROM restaurant_categories")
restaurant_categories = cursor.fetchall()
return jsonify(restaurant_categories)
finally:
db.close()
@app.route('/api/test')
def get_test():
db = get_db_connection()
try:
with db.cursor() as cursor:
cursor.execute("SELECT * FROM restaurant_categories")
restaurant_categories = cursor.fetchall()
res = {
"categories": [],
"values": []
}
for item in restaurant_categories:
res['categories'].append(item['category_name']) # 假设数据库字段名为category_name
res['values'].append(item['value']) # 假设数据库字段名为value
return jsonify(res)
finally:
db.close()
@app.route('/api/echarts3')
def get_echarts3():
res = [
{"value": 1048, "name": 'Search Engine'},
{"value": 735, "name": 'Direct'},
{"value": 580, "name": 'Email'},
{"value": 484, "name": 'Union Ads'},
{"value": 300, "name": 'Video Ads'}
]
return jsonify(res)
if __name__ == '__main__':
app.run(debug=True)
最后修改:2024 年 06 月 12 日
© 允许规范转载