回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>目前,我有一个仪表板,允许用户以xlsx/csv格式导出查询的数据,但生成的文件中包含第一个用户首先查询的数据。如果另一个用户查询他/她自己的数据并尝试导出,他/她将打开下载的文件,并在另一个实例中查看第一个用户查询的数据</p>
<p>我相信这是由于下面我的代码片段中显示了全局变量“queryd_df”。是否有更好的方法将回调中的数据共享/发送到服务器路由导出</p>
<p>任何其他建议都将不胜感激,谢谢</p>
<pre><code>@app.callback(
Output('tables', 'children'),
[Input("bom_1", "value"),
Input("bom_2", "value"),
Input("org_1", "value"),
Input("org_2", "value"),
Input("level", "value"),
Input('button', 'n_clicks')]
)
def update_tables(bom_1, bom_2, org_1, org_2, level, n_clicks):
global queried_df
if n_clicks == 0:
return dash.no_update
queried_df = bc.compare(bom_1, org_1, bom_2, org_2, level)
# perform other actions
return table
@app.server.route('/export/')
def download_excel():
strIO = io.BytesIO()
writer = pd.ExcelWriter(strIO, engine="xlsxwriter")
left = queried_df[0]
left.to_excel(writer, sheet_name='Sheet1', startrow=1)
excel_data = strIO.getvalue()
strIO.seek(0)
return send_file(strIO, attachment_filename='test.xlsx', as_attachment=True)
</code></pre>