From fdabbbd527a62f8a553cfb9e62081889f91b572d Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 10 Jan 2024 12:25:53 -0500 Subject: [PATCH 1/2] Create `app-express.py` from `app.py` --- .../input_action_button/app-express.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 shiny/api-examples/input_action_button/app-express.py diff --git a/shiny/api-examples/input_action_button/app-express.py b/shiny/api-examples/input_action_button/app-express.py new file mode 100644 index 000000000..66f6abe13 --- /dev/null +++ b/shiny/api-examples/input_action_button/app-express.py @@ -0,0 +1,26 @@ +import matplotlib.pyplot as plt +import numpy as np + +from shiny import App, Inputs, Outputs, Session, reactive, render, ui + +app_ui = ui.page_fluid( + ui.input_slider("n", "Number of observations", min=0, max=1000, value=500), + ui.input_action_button("go", "Go!", class_="btn-success"), + ui.output_plot("plot"), +) + + +def server(input: Inputs, output: Outputs, session: Session): + @render.plot(alt="A histogram") + # Use reactive.event() to invalidate the plot only when the button is pressed + # (not when the slider is changed) + @reactive.event(input.go, ignore_none=False) + def plot(): + np.random.seed(19680801) + x = 100 + 15 * np.random.randn(input.n()) + fig, ax = plt.subplots() + ax.hist(x, bins=30, density=True) + return fig + + +app = App(app_ui, server) From 64c8eae57bd54673c778911dd09c4263df277fab Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 10 Jan 2024 12:23:21 -0500 Subject: [PATCH 2/2] Rewrite `app-express.py` using Shiny Express --- .../input_action_button/app-express.py | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/shiny/api-examples/input_action_button/app-express.py b/shiny/api-examples/input_action_button/app-express.py index 66f6abe13..925ebc4d6 100644 --- a/shiny/api-examples/input_action_button/app-express.py +++ b/shiny/api-examples/input_action_button/app-express.py @@ -1,26 +1,20 @@ import matplotlib.pyplot as plt import numpy as np -from shiny import App, Inputs, Outputs, Session, reactive, render, ui +from shiny import reactive, render +from shiny.express import input, ui -app_ui = ui.page_fluid( - ui.input_slider("n", "Number of observations", min=0, max=1000, value=500), - ui.input_action_button("go", "Go!", class_="btn-success"), - ui.output_plot("plot"), -) +ui.input_slider("n", "Number of observations", min=0, max=1000, value=500) +ui.input_action_button("go", "Go!", class_="btn-success") -def server(input: Inputs, output: Outputs, session: Session): - @render.plot(alt="A histogram") - # Use reactive.event() to invalidate the plot only when the button is pressed - # (not when the slider is changed) - @reactive.event(input.go, ignore_none=False) - def plot(): - np.random.seed(19680801) - x = 100 + 15 * np.random.randn(input.n()) - fig, ax = plt.subplots() - ax.hist(x, bins=30, density=True) - return fig - - -app = App(app_ui, server) +@render.plot(alt="A histogram") +# Use reactive.event() to invalidate the plot only when the button is pressed +# (not when the slider is changed) +@reactive.event(input.go, ignore_none=False) +def plot(): + np.random.seed(19680801) + x = 100 + 15 * np.random.randn(input.n()) + fig, ax = plt.subplots() + ax.hist(x, bins=30, density=True) + return fig