从 Dash Datatable 下载的文件正在导出 HTML 而不是数据框
Downloaded file from Dash Datatable is exporting HTML rather than dataframe
我是 Dash 的新手,一直在努力寻找我在网上找到的所有帮助。请在这里提供任何建议!
我有一个数据 table,我想通过屏幕上的按钮提供下载。该代码将下载一个文件,但该文件包含 HTML。我阅读了 this,但不确定如何将 @app.server.route
添加到我的代码中。当我读到它时,服务器路由似乎是用于大文件的?我只有几百条记录。
这是我的代码:
import dash
import dash_table
import pandas as pd
#import data
df = pd.read_excel(r'C:\Users\abc\Documents\File.xlsm', sheet_name = "Data")
#clean up the data
new_header = df.iloc[0] #grab the first row for the header
df = df[1:] #take the data less the header row
df.columns = new_header #set the header row as the df header
app = dash.Dash(__name__)
app.layout = html.Div([
html.H3("Data"),
#horizontal rule
html.Hr(),
#download button
html.A(
html.Button('Download to Excel'),
id='excel-download',
download="data.csv",
href='',
target="_blank"
),
#add a space
html.P(),
#data table
dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict('records'),
#column headers fixed
#fixed_rows={'headers': True},
style_cell={
'minWidth': 95, 'maxWidth': 95, 'width': 95
},
#editable formatting
editable=True,
filter_action="native",
sort_action="native",
sort_mode="multi",
column_selectable="multi",
#row_selectable="multi",
#row_deletable=True,
selected_columns=[],
selected_rows=[],
page_action="native",
page_current= 0#,
#page_size= 10,
),
])
#style callback
@app.callback(
Output('datatable-interactivity', 'style_data_conditional'),
[Input('datatable-interactivity', 'selected_columns')]
)
#download callback https://community.plotly.com/t/download-raw-data/4700/7
@app.callback(
dash.dependencies.Output('excel-download', 'href'),
[dash.dependencies.Input('table', 'value')])
def update_download_link(filter_value):
dff = filter_data(filter_value)
csv_string = dff.to_csv(index=False, encoding='utf-8')
csv_string = "data:text/csv;charset=utf-8," + urllib.quote(csv_string)
return csv_string
def update_styles(selected_columns):
return [{
'if': { 'column_id': i },
'background_color': '#D2F3FF'
} for i in selected_columns]
if __name__ == '__main__':
app.run_server(debug=False, port=5554)
我不明白的一件事是 [dash.dependencies.Input('table', 'value')])
行如何用于 'value'
部分。我的代码在我找到的示例中的任何地方都没有价值。我也尝试在那里使用 'data' 来对应 DataTable() 中的数据引用,但结果是一样的。我得到一个带有 HTML 输出而不是 df.
的 excel 文件
看来你不见了export_format
dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict('records'),
#column headers fixed
#fixed_rows={'headers': True},
style_cell={
'minWidth': 95, 'maxWidth': 95, 'width': 95
},
#editable formatting
editable=True,
filter_action="native",
export_format="csv", # THIS LINE!
sort_action="native",
sort_mode="multi",
column_selectable="multi",
#row_selectable="multi",
#row_deletable=True,
selected_columns=[],
selected_rows=[],
page_action="native",
page_current= 0#,
#page_size= 10,
),
我是 Dash 的新手,一直在努力寻找我在网上找到的所有帮助。请在这里提供任何建议!
我有一个数据 table,我想通过屏幕上的按钮提供下载。该代码将下载一个文件,但该文件包含 HTML。我阅读了 this,但不确定如何将 @app.server.route
添加到我的代码中。当我读到它时,服务器路由似乎是用于大文件的?我只有几百条记录。
这是我的代码:
import dash
import dash_table
import pandas as pd
#import data
df = pd.read_excel(r'C:\Users\abc\Documents\File.xlsm', sheet_name = "Data")
#clean up the data
new_header = df.iloc[0] #grab the first row for the header
df = df[1:] #take the data less the header row
df.columns = new_header #set the header row as the df header
app = dash.Dash(__name__)
app.layout = html.Div([
html.H3("Data"),
#horizontal rule
html.Hr(),
#download button
html.A(
html.Button('Download to Excel'),
id='excel-download',
download="data.csv",
href='',
target="_blank"
),
#add a space
html.P(),
#data table
dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict('records'),
#column headers fixed
#fixed_rows={'headers': True},
style_cell={
'minWidth': 95, 'maxWidth': 95, 'width': 95
},
#editable formatting
editable=True,
filter_action="native",
sort_action="native",
sort_mode="multi",
column_selectable="multi",
#row_selectable="multi",
#row_deletable=True,
selected_columns=[],
selected_rows=[],
page_action="native",
page_current= 0#,
#page_size= 10,
),
])
#style callback
@app.callback(
Output('datatable-interactivity', 'style_data_conditional'),
[Input('datatable-interactivity', 'selected_columns')]
)
#download callback https://community.plotly.com/t/download-raw-data/4700/7
@app.callback(
dash.dependencies.Output('excel-download', 'href'),
[dash.dependencies.Input('table', 'value')])
def update_download_link(filter_value):
dff = filter_data(filter_value)
csv_string = dff.to_csv(index=False, encoding='utf-8')
csv_string = "data:text/csv;charset=utf-8," + urllib.quote(csv_string)
return csv_string
def update_styles(selected_columns):
return [{
'if': { 'column_id': i },
'background_color': '#D2F3FF'
} for i in selected_columns]
if __name__ == '__main__':
app.run_server(debug=False, port=5554)
我不明白的一件事是 [dash.dependencies.Input('table', 'value')])
行如何用于 'value'
部分。我的代码在我找到的示例中的任何地方都没有价值。我也尝试在那里使用 'data' 来对应 DataTable() 中的数据引用,但结果是一样的。我得到一个带有 HTML 输出而不是 df.
看来你不见了export_format
dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict('records'),
#column headers fixed
#fixed_rows={'headers': True},
style_cell={
'minWidth': 95, 'maxWidth': 95, 'width': 95
},
#editable formatting
editable=True,
filter_action="native",
export_format="csv", # THIS LINE!
sort_action="native",
sort_mode="multi",
column_selectable="multi",
#row_selectable="multi",
#row_deletable=True,
selected_columns=[],
selected_rows=[],
page_action="native",
page_current= 0#,
#page_size= 10,
),