Greetings and salutations!
I am creating a simple GUI to allow the user to display the dataframe of a local CSV file using a dropdown menu.
The dropdown menu is populated by the files with .csv extension in a generic folder.
When I first run the app, it works as expected and selecting a filename generates a datatable with all the rows/columns of that file. However, when I select a different filename from the dropdown menu, the table does not update.
Cannot find the bug in my code, so any guidance would be greatly appreciated.
import pandas as pd
import pathlib
import dash
from dash import dcc, Input, Output, html, dash_table
# ======================== Dash App
app = dash.Dash(__name__)
# ======================== Generic Test Results directory (for universal reuse)
output_folder = r"C:\TestStand Results"
# Filter file name list for files ending with .csv
fileNames = []
filePaths = []
for p in pathlib.Path(output_folder).glob("*.csv"):
if p.is_file():
fileNames.append(p.name)
filePaths.append(p)
# ======================== App Layout
title = html.H1(
"TestStand Data Log",
style={"text-align": "center", "background-color": "#ede9e8"},
)
dropdown = html.Div(
dcc.Dropdown(
id="dropdown_filename", options=[{"label": i, "value": i} for i in fileNames]
)
)
data_log = html.Div(id="data_log")
app.layout = html.Div([title, dropdown, data_log])
@app.callback(
[Output("data_log", "children")],
[Input("dropdown_filename", "value")],
)
def update_table(user_select):
# ======================== Reading Selected csv file
if user_select is not None:
filepath = ""
for path in filePaths:
if path.name == user_select:
filepath = path
ts_table = pd.read_csv(filepath)
return [dash_table.DataTable(id="data_log", data=ts_table.to_dict("records"))]
# -------------------------------------------------------------------------------------
# Run local server
if __name__ == "__main__":
app.run_server(debug=True)