Skip to content

Conversation

wch
Copy link
Collaborator

@wch wch commented Mar 13, 2024

This PR supersedes #1212. It adds support for Shiny Express apps in Quarto Dashboards. Prior to this PR, it would throw an error if a Quarto document had an import from shiny.express.

For example:

---
title: "Palmer Penguins"
format: dashboard
server: shiny
---

```{python}
#| context: setup
import seaborn as sns
from shiny.express import render, ui, input
penguins = sns.load_dataset("penguins")
```

# {.sidebar}

```{python}
species = list(penguins["species"].value_counts().index)
ui.input_checkbox_group(
    "species", "Species:",
    species, selected = species
)
```

# Plots

```{python}
@render.plot
def depth():
    data = penguins[penguins["species"].isin(input.species())]
    return sns.displot(
        data, x = "bill_depth_mm",
        hue = "species", kind = "kde",
        fill = True
    )
```

This is what it looks like:

image

The generated app.py looks like this.

# This file generated by Quarto; do not edit by hand.
# shiny_mode: core

from __future__ import annotations

from pathlib import Path
from shiny import App, Inputs, Outputs, Session, ui

import seaborn as sns
from shiny import reactive
from shiny.express import render, ui
penguins = sns.load_dataset("penguins")

# ========================================================================




def server(input: Inputs, output: Outputs, session: Session) -> None:
    # from shiny.express import render, ui
    species = list(penguins["species"].value_counts().index)
    ui.input_checkbox_group(
        "species", "Species:",
        species, selected = species
    )

    # ========================================================================

    @render.plot
    def depth():
        data = penguins[penguins["species"].isin(input.species())]
        return sns.displot(
            data, x = "bill_depth_mm",
            hue = "species", kind = "kde",
            fill = True
        )

    # ========================================================================



    return None


_static_assets = ["simple_files"]
_static_assets = {"/" + sa: Path(__file__).parent / sa for sa in _static_assets}

app = App(
    Path(__file__).parent / "simple.html",
    server,
    static_assets=_static_assets,
)

A few things to note about this:

  • It uses the UI was generated by Quarto (in the form of a static .html file).
  • The presence of a magic comment # shiny_mode: express or # shiny_mode: core will force the app to be run as an Express or Core app. This comment must be in the first 1000 characters of the app file.
  • The is_express_app() function has changed. There are other places where we use copies of this code, (Shiny Server and rsconnect-python), and the function will need to be updated in those places.

@wch wch requested a review from jcheng5 March 13, 2024 00:41
@wch
Copy link
Collaborator Author

wch commented Mar 13, 2024

  • Use a magic comment to force app to be treated as Core or Express app.
  • Warn if page_opts() or app_opts() are called?

@wch wch force-pushed the quarto-express-2 branch from 2e644a6 to d16ca47 Compare March 13, 2024 22:00
@wch wch merged commit 2b66c89 into main Mar 14, 2024
@wch wch deleted the quarto-express-2 branch March 14, 2024 20:10
schloerke added a commit that referenced this pull request Mar 15, 2024
* main:
  Add support for Shiny Express in Quarto Dashboards, take 2 (#1217)
  Force UTF-8 for Shiny Express on Windows (#1203)
  Typing fixes
  feat(card): Report full screen state as a Shiny input (#1215)
  Use TypeScript strict mode (#1208)
  Update author email
  test: Resolve testing comments (#1206)
  Fix URL
  Update badges
  Bump dev version to 0.8.1.9000 (#1202)
  chore(deps): Update bslib css (#1200)
  chore: no new features
  v0.8.1
schloerke added a commit to joesho112358/py-shiny that referenced this pull request Mar 23, 2024
* main: (24 commits)
  chore: Disable broken test on CI; Fix broken docs entries (posit-dev#1241)
  Add back missing session methods (posit-dev#1239)
  Add kitchensink tests for valuebox (posit-dev#1229)
  For Express, emit message if need to upgrade rsconnect-python (posit-dev#1233)
  feat: Editable data frame (posit-dev#1198)
  Add tests for shiny_mode comment detection
  fix(input_dark_mode): Allow users to customize `style` attribute (posit-dev#1207)
  chore(get_current_session): Return default session if current is explicitly None (posit-dev#1086)
  chore: use dev htmtlools (posit-dev#1228)
  feat(layout_columns): Use `sm` as the default `col_widths` breakpoint (posit-dev#1222)
  feat: ensure min/max height args on `value_box()` and column layouts (posit-dev#1223)
  fix(static_assets): Need the directory containing the `app.py`, not the file itself (posit-dev#1219)
  Update bslib and shiny dependencies (posit-dev#1221)
  Update docstring and give hint for mypy
  Add support for Shiny Express in Quarto Dashboards, take 2 (posit-dev#1217)
  Force UTF-8 for Shiny Express on Windows (posit-dev#1203)
  Typing fixes
  feat(card): Report full screen state as a Shiny input (posit-dev#1215)
  Use TypeScript strict mode (posit-dev#1208)
  Update author email
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants