I am in the process of learning Dash with the final goal being the development of an app that allows quick analysis of data sets in an excel file. I’d like a drop down menu that will allow for switching between the sheets in the excel file. I’m having trouble getting this to run properly. I can make it output to a graph but not a table. My code is as follows:
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import dash_table_experiments as dt
import pandas as pd
app = dash.Dash()
df = pd.read_excel('output.xlsx', sheet_name=None)
app.layout = html.Div(
html.Div([dcc.Dropdown(id='drop_value',
options=[{'label': i, 'value': i} for i in df],
value='Sheet1')]),
html.Div([dt.DataTable(rows=[{}],
id='table')])
)
@app.callback(Output('table', 'rows'), [Input('drop_value', 'value')])
def update_info_table(drop_value):
if drop_value == 'Sheet1':
new_data = df['Sheet1'].to_dict()
return new_data
elif drop_value == 'Sheet2':
new_data = df['Sheet2'].to_dict()
return new_data
else:
new_data = df['Sheet3'].to_dict()
return new_data
if __name__ == '__main__':
app.run_server()
When I run this code I get the following error:
TypeError: init() takes from 1 to 2 positional arguments but 3 were given
I’m assuming this has something to do with the format of the dataset I’m trying to feed to the datatable. Right now I’m just using a dummy excel file with only two columns labeled ‘x1’ and ‘y1’.
Adjusted code and seems to work now. Correct code is below for anyone who needs.
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import dash_table_experiments as dt
# Load in data set
sheet_to_df_map = pd.ExcelFile('output.xlsx')
dropdown_options = pd.read_excel('output.xlsx', sheet_name=None)
# Create the dash app
app = dash.Dash()
# Define the layout for the drop down menu
app.layout = html.Div([
html.H2("Select Sheet Number"),
html.Div([dcc.Dropdown(id="field_dropdown", options=[{
'label': i,
'value': i
} for i in dropdown_options],
value='Sheet3')],
style={'width': '25%',
'display': 'inline-block'}),
dt.DataTable(rows=[{}],
row_selectable=True,
filterable=True,
sortable=True,
selected_row_indices=[],
id='datatable')
])
@app.callback(
dash.dependencies.Output('datatable', 'rows'),
[dash.dependencies.Input('field_dropdown', 'value')])
def update_datatable(user_selection):
if user_selection == 'Sheet1':
return sheet_to_df_map.parse(0).to_dict('records')
elif user_selection == 'Sheet2':
return sheet_to_df_map.parse(1).to_dict('records')
else:
return sheet_to_df_map.parse(2).to_dict('records')
if __name__ == '__main__':
app.run_server()code here
Tags: excel, file, pythonpython, select