Skip to content

Commit 51b0a13

Browse files
committed
minor tweaks to shiny tutorial
1 parent 96ad329 commit 51b0a13

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

docs/dashboards/interactivity/shiny-python/_shiny-requirements.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
::: {.callout-note}
33
### Shiny Prerequisites
44

5-
In order to use Shiny within Quarto documents you will need the latest version of the `shiny` (>=0.6) and `shinywidgets` (>=0.2.2). You can install the latest version of these with:
5+
In order to use Shiny within Quarto documents you will need the latest version of the `shiny` (>=0.6) and `shinywidgets` (>=0.2.2) packages. You can install the latest version of these with:
66

77
```{.bash}
88
pip install --upgrade shiny shinywidgets

docs/dashboards/interactivity/shiny-python/execution.qmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ To learn more about Shiny for Python interactive documents see the following art
6363

6464
- [Getting Started](index.qmd) explains the basics of Shiny interactive documents.
6565

66+
- [Component Browser](https://jcheng.shinyapps.io/shiny-component-browser/#outputs) enumerates the available Shiny inputs and outputs, along with code snippets you can copy and paste into your dashboard.
67+
6668
- [Running Dashboards](running.qmd) covers how to run interactive dashboards both within VS Code and at the command line, as well as how to deploy them to end users.
6769

6870
- [Component Layout](/docs/interactive/layout.qmd) enumerates the various techniques you can use to layout interactive components within your documents.

docs/dashboards/interactivity/shiny-python/index.qmd

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,17 @@ The first thing to do is add `server: shiny` to the front matter. This tells Qua
3636

3737
Next, we use functions matching the pattern `ui.input_xxx` to create input controls. For example, `ui.input_select()` creates a select box, `ui.input_slider()` creates a slider, and so on. The values returned by these functions are then rendered into HTML and JavaScript by Quarto.
3838

39-
This example only uses two types of inputs, but Shiny has many more. Use the [Shiny Component Browser](https://jcheng.shinyapps.io/shiny-component-browser/) to see them all, along with code snippets you can copy and paste into your dashboard.
39+
This example only uses two types of inputs, but Shiny has many more. Use the [Shiny Component Browser](https://jcheng.shinyapps.io/shiny-component-browser/) to see them all, along with code snippets you can copy and paste into your dashboard.
40+
41+
The example above defined an input with the following code:
42+
43+
44+
```` {.python .pymd}
45+
```{{python}}
46+
ui.input_select("x", label="Variable:",
47+
choices=["bill_length_mm", "bill_depth_mm"])
48+
```
49+
````
4050

4151
**Every input function takes an input ID as its first argument.** An input ID is string that uniquely identifies this input; it must be a simple, syntactically valid Python variable name. We will use this ID to access the input's value from other parts of the dashboard.
4252

@@ -46,15 +56,22 @@ Make sure each input ID in your Shiny dashboard is unique. If you use the same I
4656

4757
The second argument for each input function is usually a human-readable string that will be displayed next to the input control. For example, the `ui.input_select()` function passes `"Variable:"`as the second argument, which is why the select box has the label "Variable:" next to it.
4858

49-
```` {.python .pymd}
59+
#### Sidebars
60+
61+
In many dashboards, it's desirable to visually gather all of your input controls into a sidebar. You can do this by adding the `.sidebar` class to a level 2 header, as we did in the example:
62+
63+
````{.python .pymd}
64+
## {.sidebar}
65+
5066
```{{python}}
51-
ui.input_select("x", label="Variable:",
67+
from shiny import render, reactive, ui
68+
3ui.input_select("x", "Variable:",
5269
choices=["bill_length_mm", "bill_depth_mm"])
70+
ui.input_select("dist", "Distribution:", choices=["hist", "kde"])
71+
ui.input_checkbox("rug", "Show rug marks", value = False)
5372
```
5473
````
5574

56-
In many dashboards, it's desirable to visually gather all of your input controls into a sidebar. You can do this by adding the `.sidebar` class to a level 2 header, as in the `## {.sidebar}` line above.
57-
5875
### Displaying Dynamic Output
5976

6077
In Shiny, dashboards can contain outputs---plots, tables, text, etc.---that dynamically update in response to user input.
@@ -73,7 +90,7 @@ def displot():
7390

7491
The function here is given the name `displot`. The body of the function is using typical Seaborn code to create the plot. And a `@render.plot` decorator is added to the function, to indicate to Shiny that this function should be used to create a plot. (If you haven't seen decorators before, they're a Python feature that allows you to add additional behavior to a function.)
7592

76-
The `input.x()` and `input.rug()` method calls are retrieving the values of the `x` and `rug` inputs created earlier in the dashboard.
93+
The `input.x()`, `input.rug()`. and `input.dist()` method calls are retrieving the values of the `x`, `rug`, and `dist` inputs created earlier in the dashboard.
7794

7895
Note that our code never calls the `displot()` function! Just the act of defining the function, and decorating it with `@render.plot`, is enough to tell Shiny and Quarto to:
7996

@@ -105,17 +122,34 @@ Here is the source code for this dashboard. You can click on the numbers on the
105122

106123
{{< include _shiny-advanced.md >}}
107124

108-
### Setup Chunks
125+
### Setup Cell
126+
127+
In static Quarto documents, `{python}` code cells run only when the document is rendered, not when it is viewed. In `server: shiny` documents, `{python}` code cells are run both during render time _and_ each time the dashboard is loaded in a browser. This is important because each visitor to the dashboard needs their own independent copies of inputs/outputs in memory, so that simultaneous users don't interfere with each other.
109128

110-
In static Quarto documents, `{python}` code chunks run only when the document is rendered, not when it is viewed. In `server: shiny` documents, `{python}` code chunks are run both during render time _and_ each time the dashboard is loaded in a browser. This is important because each visitor to the dashboard needs their own independent copies of inputs/outputs in memory, so that simultaneous users don't interfere with each other.
129+
However, sometimes we have code that would be excessive to run for every user, and we only want the code to run once, when the document's Shiny runtime process is starting up. For example, in the example above, we import packages and load data using `sns.load_dataset("penguins")`:
130+
131+
````{.python .pymd}
132+
```{{python}}
133+
#| context: setup
134+
import seaborn as sns
135+
from shiny import render, reactive, ui
136+
penguins = sns.load_dataset("penguins")
137+
```
138+
````
111139

112-
However, sometimes we have code that would be excessive to run for every user, and we only want the code to run once, when the document's Shiny runtime process is starting up. For example, in the example above, we load data using `sns.load_dataset("penguins")`; it would be wasteful in terms of both time and memory to load the data once for each user, instead of once for the process.
140+
We do this in a setup cell because it would be wasteful in terms of both time and memory to load the data once for each user, instead of once for the process.
113141

114-
By simply adding `#| context: setup` to the code chunk, we can tell Quarto to run the code only once, when the Shiny process starts up. This is called a **setup chunk**. Setup chunks are a great way to factor out code that you want to run once, not on every page load. Variables you define in setup chunks can be read by all other code chunks in the document.
142+
By simply adding `#| context: setup` to the code cell, we can tell Quarto to run the code only once, when the Shiny process starts up. Setup cells are a great way to factor out code that you want to run once, not on every page load. Variables you define in setup cells can be read by all other code cells in the document.
115143

116144
### Dashboard Pages
117145

118-
At the top of this dashboard, you can see "Plots" and "Data" headings. These are called **dashboard pages**. Dashboard pages are a way to organize your dashboard into multiple pages, each with its own set of outputs. You can insert dashboard pages by adding level 1 headers to your Markdown--in this case, `# Plots` and `# Data`.
146+
At the top of this dashboard, you can see "Plots" and "Data" headings. These are called **dashboard pages**. Dashboard pages are a way to organize your dashboard into multiple pages, each with its own set of outputs. You can insert dashboard pages by adding level 1 headers to your Markdown. In this case, `# Plots` and `# Data`:
147+
148+
```` {.python .pymd}
149+
# Plots
150+
151+
# Data
152+
````
119153

120154
### Data Frame Outputs
121155

@@ -175,6 +209,8 @@ Note the `filtered_penguins()` call. To reiterate, this call does not necessaril
175209

176210
To learn more about Shiny for Python interactive documents see the following articles:
177211

212+
- [Component Browser](https://jcheng.shinyapps.io/shiny-component-browser/#outputs) enumerates the available Shiny inputs and outputs, along with code snippets you can copy and paste into your dashboard.
213+
178214
- [Running Dashboards](running.qmd) covers in more depth how to run Shiny dashboards both within VS Code and at the command line, as well as how to deploy them to end users.
179215

180216
- [Execution Contexts](execution.qmd) goes in depth on when different code cells run (e.g. rendering vs. serving).

docs/dashboards/interactivity/shiny-python/running.qmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ To learn more about Shiny for Python interactive documents see the following art
6262

6363
- [Getting Started](index.qmd) explains the basics of Shiny interactive documents.
6464

65+
- [Component Browser](https://jcheng.shinyapps.io/shiny-component-browser/#outputs) enumerates the available Shiny inputs and outputs, along with code snippets you can copy and paste into your dashboard.
66+
6567
- [Execution Contexts](execution.qmd) goes in depth on when different code cells run (e.g. rendering vs. serving).
6668

6769
- [Component Layout](/docs/interactive/layout.qmd) enumerates the various techniques you can use to layout interactive components within your documents.

0 commit comments

Comments
 (0)