From aa5baff15166401ad6e6e097bde0acfd323e415b Mon Sep 17 00:00:00 2001 From: adejumoridwan Date: Thu, 22 Jun 2023 08:41:09 +0100 Subject: [PATCH 01/10] change input_file example --- shiny/examples/input_file/app.py | 68 +++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 18 deletions(-) diff --git a/shiny/examples/input_file/app.py b/shiny/examples/input_file/app.py index 5029893a9..e7649b9a4 100644 --- a/shiny/examples/input_file/app.py +++ b/shiny/examples/input_file/app.py @@ -1,29 +1,61 @@ import pandas as pd -from shiny import * -from shiny.types import FileInfo +from shiny import App, reactive, render, ui 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("file", "File", accept=".csv"), + ui.input_checkbox("row_count", "Row count", False), + ui.input_checkbox("column_count", "Column count", False), + ui.input_checkbox("column_names", "Column names", False), + ui.output_table("summary"), ) -def server(input: Inputs, output: Outputs, session: Session): +def server(input, output, session): + # Reading in the data into a reactive calcuation makes it easier to work with + @reactive.Calc + def parsed_file(): + file = input.file() + if file is None: + # Returning an empty dataframe is a bit simpler than the conditional UI + 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], + } + ) + + # return only selected columns based on input + inputs = [input.row_count(), input.column_count(), input.column_names()] + + index_of_false = [] + for index, element in enumerate(inputs): + if not element: + index_of_false.append(index) + + if index_of_false is not None: + return info_df.drop(info_df.columns[index_of_false], axis=1) + + return info_df app = App(app_ui, server) From ab7857f48a684ce71baba7fedf3a82b868017447 Mon Sep 17 00:00:00 2001 From: adejumoridwan Date: Fri, 23 Jun 2023 14:38:04 +0100 Subject: [PATCH 02/10] More realistic file import example #567~ --- shiny/examples/input_file/app.py | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/shiny/examples/input_file/app.py b/shiny/examples/input_file/app.py index e7649b9a4..66f4cf57f 100644 --- a/shiny/examples/input_file/app.py +++ b/shiny/examples/input_file/app.py @@ -4,20 +4,18 @@ app_ui = ui.page_fluid( ui.input_file("file", "File", accept=".csv"), - ui.input_checkbox("row_count", "Row count", False), - ui.input_checkbox("column_count", "Column count", False), - ui.input_checkbox("column_names", "Column names", False), + ui.input_checkbox_group( + "stats", "Summary Stats", choices=["Row Count", "Column Count", "Column Names"] + ), ui.output_table("summary"), ) def server(input, output, session): - # Reading in the data into a reactive calcuation makes it easier to work with @reactive.Calc def parsed_file(): file = input.file() if file is None: - # Returning an empty dataframe is a bit simpler than the conditional UI return pd.DataFrame() return pd.read_csv(file[0]["datapath"]) @@ -44,18 +42,7 @@ def summary(): } ) - # return only selected columns based on input - inputs = [input.row_count(), input.column_count(), input.column_names()] - - index_of_false = [] - for index, element in enumerate(inputs): - if not element: - index_of_false.append(index) - - if index_of_false is not None: - return info_df.drop(info_df.columns[index_of_false], axis=1) - - return info_df + return info_df.loc[:, input.stats()] app = App(app_ui, server) From 2e6fb393f058c0a094344c90fd9039e6c6da1bbc Mon Sep 17 00:00:00 2001 From: adejumoridwan Date: Thu, 20 Jul 2023 17:17:16 +0100 Subject: [PATCH 03/10] add more realistic file import example --- examples/snowflake/creds.json | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 examples/snowflake/creds.json diff --git a/examples/snowflake/creds.json b/examples/snowflake/creds.json new file mode 100644 index 000000000..ecfeae090 --- /dev/null +++ b/examples/snowflake/creds.json @@ -0,0 +1,9 @@ +{ + "account": "oucigty-de90610", + "user": "ADEJUMORIDWAN", + "password": "Taxonom1!?", + "warehouse": "COMPUTE_WH", + "database": "SNOWFLAKE_SAMPLE_DATA", + "schema": "TPCDS_SF10TCL", + "role": "ACCOUNTADMIN" +} From 5974ed00cbc8b065ab16cb672051cde038fb8be2 Mon Sep 17 00:00:00 2001 From: adejumoridwan Date: Thu, 20 Jul 2023 17:27:32 +0100 Subject: [PATCH 04/10] add more realistic file import example --- shiny/examples/input_file/app.py | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 shiny/examples/input_file/app.py diff --git a/shiny/examples/input_file/app.py b/shiny/examples/input_file/app.py new file mode 100644 index 000000000..66f4cf57f --- /dev/null +++ b/shiny/examples/input_file/app.py @@ -0,0 +1,48 @@ +import pandas as pd + +from shiny import App, reactive, render, ui + +app_ui = ui.page_fluid( + ui.input_file("file", "File", accept=".csv"), + ui.input_checkbox_group( + "stats", "Summary Stats", choices=["Row Count", "Column Count", "Column Names"] + ), + ui.output_table("summary"), +) + + +def server(input, output, session): + @reactive.Calc + def parsed_file(): + file = input.file() + 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], + } + ) + + return info_df.loc[:, input.stats()] + + +app = App(app_ui, server) From bdcb81b8a63b886c5f9818cf3719e66ccbf1ac3c Mon Sep 17 00:00:00 2001 From: adejumoridwan Date: Tue, 8 Aug 2023 16:23:01 +0100 Subject: [PATCH 05/10] improve file import example --- examples/snowflake/creds.json | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 examples/snowflake/creds.json diff --git a/examples/snowflake/creds.json b/examples/snowflake/creds.json deleted file mode 100644 index ecfeae090..000000000 --- a/examples/snowflake/creds.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "account": "oucigty-de90610", - "user": "ADEJUMORIDWAN", - "password": "Taxonom1!?", - "warehouse": "COMPUTE_WH", - "database": "SNOWFLAKE_SAMPLE_DATA", - "schema": "TPCDS_SF10TCL", - "role": "ACCOUNTADMIN" -} From 5e4af8ebe3cfc7fb5aba57cfac897cb5a936d36a Mon Sep 17 00:00:00 2001 From: Adejumo Ridwan Suleiman Date: Tue, 8 Aug 2023 20:30:45 +0100 Subject: [PATCH 06/10] Update shiny/examples/input_file/app.py Co-authored-by: Winston Chang --- shiny/examples/input_file/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shiny/examples/input_file/app.py b/shiny/examples/input_file/app.py index 66f4cf57f..0510def95 100644 --- a/shiny/examples/input_file/app.py +++ b/shiny/examples/input_file/app.py @@ -3,7 +3,7 @@ from shiny import App, reactive, render, ui app_ui = ui.page_fluid( - ui.input_file("file", "File", accept=".csv"), + ui.input_file("file1", "Upload a CSV file", accept=".csv"), ui.input_checkbox_group( "stats", "Summary Stats", choices=["Row Count", "Column Count", "Column Names"] ), From e891bfaad441f6e360668896182389d58900829f Mon Sep 17 00:00:00 2001 From: Adejumo Ridwan Suleiman Date: Tue, 8 Aug 2023 20:31:06 +0100 Subject: [PATCH 07/10] Update shiny/examples/input_file/app.py Co-authored-by: Winston Chang --- shiny/examples/input_file/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shiny/examples/input_file/app.py b/shiny/examples/input_file/app.py index 0510def95..f3f179f25 100644 --- a/shiny/examples/input_file/app.py +++ b/shiny/examples/input_file/app.py @@ -11,7 +11,7 @@ ) -def server(input, output, session): +def server(input: Inputs, output: Outputs, session: Session): @reactive.Calc def parsed_file(): file = input.file() From ecca9b3578a72d7380e7463d05ad327abaaa3bd9 Mon Sep 17 00:00:00 2001 From: Adejumo Ridwan Suleiman Date: Tue, 8 Aug 2023 20:31:50 +0100 Subject: [PATCH 08/10] Update shiny/examples/input_file/app.py Co-authored-by: Winston Chang --- shiny/examples/input_file/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shiny/examples/input_file/app.py b/shiny/examples/input_file/app.py index f3f179f25..b97509638 100644 --- a/shiny/examples/input_file/app.py +++ b/shiny/examples/input_file/app.py @@ -14,7 +14,7 @@ def server(input: Inputs, output: Outputs, session: Session): @reactive.Calc def parsed_file(): - file = input.file() + file: list[FileInfo] = input.file1() if file is None: return pd.DataFrame() return pd.read_csv(file[0]["datapath"]) From b520b232e02e844639c0a5836733a04b7ab2dea7 Mon Sep 17 00:00:00 2001 From: Adejumo Ridwan Suleiman Date: Tue, 8 Aug 2023 20:32:30 +0100 Subject: [PATCH 09/10] Update shiny/examples/input_file/app.py Co-authored-by: Winston Chang --- shiny/examples/input_file/app.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shiny/examples/input_file/app.py b/shiny/examples/input_file/app.py index b97509638..9c73d1e76 100644 --- a/shiny/examples/input_file/app.py +++ b/shiny/examples/input_file/app.py @@ -42,6 +42,8 @@ def summary(): } ) + # input.stats() is a list of strings; subset the columns based on the selected + # checkboxes return info_df.loc[:, input.stats()] From d1f37d2c71f7d6a6f190196f399c53a757e5f196 Mon Sep 17 00:00:00 2001 From: adejumoridwan Date: Tue, 8 Aug 2023 21:25:09 +0100 Subject: [PATCH 10/10] improve file input example --- shiny/api-examples/input_file/app.py | 16 +++++++++++----- shiny/examples/input_file/app.py | 5 ++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/shiny/api-examples/input_file/app.py b/shiny/api-examples/input_file/app.py index 66f4cf57f..a6134cb34 100644 --- a/shiny/api-examples/input_file/app.py +++ b/shiny/api-examples/input_file/app.py @@ -1,20 +1,24 @@ import pandas as pd -from shiny import App, reactive, render, ui +from shiny import App, Inputs, Outputs, Session, reactive, render, ui +from shiny.types import FileInfo app_ui = ui.page_fluid( - ui.input_file("file", "File", accept=".csv"), + 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"] + "stats", + "Summary Stats", + choices=["Row Count", "Column Count", "Column Names"], + selected=["Row Count", "Column Count", "Column Names"], ), ui.output_table("summary"), ) -def server(input, output, session): +def server(input: Inputs, output: Outputs, session: Session): @reactive.Calc def parsed_file(): - file = input.file() + file: list[FileInfo] = input.file1() if file is None: return pd.DataFrame() return pd.read_csv(file[0]["datapath"]) @@ -42,6 +46,8 @@ def summary(): } ) + # input.stats() is a list of strings; subset the columns based on the selected + # checkboxes return info_df.loc[:, input.stats()] diff --git a/shiny/examples/input_file/app.py b/shiny/examples/input_file/app.py index 9c73d1e76..d7bb420db 100644 --- a/shiny/examples/input_file/app.py +++ b/shiny/examples/input_file/app.py @@ -5,7 +5,10 @@ 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"] + "stats", + "Summary Stats", + choices=["Row Count", "Column Count", "Column Names"], + selected=["Row Count", "Column Count", "Column Names"], ), ui.output_table("summary"), )