Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/facett

py/facett_demo/facet_host.py

"""Host glue: mount `facett` components in this Streamlit app.

Since 0.2.0 the Streamlit glue lives in the components package itself —
``facett.streamlit`` (installed by ``facett[streamlit]``, which this app depends on) —
so every Streamlit app mounts components through ONE ``declare_component`` rather than
each carrying a copy. This module only adds what is THIS app's: the sidebar's dev-mode
toggle, read from the session and handed to the component as ``dev``.

There is deliberately no UI code here. Nothing reimplements a facett surface: the wasm
draws every pixel, and this file just says which component to mount and hands it the
caller's data. The mount roads (the installed package's ``path=``, or a host's ``url=``
via ``$FACETT_COMPONENT_URL``) and the secure-context constraint are documented in
``facett.streamlit``.
"""

from __future__ import annotations

from typing import Any

import facett.streamlit as _fst

__all__ = ["facett_component", "is_data_driven", "manifest", "data_driven", "build_info", "dev_mode"]

# Re-exported so panes ask the SHIPPED manifest what is renderable.
manifest = _fst.manifest
data_driven = _fst.data_driven
is_data_driven = _fst.is_data_driven
build_info = _fst.build_info

#: The env var naming a host that serves the bundle (knut) — `facett.streamlit`'s.
COMPONENT_URL_ENV = _fst.COMPONENT_URL_ENV

#: The sidebar toggle's session key. On, every mounted component shows the navgraph
#: (numbered navigation graph, `navgraph-design.md`) — drawn by the wasm, not here.
DEV_MODE_KEY = "dev_mode"


def dev_mode() -> bool:
    """Whether the sidebar's dev-mode toggle is on (False outside a Streamlit run)."""
    try:
        import streamlit as st

        return bool(st.session_state.get(DEV_MODE_KEY, False))
    except Exception:  # noqa: BLE001 — no session outside `streamlit run` / AppTest
        return False


def facett_component(
    name: str,
    *,
    data: Any | None = None,
    height: int = 520,
    key: str | None = None,
    dev: bool | None = None,
    nav: str = "labels",
) -> Any:
    """Render facett component `name` through `facett.streamlit.facett_component`.

    `dev` defaults to the sidebar toggle; everything else is passed through unchanged.
    """
    return _fst.facett_component(
        name, data=data, height=height, key=key, dev=dev_mode() if dev is None else dev, nav=nav
    )