docs: altair component page#326
Conversation
…art and jchart name and object
There was a problem hiding this comment.
Pull request overview
This PR adds comprehensive documentation for using Altair (Vega-Altair) interactive visualizations in Shiny for Python applications. The documentation includes both Express and Core syntax examples, along with three interactive variations demonstrating different selection types.
Key changes:
- Added complete Altair component documentation page with detailed usage instructions
- Provided example apps for both Express and Core syntax
- Included three variation examples: hover point selection, brush (interval) selection, and legend point selection
Reviewed changes
Copilot reviewed 9 out of 10 changed files in this pull request and generated 12 comments.
Show a summary per file
| File | Description |
|---|---|
| images/altair.png | Screenshot/preview image for the Altair component documentation |
| components/outputs/plot-altair/index.qmd | Main documentation page explaining Altair integration, API references, and usage details |
| components/outputs/plot-altair/app-express.py | Basic Express syntax example showing filtered bar chart |
| components/outputs/plot-altair/app-core.py | Basic Core syntax example showing filtered bar chart |
| components/outputs/plot-altair/app-variation-hover-point-express.py | Express example demonstrating IndexSelection with hover interactions |
| components/outputs/plot-altair/app-variation-hover-point-core.py | Core example demonstrating IndexSelection with hover interactions |
| components/outputs/plot-altair/app-variation-brush-express.py | Express example demonstrating IntervalSelection with brush interactions |
| components/outputs/plot-altair/app-variation-brush-core.py | Core example demonstrating IntervalSelection with brush interactions |
| components/outputs/plot-altair/app-variation-point-express.py | Express example demonstrating PointSelection with legend binding |
| components/outputs/plot-altair/app-variation-point-core.py | Core example demonstrating PointSelection with legend binding |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…l-brush-express.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…over-express.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…over-core.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…over-express.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
| :::{#relevant-functions} | ||
| ::: |
| :::{#example} | ||
| ::: |
| Some widgets have attributes that _contain_ observable traits. | ||
| One practical example of this is the `selections` attribute of altair's `JupyterChart` class, which has an [observable `point` trait](https://altair-viz.github.io/user_guide/interactions/jupyter_chart.html#point-selections). | ||
|
|
||
| For more information about Shiny, Alatir, and JupyterChart, see the [Altair components page](/components/outputs/plot-altair/index.qmd). |
There was a problem hiding this comment.
Maybe we should feature this more prominently (perhaps in a callout) at the bottom of the ## Get Started section?
| @@ -0,0 +1,220 @@ | |||
| --- | |||
| title: Plot (Altair) | |||
| sidebar: components | |||
|
|
||
| To make an Altair figure, we need to do the following steps: | ||
|
|
||
| ::: {.panel-tabset group="syntax"} |
There was a problem hiding this comment.
| ::: {.panel-tabset group="syntax"} | |
| ::: {.panel-tabset .shiny-mode-tabset group="shiny-app-mode"} |
| ::: {.panel-tabset group="syntax"} | ||
| ## Express | ||
|
|
||
| 1. Import the `render_altair()` functions from `shinywidgets`. |
There was a problem hiding this comment.
| 1. Import the `render_altair()` functions from `shinywidgets`. | |
| 1. Import `render_altair` from `shinywidgets`. |
| 2. Define a function where you want to create the figure. | ||
| - If your function calls reactive values, Shiny will update your figure whenever those values change, in a [reactive fashion](https://shiny.posit.co/py/docs/reactive-programming.html). | ||
| - Do not forget to return the plot object from your function. | ||
|
|
||
| 3. Decorate your plotting function with a `@render_altair()` decorator. | ||
| - If your plotting function is not the same as the `id` you used in the `ui.output_widget()`, you can add an additional `@output(id=...)` decorator. |
There was a problem hiding this comment.
| 2. Define a function where you want to create the figure. | |
| - If your function calls reactive values, Shiny will update your figure whenever those values change, in a [reactive fashion](https://shiny.posit.co/py/docs/reactive-programming.html). | |
| - Do not forget to return the plot object from your function. | |
| 3. Decorate your plotting function with a `@render_altair()` decorator. | |
| - If your plotting function is not the same as the `id` you used in the `ui.output_widget()`, you can add an additional `@output(id=...)` decorator. | |
| 2. Define a function that returns an `altair.Chart` | |
| 3. Decorate that function with a `@render_altair` | |
| - This decorator lets Shiny know to automatically update the chart when it's [reactive](../../../docs/reactive-programming.Rmd) dependencies (e.g., [inputs](../../#inputs)) change |
| - If your plotting function is not the same as the `id` you used in the `ui.output_widget()`, you can add an additional `@output(id=...)` decorator. | ||
| ::: | ||
|
|
||
| To learn more about using Jupyter widgets with Shiny visit: [shiny.posit.co/py/docs/jupyter-widgets.html](https://shiny.posit.co/py/docs/jupyter-widgets.html). |
There was a problem hiding this comment.
I feel like this should come towards the end of the article (and mention that altair is just example of a Jupyter widget in Shiny)?
There was a problem hiding this comment.
Oh, now I see we do this for Plotly -- I guess it's not super important, but ideally we'd have something better on both pages.
Related to this, a stretch goal (for both components) would be to have a few more "basic" variations before diving into the more complex "plots as inputs" and "plot mutation" concepts.
I also think a link to plotly/atair galleries at this point might be more useful than a link to the generic Jupyter Widgets article.
| we will need to use the [`shinywidgets`](https://shiny.posit.co/py/docs/jupyter-widgets.html) | ||
| library to connect Shiny with [`ipywidgets`](https://ipywidgets.readthedocs.io/en/latest/) | ||
|
|
||
| To make an Altair figure, we need to do the following steps: |
There was a problem hiding this comment.
I like how you've split the explanation up into two modes -- let's do the same for Plotly?
| - title: JupyterChart update | ||
| description: "Shows how Shiny can access and update an Altair figure using the JupyterChart representation. This example builds on the previous basic example to show how Shiny can update the Altair figure reactively." | ||
| apps: | ||
| - title: Preview | ||
| file: app-variation-update-express.py | ||
| height: 720 | ||
| - title: Express | ||
| file: app-variation-update-express.py | ||
| shinylive: | ||
| height: 720 | ||
| - title: Core | ||
| file: app-variation-update-core.py | ||
| shinylive: | ||
| height: 720 |
There was a problem hiding this comment.
I would bring the updating variations down below the "responding to user interaction" examples.
Updating (more precisely, mutating) charts in this way is more of a "only do this if you really need to for performance". Otherwise, you should just lean into the reactivity that render_altair provides



Adds a new Altair output component page. I also added a reference in the Jupyter Widgets Concepts page to the altair output.
My goal for the components page is to provide an altair, jupyterchart, and shiny example for all the ones in the altair jupyterchart docs page.
Not all of the examples are reproduced, some are redundant (like the observe results) and others like the
ipywidgets.link()aren't supported byshinywidgetsRelevant conversations:
Documentation references:
https://altair-viz.github.io/user_guide/interactions/jupyter_chart.html#selection-params
https://shiny.posit.co/py/docs/jupyter-widgets.html#user-input
TODO: having trouble with shiny -> JupyterChart updating. e.g., having a shiny radio button to change color from this example: https://altair-viz.github.io/user_guide/interactions/jupyter_chart.html#updating-charts