各位iT邦前輩們,晚安,先前依照某iT邦大提供的方法,做好一個下拉式選單連接資料庫,但是如果要作成雙關聯下拉式,選取好之後按鈕,再從資料庫抓取資料,卻一直沒辦法,資料庫如下:
ID | Model_Name | value_without | time |
---|---|---|---|
0050 | Multiple Regression | 0.5781 | 13.03 |
0050 | Logistic Regression | 0.587 | 10.93 |
0051 | Multiple Regression | 0.5158 | 10.98 |
0051 | Logistic Regression | 0.572 | 10.16 |
0052 | Multiple Regression | 0.5734 | 14.67 |
0052 | Logistic Regression | 0.5396 | 12.42 |
目前寫好的Python:
class ETL(db.Model):
tablename = 'etl1'
ID = db.Column(db.Float)
Model_Name = db.Column(db.String(255))
value_without = db.Column(db.Float)
time = db.Column(db.Float)
@app.route('/', methods=['GET', 'POST'])
def index():
etl1s = ETL.query.all()
if 'eid' in request.form:
eid = request.form.get('eid' ,type=float)
step1 = ETL.query.get(eid)
return render_template("index.html", etl1s=etl1s, step1=step1)
return render_template("index.html", etl1s=etl1s)
HTML:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>ETL_SQL</title>
</head>
<body>
<form method="POST">
<select name="eid" id="eid">
<option value="0050">0050</option>
<option value="0051">0051</option>
<option value="0052">0052</option>
</select>
<br>
<input type="submit" value="Search" onClick="windows.location.reload()">
<br>
</form>
{% if step1 %}
<div>ID:{{step1.ID}}</div>
<div>Model Name:{{step1.Model_Name}}</div>
<div>Value Without:{{step1.value_without}}</div>
<div>Time:{{step1.time}}</div>
{% endif %}
</body>
</html>
現在問題想要作成兩個關聯式下拉式選單,也就是在網頁中讓使用者下拉式選擇欄位ID,再選擇Model_Name,然後按按鈕做確認,從資料庫抓取所選擇的ID、Model_Name、value_without及time欄位資料,那麼在Python及HTML及要多加那些語法,懇請IT邦大大們解法,謝謝。
唉,自己看吧。這個很基本。
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test.db")
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////{}'.format(db_path)
db = SQLAlchemy(app)
class ExamData(db.Model):
tablename = 'ExamData'
id = db.Column(db.Integer, primary_key=True)
eid = db.Column(db.Integer)
model_Name = db.Column(db.String(255))
value_without = db.Column(db.Float)
time = db.Column(db.Float)
@app.route('/', methods=['GET', 'POST'])
def all_persons():
EID = db.session.query(ExamData.eid).distinct() # 取得所有EID
model_Name = db.session.query(ExamData.model_Name).distinct() # 取得所有model_Name
if 'eid' in request.form and 'model_Name' in request.form:
result = ExamData.query.filter_by(eid=int(request.form.get("eid")), model_Name=request.form.get("model_Name"))
return render_template("index.html", EID=EID, model_Name=model_Name, result=result)
return render_template("index.html", EID=EID, model_Name=model_Name)
if __name__ == '__main__':
if not os.path.exists(db_path):
db.create_all()
db.session.add(ExamData(eid=50, model_Name='Multiple Regression',
value_without=0.5781, time=13.03))
db.session.add(ExamData(eid=50, model_Name='Logistic Regression',
value_without=0.587, time=10.93))
db.session.add(ExamData(eid=51, model_Name='Multiple Regression',
value_without=0.5158, time=10.98))
db.session.add(ExamData(eid=51, model_Name='Logistic Regression',
value_without=0.572, time=10.16))
db.session.commit()
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
<body>
<form method="POST">
<select name="eid">
{% for e in EID %}
<option>{{ e.eid }}</option>
{% endfor %}
</select>
<select name="model_Name">
{% for m in model_Name %}
<option>{{ m.model_Name }}</option>
{% endfor %}
</select>
<input type="submit" value="送出" id="btn">
</form>
{% for r in result %}
<div>{{ r.eid }}</div>
<div>{{ r.model_Name }}</div>
<div>{{ r.value_without }}</div>
<div>{{ r.time }}</div>
{% endfor %}
</body>
</html>