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..b5e0f7153 --- /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", + )