60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
|
from flask import Flask
|
||
|
from flask import render_template
|
||
|
from flask import Flask, request, jsonify
|
||
|
import threading
|
||
|
import requests, socket
|
||
|
import pymysql
|
||
|
|
||
|
# from flask_cors import CORS
|
||
|
|
||
|
class Website(threading.Thread):
|
||
|
def __init__(self):
|
||
|
threading.Thread.__init__(self)
|
||
|
self.db = pymysql.connect(host='140.125.21.65', port=3307, user='VIP125', passwd='@VIPvip125', db='VIP125',
|
||
|
charset='utf8')
|
||
|
self.app = Flask(__name__)
|
||
|
# CORS(self.app)
|
||
|
|
||
|
#前端顯示
|
||
|
self.app.add_url_rule('/', 'Index', self.Index)
|
||
|
self.app.add_url_rule('/next/<id>', 'Next', self.Next)
|
||
|
|
||
|
# API
|
||
|
self.app.add_url_rule('/api/get_data', 'get_data', self.get_data,methods=['get'])
|
||
|
self.app.add_url_rule('/api/get_one_data/<id>', 'get_one_data', self.get_one_data, methods=['get'])
|
||
|
|
||
|
|
||
|
|
||
|
def run(self):
|
||
|
#self.app.run()
|
||
|
self.app.run(host="0.0.0.0", port="5500")
|
||
|
|
||
|
# 前端
|
||
|
def Index(self):
|
||
|
return render_template('Index.html')
|
||
|
def Next(self,id): # 攜帶參數
|
||
|
return render_template('Next.html',id = id)
|
||
|
|
||
|
# 後端API
|
||
|
def get_data(self):
|
||
|
cursor = self.db.cursor()
|
||
|
sql = "select * from teaching_sql_1"
|
||
|
cursor.execute(sql)
|
||
|
data = cursor.fetchall()
|
||
|
print(f'ALL_data = {data}')
|
||
|
# 返回 JSON 响应
|
||
|
return jsonify(data)
|
||
|
|
||
|
def get_one_data(self,id):
|
||
|
cursor = self.db.cursor()
|
||
|
sql = f"SELECT * FROM teaching_sql_1 WHERE student_id='{id}';"
|
||
|
print(sql)
|
||
|
cursor.execute(sql)
|
||
|
data = cursor.fetchall()
|
||
|
print(f'user_id = {data}')
|
||
|
return jsonify(data)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
website = Website()
|
||
|
website.run()
|