簡體   English   中英

如何在所有 app.route flask 上使用 dataframe?

[英]how to use dataframe on all app.route flask?

我想使用 Flask 創建一個 web ,它可以從中下載文件。 我使用 dataframe 並希望將此 dataframe 用於我的所有應用程序路線。

@app.route('/')
def index():
   df=pd.read_sql_query(querysql(date), cnx )
   return return render_template("data.html",data=df.to_html())

@app.route('/download')
def download():
   output = 'codes_people.xlsx'
   writer = pd.ExcelWriter(output_file, engine='xlsxwriter')
   df.to_excel(writer, sheet_name="Codes")
   writer.save()
   return send_file(output, attachment_filename="testing.xlsx", as_attachment=True)

文檔 Excel 我只得到一張白紙。 如何解決?

初始化應用程序實例時,在全局 scope 處定義 dataframe。

df=pd.read_sql_query(querysql(date), cnx )

@app.route('/')
def index():
   return return render_template("data.html",data=df.to_html())

@app.route('/download')
def download():
   output = 'codes_people.xlsx'
   writer = pd.ExcelWriter(output_file, engine='xlsxwriter')
   df.to_excel(writer, sheet_name="Codes")
   writer.save()
   return send_file(output, attachment_filename="testing.xlsx", as_attachment=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM