Skip to content

notebook

notebook #

Notebook — a log as one HTML output in a Jupyter cell, redrawn in place.

The tree is rendered with html and shown through IPython's display handle, then updated at most ten times a second on the thread that logs — IPython routes output by the thread's cell, so no redraw thread is used, and a step that stays silent repaints on its next entry.

Show = Callable[[str], Callable[[str], None]] #

Shows the first HTML and returns the function that replaces it.

Notebook #

Bases: Sink

Show the log in the running cell (requires the notebook extra).

with cg.logging.to(cgui.Notebook()):
    thumbnails = thumbnail_sheet(target.sessions)  # a memoized recipe of yours
Source code in capturegraph-ui/capturegraph_ui/log/notebook.py
class Notebook(cg.logging.Sink):
    """Show the log in the running cell (requires the ``notebook`` extra).

    ```python
    with cg.logging.to(cgui.Notebook()):
        thumbnails = thumbnail_sheet(target.sessions)  # a memoized recipe of yours
    ```
    """

    def __init__(self, show: Show | None = None) -> None:
        """A sink that shows through ``show``; IPython's display handle by default."""
        self._show = show if show is not None else ipython_show
        self._log: cg.logging.Log | None = None
        self._update: Callable[[str], None] | None = None
        self._shown_at = 0.0

    def start(self, log: cg.logging.Log) -> None:
        """Show the empty log; every change from now on redraws it."""
        self._log = log
        self._update = self._show(html(log))
        self._shown_at = time.monotonic()

    def emit(self, entry: cg.logging.Entry) -> None:
        """Redraw, unless the last redraw was a moment ago."""
        if time.monotonic() - self._shown_at >= REFRESH_SECONDS:
            self._refresh()

    def finish(self) -> None:
        """Redraw once more so the final state shows."""
        self._refresh()

    def _refresh(self) -> None:
        if self._log is not None and self._update is not None:
            self._update(html(self._log))
            self._shown_at = time.monotonic()

__init__(show=None) #

A sink that shows through show; IPython's display handle by default.

Source code in capturegraph-ui/capturegraph_ui/log/notebook.py
def __init__(self, show: Show | None = None) -> None:
    """A sink that shows through ``show``; IPython's display handle by default."""
    self._show = show if show is not None else ipython_show
    self._log: cg.logging.Log | None = None
    self._update: Callable[[str], None] | None = None
    self._shown_at = 0.0

emit(entry) #

Redraw, unless the last redraw was a moment ago.

Source code in capturegraph-ui/capturegraph_ui/log/notebook.py
def emit(self, entry: cg.logging.Entry) -> None:
    """Redraw, unless the last redraw was a moment ago."""
    if time.monotonic() - self._shown_at >= REFRESH_SECONDS:
        self._refresh()

finish() #

Redraw once more so the final state shows.

Source code in capturegraph-ui/capturegraph_ui/log/notebook.py
def finish(self) -> None:
    """Redraw once more so the final state shows."""
    self._refresh()

start(log) #

Show the empty log; every change from now on redraws it.

Source code in capturegraph-ui/capturegraph_ui/log/notebook.py
def start(self, log: cg.logging.Log) -> None:
    """Show the empty log; every change from now on redraws it."""
    self._log = log
    self._update = self._show(html(log))
    self._shown_at = time.monotonic()

ipython_show(fragment) #

Display fragment in the current cell and return its updater.

Raises:

Type Description
ImportError

If IPython is not installed (the notebook extra of capturegraph-ui).

RuntimeError

If no display hook is active, as outside a notebook kernel.

Source code in capturegraph-ui/capturegraph_ui/log/notebook.py
def ipython_show(fragment: str) -> Callable[[str], None]:
    """Display ``fragment`` in the current cell and return its updater.

    Raises:
        ImportError: If IPython is not installed (the ``notebook`` extra of capturegraph-ui).
        RuntimeError: If no display hook is active, as outside a notebook kernel.
    """
    from IPython.display import HTML, display

    handle = display(HTML(fragment), display_id=True)
    if handle is None:
        raise RuntimeError("no display hook is active; the Notebook sink needs a notebook kernel")
    return lambda fresh: handle.update(HTML(fresh))