From 04b9f041365ca7933d98dd16ab613e263b2bae1f Mon Sep 17 00:00:00 2001 From: Gordon Shotwell Date: Wed, 24 Jan 2024 09:47:34 -0400 Subject: [PATCH 1/6] Update dashboard template Remove rows from core template, add express template --- .../app-templates/dashboard/app-core.py | 24 +++--- .../app-templates/dashboard/app-express.py | 82 +++++++++++++++++++ 2 files changed, 92 insertions(+), 14 deletions(-) create mode 100644 shiny/templates/app-templates/dashboard/app-express.py diff --git a/shiny/templates/app-templates/dashboard/app-core.py b/shiny/templates/app-templates/dashboard/app-core.py index 3e83227e0..341dda69d 100644 --- a/shiny/templates/app-templates/dashboard/app-core.py +++ b/shiny/templates/app-templates/dashboard/app-core.py @@ -29,21 +29,17 @@ def make_value_box(penguin): "species", "Filter by species", species, selected=species ), ), - ui.row( - ui.layout_columns( - *[make_value_box(penguin) for penguin in species], - ) + ui.layout_columns( + *[make_value_box(penguin) for penguin in species], ), - ui.row( - ui.layout_columns( - ui.card( - ui.card_header("Summary statistics"), - ui.output_data_frame("summary_statistics"), - ), - ui.card( - ui.card_header("Penguin bills"), - ui.output_plot("length_depth"), - ), + ui.layout_columns( + ui.card( + ui.card_header("Summary statistics"), + ui.output_data_frame("summary_statistics"), + ), + ui.card( + ui.card_header("Penguin bills"), + ui.output_plot("length_depth"), ), ), ) diff --git a/shiny/templates/app-templates/dashboard/app-express.py b/shiny/templates/app-templates/dashboard/app-express.py new file mode 100644 index 000000000..ab9635ea6 --- /dev/null +++ b/shiny/templates/app-templates/dashboard/app-express.py @@ -0,0 +1,82 @@ +from pathlib import Path + +import pandas as pd +import seaborn as sns + +from shiny import reactive +from shiny.express import input, render, ui + +sns.set_theme(style="white") +df = pd.read_csv(Path(__file__).parent / "penguins.csv", na_values="NA") +species = ["Adelie", "Gentoo", "Chinstrap"] + +ui.page_opts(fillable=True) + + +def count_species(df, species): + return df[df["Species"] == species].shape[0] + + +with ui.sidebar(): + ui.input_slider("mass", "Mass", 2000, 6000, 3400) + ui.input_checkbox_group("species", "Filter by species", species, selected=species), + + +@reactive.Calc +def filtered_df() -> pd.DataFrame: + filt_df = df[df["Species"].isin(input.species())] + filt_df = filt_df.loc[filt_df["Body Mass (g)"] > input.mass()] + return filt_df + + +with ui.layout_columns(): + with ui.value_box(theme="primary"): + "Adelie", + + @render.text + def adelie_count(): + return count_species(filtered_df(), "Adelie") + + with ui.value_box(theme="primary"): + "Gentoo", + + @render.text + def gentoo_count(): + return count_species(filtered_df(), "Gentoo") + + with ui.value_box(theme="primary"): + "Chinstrap", + + @render.text + def chinstrap_count(): + return count_species(filtered_df(), "Chinstrap") + + +with ui.layout_columns(): + with ui.card(): + ui.card_header("Summary statistics") + + @render.data_frame + def summary_statistics(): + display_df = filtered_df()[ + [ + "Species", + "Island", + "Bill Length (mm)", + "Bill Depth (mm)", + "Body Mass (g)", + ] + ] + return render.DataGrid(display_df, filters=True) + + with ui.card(): + ui.card_header("Penguin bills") + + @render.plot + def length_depth(): + return sns.scatterplot( + data=filtered_df(), + x="Bill Length (mm)", + y="Bill Depth (mm)", + hue="Species", + ) From 377bace7c874086fd450320ade3a1986285a591f Mon Sep 17 00:00:00 2001 From: Gordon Shotwell Date: Wed, 24 Jan 2024 11:25:33 -0400 Subject: [PATCH 2/6] Linting --- shiny/templates/app-templates/dashboard/app-express.py | 1 - 1 file changed, 1 deletion(-) diff --git a/shiny/templates/app-templates/dashboard/app-express.py b/shiny/templates/app-templates/dashboard/app-express.py index ab9635ea6..4e1034360 100644 --- a/shiny/templates/app-templates/dashboard/app-express.py +++ b/shiny/templates/app-templates/dashboard/app-express.py @@ -8,7 +8,6 @@ sns.set_theme(style="white") df = pd.read_csv(Path(__file__).parent / "penguins.csv", na_values="NA") -species = ["Adelie", "Gentoo", "Chinstrap"] ui.page_opts(fillable=True) From 6eb84acfd92c649c5f7e96852fea77e22a2c1c95 Mon Sep 17 00:00:00 2001 From: Gordon Shotwell Date: Wed, 24 Jan 2024 11:44:40 -0400 Subject: [PATCH 3/6] Revert "Linting" This reverts commit 377bace7c874086fd450320ade3a1986285a591f. --- shiny/templates/app-templates/dashboard/app-express.py | 1 + 1 file changed, 1 insertion(+) diff --git a/shiny/templates/app-templates/dashboard/app-express.py b/shiny/templates/app-templates/dashboard/app-express.py index 4e1034360..ab9635ea6 100644 --- a/shiny/templates/app-templates/dashboard/app-express.py +++ b/shiny/templates/app-templates/dashboard/app-express.py @@ -8,6 +8,7 @@ sns.set_theme(style="white") df = pd.read_csv(Path(__file__).parent / "penguins.csv", na_values="NA") +species = ["Adelie", "Gentoo", "Chinstrap"] ui.page_opts(fillable=True) From e5d5feef9f6ab3c8100d196cdddd7ab7ae58c343 Mon Sep 17 00:00:00 2001 From: Gordon Shotwell Date: Wed, 24 Jan 2024 11:49:11 -0400 Subject: [PATCH 4/6] Ignore useless tuple warnings in express files --- setup.cfg | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/setup.cfg b/setup.cfg index 46d7999dc..1fc2f7f73 100644 --- a/setup.cfg +++ b/setup.cfg @@ -126,6 +126,10 @@ console_scripts = # E203: whitespace before ':' (see https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html#flake8) ignore = E302, E501, F403, F405, W503, E203 extend_exclude = docs, .venv, venv, typings, build, _dev +per-file-ignores = + # B018 Found useless Tuple expression. This is triggered by titles in express context + # managers. + *app-express.py: B018 [isort] profile=black From 7d1bb38cf2681b353ce07b079066f95c794b8d72 Mon Sep 17 00:00:00 2001 From: Gordon Shotwell Date: Wed, 24 Jan 2024 11:53:12 -0400 Subject: [PATCH 5/6] Listen to CI and remove useless tuple --- setup.cfg | 4 ---- shiny/templates/app-templates/dashboard/app-express.py | 6 +++--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/setup.cfg b/setup.cfg index 1fc2f7f73..46d7999dc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -126,10 +126,6 @@ console_scripts = # E203: whitespace before ':' (see https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html#flake8) ignore = E302, E501, F403, F405, W503, E203 extend_exclude = docs, .venv, venv, typings, build, _dev -per-file-ignores = - # B018 Found useless Tuple expression. This is triggered by titles in express context - # managers. - *app-express.py: B018 [isort] profile=black diff --git a/shiny/templates/app-templates/dashboard/app-express.py b/shiny/templates/app-templates/dashboard/app-express.py index ab9635ea6..fde58a979 100644 --- a/shiny/templates/app-templates/dashboard/app-express.py +++ b/shiny/templates/app-templates/dashboard/app-express.py @@ -31,21 +31,21 @@ def filtered_df() -> pd.DataFrame: with ui.layout_columns(): with ui.value_box(theme="primary"): - "Adelie", + "Adelie" @render.text def adelie_count(): return count_species(filtered_df(), "Adelie") with ui.value_box(theme="primary"): - "Gentoo", + "Gentoo" @render.text def gentoo_count(): return count_species(filtered_df(), "Gentoo") with ui.value_box(theme="primary"): - "Chinstrap", + "Chinstrap" @render.text def chinstrap_count(): From 23b829e21c3490e737aeeb29910d5118bad60158 Mon Sep 17 00:00:00 2001 From: Gordon Shotwell Date: Wed, 24 Jan 2024 11:58:58 -0400 Subject: [PATCH 6/6] Useless tuple --- shiny/templates/app-templates/dashboard/app-express.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shiny/templates/app-templates/dashboard/app-express.py b/shiny/templates/app-templates/dashboard/app-express.py index fde58a979..b5e0f7153 100644 --- a/shiny/templates/app-templates/dashboard/app-express.py +++ b/shiny/templates/app-templates/dashboard/app-express.py @@ -19,7 +19,7 @@ def count_species(df, species): with ui.sidebar(): ui.input_slider("mass", "Mass", 2000, 6000, 3400) - ui.input_checkbox_group("species", "Filter by species", species, selected=species), + ui.input_checkbox_group("species", "Filter by species", species, selected=species) @reactive.Calc