我正在使用 Python Flask 從一個簡單的 html 檔案中獲取輸入。HTML 要求用戶輸入一個狀態,當他們這樣做時,我希望輸出是一個圖。
我有以下創建 Python Flask 的代碼,但是當用戶輸入狀態時,繪圖輸出在我的 Jupyter Notebook 檔案中,而不是在 HTML 頁面上。
我已經看到使用 Figure() 來讓繪圖顯示在 HTML 頁面上的方法,但我不確定如何使用 figure 函式重新創建當前繪圖。
app = Flask(__name__)
@app.route('/')
def form():
return render_template("flask_form.html")
@app.route('/', methods = ['POST'])
def form_post():
state = request.form['userinput']
dff = df[df['state'] == state]
dff['total_icu_beds_7_day_avg'] = dff['total_icu_beds_7_day_avg'].astype(float)
dff['total_beds_7_day_avg'] = dff['total_beds_7_day_avg'].astype(float)
dff['inpatient_beds_7_day_avg'] = dff['inpatient_beds_7_day_avg'].astype(float)
pivot = np.round(dff.pivot_table(index = ["collection_week"], values = ["total_icu_beds_7_day_avg", "total_beds_7_day_avg", "inpatient_beds_7_day_avg"], aggfunc = [np.mean]), 0)
pivot.columns = ['Inpatient 7 day Avg', 'Total Beds 7 day Avg', 'Total ICU Beds 7 day Avg']
pivot = pivot.reset_index()
x = pivot['collection_week']
y1 = pivot['Inpatient 7 day Avg']
y2 = pivot['Total Beds 7 day Avg']
y3 = pivot['Total ICU Beds 7 day Avg']
title = str(state " State Staffed Beds")
plt.figure(figsize=(16,6))
plt.plot(x, y1, label = 'Inpatient AVG', color = 'blue')
plt.plot(x, y2, label = 'Total Beds AVG', color = 'red')
plt.plot(x, y3, label = 'Total ICU Beds AVG', color = 'cyan')
plt.legend()
plt.title(title, fontweight = 'bold')
plt.ylabel("Number of Staffed Beds")
plt.xlabel("Date")
plt.show()
這是我的 HTML 代碼,我知道繪圖需要在輸出中,但不知道如何編碼:
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Hospital Capacity by State</h1>
<p>Enter the abbreviation of your desired state:</p>
<form action="." method="POST">
<input type="text" name="userinput">
<input type="submit" name="my-form" value="Send">
<output> </output>
</form>
</body>
</html>
uj5u.com熱心網友回復:
Python Flask 代碼:
app = Flask(__name__)
@app.route('/')
def form():
return render_template("flask_form_plot.html")
@app.route('/', methods = ['POST'])
def form_post():
state = request.form['userinput']
dff = df[df['state'] == state]
dff['total_icu_beds_7_day_avg'] = dff['total_icu_beds_7_day_avg'].astype(float)
dff['total_beds_7_day_avg'] = dff['total_beds_7_day_avg'].astype(float)
dff['inpatient_beds_7_day_avg'] = dff['inpatient_beds_7_day_avg'].astype(float)
pivot = np.round(dff.pivot_table(index = ["collection_week"], values = ["total_icu_beds_7_day_avg", "total_beds_7_day_avg", "inpatient_beds_7_day_avg"], aggfunc = [np.mean]), 0)
pivot.columns = ['Inpatient 7 day Avg', 'Total Beds 7 day Avg', 'Total ICU Beds 7 day Avg']
pivot = pivot.reset_index()
x = pivot['collection_week']
y1 = pivot['Inpatient 7 day Avg']
y2 = pivot['Total Beds 7 day Avg']
y3 = pivot['Total ICU Beds 7 day Avg']
plt.figure(figsize=(16,6))
plt.plot(x, y1, label = 'Inpatient AVG', color = 'blue')
plt.plot(x, y2, label = 'Total Beds AVG', color = 'red')
plt.plot(x, y3, label = 'Total ICU Beds AVG', color = 'cyan')
plt.legend()
plt.title(str(state) " State Staffed Beds", fontweight = 'bold')
plt.ylabel("Number of Staffed Beds")
plt.xlabel("Date")
img = io.BytesIO()
plt.savefig(img, format = 'png')
img.seek(0)
plot_data = urllib.parse.quote(base64.b64encode(img.read()).decode())
return render_template('flask_form_plot.html', plot_url = plot_data)
HTML 代碼:
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Hospital Capacity by State</h1>
<p>Enter the abbreviation of your desired state:</p>
<form action="." method="POST">
<input type="text" name="userinput">
<input type="submit" name="my-form" value="Send">
<br>
<br>
<output> <img src="data:image/png;base64, {{ plot_url }}" width="1000" height="500" alt="graph"> </output>
</form>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/459285.html
標籤:Python html matplotlib 烧瓶
