Skip to content

pywry.notebook

Notebook environment detection and widget creation helpers.


Environment Detection

pywry.notebook.NotebookEnvironment

Bases: Enum

Detected notebook environment type.

pywry.notebook.detect_notebook_environment cached

detect_notebook_environment() -> NotebookEnvironment

Detect the current notebook environment.

This function is cached - environment detection only happens once.

RETURNS DESCRIPTION
NotebookEnvironment

The detected environment. NONE means use native window, IPYTHON_TERMINAL means use native window (not inline).

pywry.notebook.should_use_inline_rendering

should_use_inline_rendering() -> bool

Check if inline notebook rendering should be used.

Returns True for: - Notebook environments (Jupyter, Colab, VS Code, etc.) - When PYWRY_SERVER__FORCE_NOTEBOOK=true (for headless web deployments)

Returns False for: - No IPython - IPython terminal - Unknown environments (unless force_notebook is set)

RETURNS DESCRIPTION
bool

True if inline rendering should be used.

pywry.notebook.is_anywidget_available

is_anywidget_available() -> bool

Check if anywidget is installed and available.

RETURNS DESCRIPTION
bool

True if anywidget >= 0.9.0 is available.

pywry.notebook.is_cloud_environment

is_cloud_environment() -> bool

Check if running in a cloud-hosted notebook environment.

Cloud environments may use CDN assets instead of embedded.

RETURNS DESCRIPTION
bool

True for Colab, Kaggle, Azure, Databricks.

pywry.notebook.clear_environment_cache

clear_environment_cache() -> None

Clear the cached environment detection.

Useful for testing or when environment changes.


Widget Factories

pywry.notebook.create_plotly_widget

create_plotly_widget(figure_json: str, widget_id: str, title: str = 'PyWry', theme: str = 'dark', width: str = '100%', height: int = 500, port: int | None = None, toolbars: list[Any] | None = None, modals: list[Any] | None = None, force_iframe: bool = False) -> Any

Create a Plotly widget using the best available backend.

Automatically selects: 1. PyWryPlotlyWidget (anywidget) if available - best performance 2. InlineWidget (FastAPI) as fallback - broader compatibility

PARAMETER DESCRIPTION
figure_json

Plotly figure as JSON string (should include 'config' if needed).

TYPE: str

widget_id

Unique widget identifier.

TYPE: str

title

Widget title.

TYPE: str DEFAULT: 'PyWry'

theme

'dark' or 'light'.

TYPE: str DEFAULT: 'dark'

width

Widget width (CSS).

TYPE: str DEFAULT: '100%'

height

Widget height in pixels.

TYPE: int DEFAULT: 500

port

Server port (only for InlineWidget fallback).

TYPE: int DEFAULT: None

toolbars

List of toolbars. Each can be a Toolbar model or dict with: - position: "top", "bottom", "left", "right", "inside" - items: list of item configs (Button, Select, etc.)

TYPE: list[Toolbar | dict] DEFAULT: None

modals

List of modals. Each can be a Modal model or dict with: - component_id: unique identifier for the modal - title: modal header title - items: list of content items (HTML, form elements, etc.) - size: 'small', 'medium', 'large', or 'fullscreen'

TYPE: list[Modal | dict] DEFAULT: None

force_iframe

If True, force use of InlineWidget instead of anywidget. Required for BROWSER mode which needs open_in_browser() method. Default: False.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
BaseWidget

Widget instance implementing BaseWidget protocol.

pywry.notebook.create_dataframe_widget

create_dataframe_widget(config: Any, widget_id: str, title: str = 'PyWry', theme: str = 'dark', aggrid_theme: str = 'alpine', width: str = '100%', height: int = 500, header_html: str = '', toolbars: list[Any] | None = None, modals: list[Any] | None = None, port: int | None = None, force_iframe: bool = False) -> Any

Create a DataFrame/AG Grid widget using the best available backend.

Automatically selects: 1. PyWryAgGridWidget (anywidget) if available - best performance 2. InlineWidget (FastAPI) as fallback - broader compatibility

PARAMETER DESCRIPTION
config

Unified grid configuration from grid.build_grid_config().

TYPE: GridConfig

widget_id

Unique widget identifier.

TYPE: str

title

Widget title.

TYPE: str DEFAULT: 'PyWry'

theme

'dark' or 'light'.

TYPE: str DEFAULT: 'dark'

aggrid_theme

AG Grid theme name.

TYPE: str DEFAULT: 'alpine'

width

Widget width (CSS).

TYPE: str DEFAULT: '100%'

height

Widget height in pixels.

TYPE: int DEFAULT: 500

header_html

Custom HTML for header section.

TYPE: str DEFAULT: ''

toolbars

List of toolbars. Each can be a Toolbar model or dict with: - position: "top", "bottom", "left", "right", "inside" - items: list of item configs (Button, Select, etc.)

TYPE: list[Toolbar | dict] DEFAULT: None

modals

List of modals. Each can be a Modal model or dict with: - component_id: unique identifier for the modal - title: modal header title - items: list of content items (HTML, form elements, etc.) - size: 'small', 'medium', 'large', or 'fullscreen'

TYPE: list[Modal | dict] DEFAULT: None

port

Server port (only for InlineWidget fallback).

TYPE: int DEFAULT: None

force_iframe

If True, force use of InlineWidget instead of anywidget. Required for BROWSER mode which needs open_in_browser() method. Default: False.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
BaseWidget

Widget instance implementing BaseWidget protocol.