diff --git a/examples/frameworks/shiny/README.md b/examples/frameworks/shiny/README.md new file mode 100644 index 00000000000..d2d8ccae750 --- /dev/null +++ b/examples/frameworks/shiny/README.md @@ -0,0 +1,8 @@ +# PyShiny + marimo + +This is a simple example of how to use PyShiny with marimo. This example programmatically creates multiple marimo apps from a directory, and then serves them as a single PyShiny app. + +## Running the app + +1. [Install `uv`](https://github.com/astral-sh/uv/?tab=readme-ov-file#installation) +2. Run the app with `shiny run main.py --port 8007` diff --git a/examples/frameworks/shiny/main.py b/examples/frameworks/shiny/main.py new file mode 100644 index 00000000000..145aea88dc3 --- /dev/null +++ b/examples/frameworks/shiny/main.py @@ -0,0 +1,88 @@ +# /// script +# requires-python = ">=3.12" +# dependencies = [ +# "shiny", +# "marimo", +# "vega-datasets==0.9.0", +# ] +# /// +from shiny import App, render, ui +from htmltools import HTML +import marimo +import os +from starlette.middleware.wsgi import WSGIMiddleware +from starlette.applications import Starlette +from starlette.routing import Mount, Route +from starlette.responses import RedirectResponse + + +ui_dir = os.path.join(os.path.dirname(__file__), "..", "..", "ui") +templates_dir = os.path.join(os.path.dirname(__file__), "templates") + +marimo_app = marimo.create_asgi_app() +app_names: list[str] = [] + +for filename in sorted(os.listdir(ui_dir)): + if filename.endswith(".py"): + app_name = os.path.splitext(filename)[0] + app_path = os.path.join(ui_dir, filename) + marimo_app = marimo_app.with_app(path=f"/{app_name}", root=app_path) + app_names.append(app_name) + +shiny_ui = ui.page_fluid( + ui.head_content( + ui.tags.script(src="https://cdn.tailwindcss.com"), + ui.tags.link( + href="https://cdn.jsdelivr.net/npm/daisyui@4.11.1/dist/full.min.css", + rel="stylesheet", + ), + ), + ui.output_ui("_html"), + title="Marimo apps in Shiny", +) + + +def shiny_server(input, output, session): + @render.ui + def _html(): + subdiv = [] + for name in app_names: + subdiv.append( + f""" +
+ """ + ) + body = f""" +