Py.Cafe

KittyNyan/

shiny-interactive-example

Interactive Shiny App

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# check out https://shiny.posit.co/py/ for documentation
# And check out https://py.cafe/shiny-unofficial for more examples
from shiny import App, render, ui, reactive

app_ui = ui.page_fluid(
    ui.h2("Hello Shiny!"),
    ui.input_slider("n", "N", 0, 100, 20),
    ui.output_text_verbatim("txt"),
    ui.input_action_button("set", "Set slider to 42"),
    ui.input_action_button("reset", "Reset slider"),
)


def server(input, output, session):
    @output
    @render.text
    def txt():
        return f"n*2 is {input.n() * 2}"

    @reactive.Effect
    @reactive.event(input.set)
    def _():
        ui.update_slider(id="n", value=42)

    @reactive.Effect
    @reactive.event(input.reset)
    def _():
        ui.update_slider(id="n", value=20)


app = App(app_ui, server)