# Embed the Modules

The editor and viewer are Shiny Core modules, so you can place them in another app. This guide shows the smallest app that uses both, and the assets the page needs for the viewer to draw anything.


# Connect the editor to the viewer

Each module has a UI function and a server function. [editor_server()](../reference/editor_server.md#shypermaid.editor_server) returns the committed Mermaid code, and [viewer_server()](../reference/viewer_server.md#shypermaid.viewer_server) takes that value as its `code` argument:


*\[Rich HTML output -- view on the documentation site\]*


The dotted lines pair each UI function with its server function through a shared id. The solid line is the only data that crosses between the modules.


# Add the page assets

The viewer draws nothing on its own. The browser needs Mermaid, svg-pan-zoom, and the package's JavaScript binding, and the page needs the package stylesheet. All four ship in the package's `www/` folder (the libraries are in `www/vendor/`), so the app works offline. The app below adds them to the page head and serves `www/` at `/shypermaid`:

``` python
from pathlib import Path

import shypermaid
from shiny import App, ui

from shypermaid import editor_server, editor_ui, viewer_server, viewer_ui

WWW = Path(shypermaid.__file__).parent / "www"
MERMAID_JS = "shypermaid/vendor/mermaid.min.js"
PANZOOM_JS = "shypermaid/vendor/svg-pan-zoom.min.js"

app_ui = ui.page_fillable(
    ui.head_content(
        ui.tags.script(src=MERMAID_JS),
        ui.tags.script(src=PANZOOM_JS),
        ui.tags.link(rel="stylesheet", href="shypermaid/styles.css"),
        ui.tags.script(src="shypermaid/mermaid-binding.js"),
    ),
    ui.layout_sidebar(ui.sidebar(editor_ui("editor")), viewer_ui("viewer")),
)


def server(input, output, session):
    code = editor_server("editor")
    viewer_server("viewer", code=code)


app = App(app_ui, server, static_assets={"/shypermaid": WWW})
```

The script order matters. Mermaid and svg-pan-zoom must load before `mermaid-binding.js`, because the binding calls both libraries when the first diagram arrives.

> **Warning: Warning**
>
> Without the `static_assets` argument, the libraries, stylesheet, and binding return 404 errors. The page still loads, but the canvas stays empty and no error appears in the app.


# Use your own code source

[viewer_server()](../reference/viewer_server.md#shypermaid.viewer_server) accepts any `reactive.Value[str]`, so the editor is optional. A fixed diagram, a file reader, or another module can supply the code instead:

``` python
from shiny import reactive


def server(input, output, session):
    code = reactive.Value("flowchart TD\n    a --> b")
    viewer_server("viewer", code=code)
```

We can confirm it works by running the app and checking that the canvas shows the two node flowchart.


# Recap

Pass the value from [editor_server()](../reference/editor_server.md#shypermaid.editor_server) (or any `reactive.Value[str]`) to [viewer_server()](../reference/viewer_server.md#shypermaid.viewer_server). Add the two libraries, the stylesheet, and the binding to the page head, and serve `www/` at `/shypermaid`. The [Render Cycle](02.02-explanation-render-cycle.md) page explains what happens to the code after it leaves Python.
