diff --git a/shiny/api-examples/input_file/app.py b/shiny/api-examples/input_file/app.py index 7038bf6de..a6134cb34 100644 --- a/shiny/api-examples/input_file/app.py +++ b/shiny/api-examples/input_file/app.py @@ -1,31 +1,54 @@ import pandas as pd -from shiny import App, Inputs, Outputs, Session, render, ui +from shiny import App, Inputs, Outputs, Session, reactive, render, ui from shiny.types import FileInfo app_ui = ui.page_fluid( - ui.layout_sidebar( - ui.panel_sidebar( - ui.input_file("file1", "Choose CSV File", accept=[".csv"], multiple=False), - ui.input_checkbox("header", "Header", True), - width=5, - ), - ui.panel_main( - ui.output_ui("contents"), - ), + ui.input_file("file1", "Choose CSV File", accept=[".csv"], multiple=False), + ui.input_checkbox_group( + "stats", + "Summary Stats", + choices=["Row Count", "Column Count", "Column Names"], + selected=["Row Count", "Column Count", "Column Names"], ), + ui.output_table("summary"), ) def server(input: Inputs, output: Outputs, session: Session): + @reactive.Calc + def parsed_file(): + file: list[FileInfo] = input.file1() + if file is None: + return pd.DataFrame() + return pd.read_csv(file[0]["datapath"]) + @output - @render.ui - def contents(): - if input.file1() is None: - return "Please upload a csv file" - f: list[FileInfo] = input.file1() - df = pd.read_csv(f[0]["datapath"], header=0 if input.header() else None) - return ui.HTML(df.to_html(classes="table table-striped")) + @render.table + def summary(): + df = parsed_file() + + if df.empty: + return pd.DataFrame() + + # Get the row count, column count, and column names of the DataFrame + row_count = df.shape[0] + column_count = df.shape[1] + names = df.columns.tolist() + column_names = ", ".join(str(name) for name in names) + + # Create a new DataFrame to display the information + info_df = pd.DataFrame( + { + "Row Count": [row_count], + "Column Count": [column_count], + "Column Names": [column_names], + } + ) + + # input.stats() is a list of strings; subset the columns based on the selected + # checkboxes + return info_df.loc[:, input.stats()] app = App(app_ui, server) diff --git a/shiny/examples/input_file/app.py b/shiny/examples/input_file/app.py new file mode 100644 index 000000000..d7bb420db --- /dev/null +++ b/shiny/examples/input_file/app.py @@ -0,0 +1,53 @@ +import pandas as pd + +from shiny import App, reactive, render, ui + +app_ui = ui.page_fluid( + ui.input_file("file1", "Upload a CSV file", accept=".csv"), + ui.input_checkbox_group( + "stats", + "Summary Stats", + choices=["Row Count", "Column Count", "Column Names"], + selected=["Row Count", "Column Count", "Column Names"], + ), + ui.output_table("summary"), +) + + +def server(input: Inputs, output: Outputs, session: Session): + @reactive.Calc + def parsed_file(): + file: list[FileInfo] = input.file1() + if file is None: + return pd.DataFrame() + return pd.read_csv(file[0]["datapath"]) + + @output + @render.table + def summary(): + df = parsed_file() + + if df.empty: + return pd.DataFrame() + + # Get the row count, column count, and column names of the DataFrame + row_count = df.shape[0] + column_count = df.shape[1] + names = df.columns.tolist() + column_names = ", ".join(str(name) for name in names) + + # Create a new DataFrame to display the information + info_df = pd.DataFrame( + { + "Row Count": [row_count], + "Column Count": [column_count], + "Column Names": [column_names], + } + ) + + # input.stats() is a list of strings; subset the columns based on the selected + # checkboxes + return info_df.loc[:, input.stats()] + + +app = App(app_ui, server)