-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gordon Shotwell
committed
Jan 25, 2024
1 parent
b6907f7
commit 7d4c4d3
Showing
2 changed files
with
92 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
) |