Skip to content

API Docs

shinyobservable.Observable

Bases: object

Create an Observable notebook instance

Parameters:

Name Type Description Default
notebook str

The URL of the notebook to be embedded.

required
cells list

The cells to be embedded. If None, the entire notebook is embedded.

None
width int | str

The width of the notebook element.

None
Source code in shinyobservable/_core.py
class Observable(object):
    """Create an Observable notebook instance

    Args:
        notebook: The URL of the notebook to be embedded.
        cells: The cells to be embedded. If `None`, the entire notebook is embedded.
        width: The width of the notebook element.
    """

    data = dict()

    def __init__(
        self, notebook: str, cells: list = None, width: int | str = None
    ) -> None:
        if isinstance(width, int):
            width = f"{width}px"

        self.width = width
        if "//api" not in notebook:
            notebook = (
                f"{BASE_API_URL}/{relpath(notebook, BASE_URL)}.js?v={API_VERSION}"
            )

        self.notebook = notebook
        self.cells = cells

    def redefine(self, **kwargs) -> Observable:
        """Redefine cells of the embedded notebook

        Examples:
            >>> # Redefine the data cell of a notebook
            >>> nb = Observable("some-notebook").redefine(
            ...     data=[
            ...         dict(x=1, y=2),
            ...         dict(x=2, y=4)
            ...     ]
            ... )
            >>>
        """
        self.data = kwargs
        return self

    def to_dict(self):
        return dict(
            notebook=self.notebook, cells=self.cells, data=self.data, width=self.width
        )

redefine(**kwargs)

Redefine cells of the embedded notebook

Examples:

>>> # Redefine the data cell of a notebook
>>> nb = Observable("some-notebook").redefine(
...     data=[
...         dict(x=1, y=2),
...         dict(x=2, y=4)
...     ]
... )
>>>
Source code in shinyobservable/_core.py
def redefine(self, **kwargs) -> Observable:
    """Redefine cells of the embedded notebook

    Examples:
        >>> # Redefine the data cell of a notebook
        >>> nb = Observable("some-notebook").redefine(
        ...     data=[
        ...         dict(x=1, y=2),
        ...         dict(x=2, y=4)
        ...     ]
        ... )
        >>>
    """
    self.data = kwargs
    return self

shinyobservable.ObservableRenderer

Bases: Renderer[Observable]

A decorator for a function that returns an Observable object

Source code in shinyobservable/render.py
class ObservableRenderer(Renderer[Observable]):
    """A decorator for a function that returns an `Observable` object"""

    def auto_output_ui(self) -> Tag:
        return output_observable(self.output_id)

    async def transform(self, value: Observable) -> Jsonifiable:
        return value.to_dict()

shinyobservable.output_observable(id)

Create an output control for an Observable object

Parameters:

Name Type Description Default
id str

An output id.

required
Source code in shinyobservable/ui.py
def output_observable(id: str) -> Tag:
    """Create an output control for an `Observable` object

    Arguments:
        id: An output id.
    """
    return ui.div(
        observable_bindings_dep,
        # Use resolve_id so that our component will work in a module
        id=resolve_id(id),
        class_="shiny-observable-output",
    )

shinyobservable.ObservableContext

Bases: object

Create an Observable context instance

Parameters:

Name Type Description Default
id str

The output id of the notebook instance to be updated.

required
session Session

A Shiny session. If None, the active session is used.

None
Source code in shinyobservable/_context.py
class ObservableContext(object):
    """Create an Observable context instance

    Arguments:
        id: The output id of the notebook instance to be updated.
        session: A Shiny session. If `None`, the active session is used.
    """

    data = dict()

    def __init__(self, id: str, session: Session = None) -> None:
        self.id = id
        self._session = require_active_session(session)
        self._message_queue = []

    async def __aenter__(self):
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        await self.render()

    def redefine(self, **kwargs) -> None:
        """Redefine cells of the embedded notebook"""
        self.data = kwargs

    async def render(self):
        await self._session.send_custom_message(
            f"observable-{self.id}", {"id": self.id, "data": self.data}
        )

redefine(**kwargs)

Redefine cells of the embedded notebook

Source code in shinyobservable/_context.py
def redefine(self, **kwargs) -> None:
    """Redefine cells of the embedded notebook"""
    self.data = kwargs