как использовать dataframe на всех app.route flask? - PullRequest
0 голосов
/ 10 июля 2020

Я хочу создать сеть, используя Flask, которая может загружать из нее файл. Я использую фрейм данных и хочу использовать этот фрейм данных для всего маршрута своего приложения.

@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 Я получаю только пустой лист. Как решить?

1 Ответ

0 голосов
/ 10 июля 2020

Определите фрейм данных в глобальной области при инициализации экземпляра приложения.

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)

...