Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamlit comp updates #42

Merged
merged 8 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

/_inv/
/objects.json

.venv/
__pycache__/
37 changes: 37 additions & 0 deletions docs/apps/comp-streamlit/penguins/app-core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from pathlib import Path

import pandas as pd
from palmerpenguins import load_penguins
from plots import dist_plot, scatter_plot
from shiny import App, reactive, render, ui

app_ui = ui.page_sidebar(
ui.sidebar(
ui.input_slider("mass", "Mass", 2000, 8000, 6000),
ui.input_checkbox("smoother", "Add Smoother"),
),
ui.card(ui.output_plot(id="scatter")),
ui.card(ui.output_plot(id="mass_distribution")),
)


def server(input, output, session):
df = load_penguins()
print(df)

@reactive.Calc
def filtered_data():
filt_df = df.copy()
filt_df = filt_df.loc[df["body_mass_g"] < input.mass()]
return filt_df

@render.plot
def mass_distribution():
return dist_plot(filtered_data())

@render.plot
def scatter():
return scatter_plot(filtered_data(), input.smoother())


app = App(app_ui, server)
34 changes: 34 additions & 0 deletions docs/apps/comp-streamlit/penguins/app-express.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from pathlib import Path

import pandas as pd
from palmerpenguins import load_penguins
from plots import dist_plot, scatter_plot
from shiny import reactive, render, ui
from shiny.express import input, ui

df = load_penguins()

with ui.sidebar():
ui.input_slider("mass", "Mass", 2000, 8000, 6000)
ui.input_checkbox("smoother", "Add Smoother")


@reactive.Calc
def filtered_data():
filt_df = df.copy()
filt_df = filt_df.loc[df["body_mass_g"] < input.mass()]
return filt_df


with ui.card():

@render.plot
def scatter():
return scatter_plot(filtered_data(), input.smoother())


with ui.card():

@render.plot
def mass_distribution():
return dist_plot(filtered_data())
31 changes: 31 additions & 0 deletions docs/apps/comp-streamlit/penguins/plots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from plotnine import aes, geom_density, geom_point, ggplot, stat_smooth, theme_light


def dist_plot(df):
plot = (
ggplot(df, aes(x="body_mass_g", fill="species"))
+ geom_density(alpha=0.2)
+ theme_light()
)
return plot


def scatter_plot(df, smoother):
plot = (
ggplot(
df,
aes(
x="bill_length_mm",
y="bill_depth_mm",
color="species",
group="species",
),
)
+ geom_point()
+ theme_light()
)

if smoother:
plot = plot + stat_smooth()

return plot
4 changes: 4 additions & 0 deletions docs/apps/comp-streamlit/penguins/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shiny
palmerpenguins
plotnine
pandas
39 changes: 39 additions & 0 deletions docs/apps/comp-streamlit/slider-update/app-core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from pathlib import Path

import pandas as pd
from palmerpenguins import load_penguins
from plots import dist_plot, scatter_plot
from shiny import App, reactive, render, ui

app_ui = ui.page_sidebar(
ui.sidebar(
ui.input_slider("mass", "Mass", 2000, 8000, 6000),
ui.input_checkbox("smoother", "Add Smoother"),
ui.input_action_button("reset", "Reset Slider"),
),
ui.card(ui.output_plot(id="scatter")),
ui.card(ui.output_plot(id="mass_distribution")),
)


def server(input, output, session):
df = load_penguins()

@reactive.calc
def filtered_data():
filt_df = df.copy()
filt_df = filt_df.loc[df["body_mass_g"] < input.mass()]
return filt_df

@output
@render.plot
def mass_distribution():
return dist_plot(filtered_data())

@output
@render.plot
def scatter():
return scatter_plot(filtered_data(), input.smoother())


app = App(app_ui, server)
40 changes: 40 additions & 0 deletions docs/apps/comp-streamlit/slider-update/app-express.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from pathlib import Path

import pandas as pd
from palmerpenguins import load_penguins
from plots import dist_plot, scatter_plot
from shiny import reactive, render
from shiny.express import input, ui

df = load_penguins()

with ui.sidebar():
ui.input_slider("mass", "Mass", 2000, 8000, 6000)
ui.input_checkbox("smoother", "Add Smoother")
ui.input_action_button("reset", "Reset Slider")


@reactive.effect
@reactive.event(input.reset)
def _():
ui.update_slider("mass", value=6000)


@reactive.calc
def filtered_data():
filt_df = df.copy()
filt_df = filt_df.loc[df["body_mass_g"] < input.mass()]
return filt_df


with ui.card():

@render.plot
def scatter():
return scatter_plot(filtered_data(), input.smoother())


with ui.card():
@render.plot
def mass_distribution():
return dist_plot(filtered_data())
31 changes: 31 additions & 0 deletions docs/apps/comp-streamlit/slider-update/plots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from plotnine import aes, geom_density, geom_point, ggplot, stat_smooth, theme_light


def dist_plot(df):
plot = (
ggplot(df, aes(x="body_mass_g", fill="species"))
+ geom_density(alpha=0.2)
+ theme_light()
)
return plot


def scatter_plot(df, smoother):
plot = (
ggplot(
df,
aes(
x="bill_length_mm",
y="bill_depth_mm",
color="species",
group="species",
),
)
+ geom_point()
+ theme_light()
)

if smoother:
plot = plot + stat_smooth()

return plot
4 changes: 4 additions & 0 deletions docs/apps/comp-streamlit/slider-update/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shiny
palmerpenguins
plotnine
pandas
Loading