Hello Dash community,
I have a dropdown contains excel/csv files’ names, users can be able to select file name then columns’ name in that file will be displayed as a checklist. The goal is to give unchecked(empty) checklist when selecting dropdown every time instead of reserving the previous state?
For example,
Select CSV 0.csv then get unchecked checklist.
Select CSV 1.csv then get unchecked checklist.
…
Please see below picture.
import dash
import pandas as pd
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
from os import listdir
from os.path import isfile, join
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
# path
my_path = "C:/Users/ZL12754/Desktop/Parametrized keyword search/"
# only get csv and xlsx files
only_files = [f for f in listdir(my_path) if
(isfile(join(my_path, f)) and f.split(sep=".")[-1] in ["csv", "xlsx"])]
# filenames
file_names_options = [{"label": item, "value": item} for item in only_files]
app = dash.Dash(__name__,
external_stylesheets=external_stylesheets
)
app.layout = html.Div(children=[
# Header
dbc.Row(children=[
html.H2(children=["Parametrized keyword search"],
style={"text-align": "center"})
]),
# Dropdown for file selections
dbc.Row(children=[
dbc.Row(children=[dbc.Badge("Select File Name:", color="info", className="mr-1")],
style={'font-weight': 'bold'}),
dbc.Row(children=[dcc.Dropdown(id="filenames_dropdown",
options=file_names_options,
clearable=False,
placeholder="Select your File...",
multi=False)])],
style={'width': '100%', 'padding': '5px 5px', 'display': 'inline-block'}),
# Checklist
dbc.Row(children=[
dbc.Row(children=[
dbc.Badge("Select Columns:", color="info", className="mr-1")],
style={'font-weight': 'bold'}),
dbc.Row(children=[
dcc.Checklist(
id="checklist",
labelStyle={'display': 'inline-block'}
)
])
],
style={'width': '100%', 'padding': '5px 5px', 'display': 'inline-block'}
),
# textarea
dbc.Row(children=[
dbc.Row(children=[dbc.Badge("Paste Parameters:", color="info", className="mr-1")],
style={'font-weight': 'bold'}),
dbc.Row(children=[dcc.Textarea(
id='textarea',
# value='Enter your Parameters that you want to search...',
placeholder='Enter your Parameters that you want to search...',
style={'width': '99%', 'height': 100, "text-align": "left"}),
]),
dbc.Row(children=[
html.Button('Confirm', id='button', n_clicks=0),
])
],
style={'width': '100%', 'padding': '5px 5px', 'display': 'inline-block'}
),
# children
dbc.Row(children=[
html.Div(id='output_children')
],
style={'width': '100%', 'padding': '5px 5px', 'display': 'inline-block'}),
])
# Updating columns check list
@app.callback(
[Output(component_id='checklist', component_property='options')],
[Input(component_id='filenames_dropdown', component_property='value')],
prevent_initial_call=True
)
def update_columns_checklist(filenames_dropdown):
if filenames_dropdown.split(sep=".")[-1] == "csv":
dff = pd.read_csv(my_path + filenames_dropdown, encoding="utf-8")
elif filenames_dropdown.split(sep=".")[-1] == "xlsx":
dff = pd.read_excel(my_path + filenames_dropdown, engine='openpyxl')
else:
dff = None
print("Error. Not CSV or Excel")
checklist_options = [{"label": item, "value": item} for item in dff.columns.to_list()]
return [checklist_options]
if __name__ == '__main__':
app.run_server(debug=False, port=7775)
Much appreciated!