Plotly-Dash 下载 DataFrame 到 CSV 不再适用于 send_data_frame
Plotly-Dash download DataFrame to CSV no longer works with send_data_frame
以下 Python 代码片段直到最近一直在为我工作,我找不到任何更改的文档。我正在尝试使用 Plotly 的 dash_core_components
模块中的 send_data_frame
函数将 Pandas DataFrame 作为 CSV 文件写入磁盘。
import numpy as np
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
from dash_extensions import Download
from dash_extensions.snippets import send_data_frame
layout = html.Div([
...
... # Trying to be as complete, but as brief as possible with my snippet.
...
... # The main gist is that there are 2 buttons on the page, one that
... # triggers a callback to download stock data (OHLC prices), and another
... # that saves the data to disk after it has been gathered.
])
# MAIN APP CALLBACK -- This downloads the OHLC data from Yahoo Finance.
@app.callback(
Output('ohlcData', 'children'),
[
Input('button', 'n_clicks')
],[
State('ticker', 'value')
], prevent_initial_call=True)
def get_ohlc_data(n_clicks, ticker):
ohlc = getOHLC(ticker) # This works, but the method is not shown here
df = ohlc.df
# Write the OHLC DataFrame to a hidden DataFrame
hiddenDF = df.to_json(date_format='iso', orient='split')
return hiddenDF
# DOWNLOAD CSV CALLBACK -- This secondary callback finds the hiddenDiv and attempts to write it to disk as a CSV file.
@app.callback(
Output('dataFrame', 'data'),
[
Input('button2', 'n_clicks')
],[
State('ohlcData', 'children')
], prevent_initial_call=True)
def writeCSV(n_clicks, hiddenDiv):
dfOHLC = pd.read_json(hiddenDiv, orient='split')
dfOHLC.index.name = 'Date'
return send_data_frame(dfOHLC.to_csv, 'OHLC.csv')
上周当我 运行 此代码时,当我尝试“下载”CSV 文件或将 CSV 文件保存到磁盘时,我会得到一个弹出窗口 window。现在我什至没有走那么远,而是收到一条警告(必须设置 debug=True
),指出:
Failed component prop type: Invalid component prop `data` key `mime_type` supplied to Download.
Bad object: {
"content":
"[A really long string of characters]",
"filename": "OHLC.csv",
"mime_type": null,
"base64": true
}
Valid keys: [
"filename",
"content",
"base64",
"type"
]
请帮忙!
鉴于错误信息,我猜测 Output('dataFrame', 'data')
中的输出选项 data
突然是一个无效参数。
当 Download
组件从 dash-extensions
迁移到 dash-core-components
时,mime_type
属性 重命名为 type
。您的错误表明您正在使用 dash-core-components
创建 Download
组件,但使用 send_data_frame
从 dash-extensions
.
发送数据
如果您始终使用其中一个,它应该会起作用。但是,dash-core-components
组件将在未来得到支持,因此我建议使用该组件。
以下 Python 代码片段直到最近一直在为我工作,我找不到任何更改的文档。我正在尝试使用 Plotly 的 dash_core_components
模块中的 send_data_frame
函数将 Pandas DataFrame 作为 CSV 文件写入磁盘。
import numpy as np
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
from dash_extensions import Download
from dash_extensions.snippets import send_data_frame
layout = html.Div([
...
... # Trying to be as complete, but as brief as possible with my snippet.
...
... # The main gist is that there are 2 buttons on the page, one that
... # triggers a callback to download stock data (OHLC prices), and another
... # that saves the data to disk after it has been gathered.
])
# MAIN APP CALLBACK -- This downloads the OHLC data from Yahoo Finance.
@app.callback(
Output('ohlcData', 'children'),
[
Input('button', 'n_clicks')
],[
State('ticker', 'value')
], prevent_initial_call=True)
def get_ohlc_data(n_clicks, ticker):
ohlc = getOHLC(ticker) # This works, but the method is not shown here
df = ohlc.df
# Write the OHLC DataFrame to a hidden DataFrame
hiddenDF = df.to_json(date_format='iso', orient='split')
return hiddenDF
# DOWNLOAD CSV CALLBACK -- This secondary callback finds the hiddenDiv and attempts to write it to disk as a CSV file.
@app.callback(
Output('dataFrame', 'data'),
[
Input('button2', 'n_clicks')
],[
State('ohlcData', 'children')
], prevent_initial_call=True)
def writeCSV(n_clicks, hiddenDiv):
dfOHLC = pd.read_json(hiddenDiv, orient='split')
dfOHLC.index.name = 'Date'
return send_data_frame(dfOHLC.to_csv, 'OHLC.csv')
上周当我 运行 此代码时,当我尝试“下载”CSV 文件或将 CSV 文件保存到磁盘时,我会得到一个弹出窗口 window。现在我什至没有走那么远,而是收到一条警告(必须设置 debug=True
),指出:
Failed component prop type: Invalid component prop `data` key `mime_type` supplied to Download.
Bad object: {
"content":
"[A really long string of characters]",
"filename": "OHLC.csv",
"mime_type": null,
"base64": true
}
Valid keys: [
"filename",
"content",
"base64",
"type"
]
请帮忙!
鉴于错误信息,我猜测 Output('dataFrame', 'data')
中的输出选项 data
突然是一个无效参数。
当 Download
组件从 dash-extensions
迁移到 dash-core-components
时,mime_type
属性 重命名为 type
。您的错误表明您正在使用 dash-core-components
创建 Download
组件,但使用 send_data_frame
从 dash-extensions
.
如果您始终使用其中一个,它应该会起作用。但是,dash-core-components
组件将在未来得到支持,因此我建议使用该组件。