Skip to content

pywry.state_mixins

Shared mixins for unified state management across rendering paths. These provide a consistent API for Grid, Plotly, and Toolbar interactions regardless of the rendering backend (native window, inline IFrame, or Jupyter widget).


Base

pywry.state_mixins.EmittingWidget

Base class for state mixins, providing the emit interface.

Functions

emit

emit(event_type: str, data: dict[str, Any]) -> None

Emit an event to the JavaScript side.

This method must be implemented by the consuming class (e.g., PyWryWidget).

alert

alert(message: str, alert_type: Literal['info', 'success', 'warning', 'error', 'confirm'] = 'info', title: str | None = None, duration: int | None = None, callback_event: str | None = None, position: Literal['top-right', 'top-left', 'bottom-right', 'bottom-left'] = 'top-right') -> None

Show a toast notification.

PARAMETER DESCRIPTION
message

The message to display.

TYPE: str

alert_type

Alert type: 'info', 'success', 'warning', 'error', or 'confirm'.

TYPE: str DEFAULT: 'info'

title

Optional title for the toast.

TYPE: str DEFAULT: None

duration

Auto-dismiss duration in ms. Defaults based on type.

TYPE: int DEFAULT: None

callback_event

Event name to emit when confirm dialog is answered.

TYPE: str DEFAULT: None

position

Toast position: 'top-right', 'top-left', 'bottom-right', 'bottom-left'.

TYPE: str DEFAULT: 'top-right'


Grid Mixin

pywry.state_mixins.GridStateMixin

Bases: EmittingWidget

Mixin for AG Grid state management.

Functions

request_grid_state

request_grid_state(context: dict[str, Any] | None = None, grid_id: str | None = None) -> None

Request the current state of the grid (sort, filter, columns, etc.).

The frontend will respond with a 'grid:state-response' event.

PARAMETER DESCRIPTION
context

Context data to include in the response. This is echoed back unchanged in the state_response event, useful for tracking which request triggered the response (e.g., view switching).

TYPE: dict DEFAULT: None

grid_id

The ID of the grid to query.

TYPE: str DEFAULT: None

restore_state

restore_state(state: dict[str, Any], grid_id: str | None = None) -> None

Restore the grid state from a previous state object.

reset_state

reset_state(grid_id: str | None = None, hard: bool = False) -> None

Reset the grid state to default values.

PARAMETER DESCRIPTION
grid_id

The ID of the grid to reset.

TYPE: str | None DEFAULT: None

hard

If True, completely destroys and recreates the grid instance. If False (default), only resets state columns/filters/sort.

TYPE: bool DEFAULT: False

update_cell

update_cell(row_id: str | int, col_id: str, value: Any, grid_id: str | None = None) -> None

Update a single cell value.

update_data

update_data(data: list[dict[str, Any]], grid_id: str | None = None, strategy: str = 'set') -> None

Update grid data rows.

PARAMETER DESCRIPTION
data

List of row data dictionaries.

TYPE: list[dict[str, Any]]

grid_id

The ID of the grid.

TYPE: str | None DEFAULT: None

strategy

Update strategy ('set', 'append', 'update'). 'set' replaces all data.

TYPE: str DEFAULT: 'set'

update_columns

update_columns(column_defs: list[dict[str, Any]], grid_id: str | None = None) -> None

Update column definitions.

update_grid

update_grid(data: list[dict[str, Any]] | Any | None = None, columns: list[dict[str, Any]] | None = None, restore_state: dict[str, Any] | None = None, grid_id: str | None = None) -> None

Update the grid with new data, columns, and/or restore saved state.

This is the primary method for switching views or making bulk updates. It combines data, column, and state updates into a single operation to minimize UI flicker.

PARAMETER DESCRIPTION
data

New row data. If a DataFrame, it will be converted to records.

TYPE: list[dict] | DataFrame DEFAULT: None

columns

New column definitions.

TYPE: list[dict] DEFAULT: None

restore_state

Previously saved grid state to restore (from grid:state-response). Contains columnState, filterModel, sortModel.

TYPE: dict DEFAULT: None

grid_id

The ID of the grid to update.

TYPE: str DEFAULT: None


Plotly Mixin

pywry.state_mixins.PlotlyStateMixin

Bases: EmittingWidget

Mixin for Plotly chart state management.

Functions

update_figure

update_figure(figure: Any, chart_id: str | None = None, animate: bool = False, config: dict[str, Any] | None = None) -> None

Update the entire chart figure (data and layout).

update_layout

update_layout(layout: dict[str, Any], chart_id: str | None = None) -> None

Update specific layout properties (Plotly.relayout).

update_traces

update_traces(patch: dict[str, Any], indices: list[int] | None = None, chart_id: str | None = None) -> None

Update specific trace properties (Plotly.restyle).

request_plotly_state

request_plotly_state(chart_id: str | None = None) -> None

Request current chart state (viewport, zoom, selections).

reset_zoom

reset_zoom(chart_id: str | None = None) -> None

Reset the chart zoom/pan to default.

set_zoom

set_zoom(xaxis_range: tuple[Any, Any] | None = None, yaxis_range: tuple[Any, Any] | None = None, chart_id: str | None = None) -> None

Set specific zoom ranges for axes.

set_trace_visibility

set_trace_visibility(visible: bool | str | list[bool | str], indices: list[int] | None = None, chart_id: str | None = None) -> None

Set visibility for specific traces.


Toolbar Mixin

pywry.state_mixins.ToolbarStateMixin

Bases: EmittingWidget

Mixin for Toolbar state interactions.

Functions

request_toolbar_state

request_toolbar_state(toolbar_id: str | None = None) -> None

Request values of all toolbar inputs.

set_toolbar_value

set_toolbar_value(component_id: str, value: Any = _UNSET, toolbar_id: str | None = None, **attrs: Any) -> None

Set a toolbar component's value and/or attributes.

PARAMETER DESCRIPTION
component_id

The component_id of the toolbar item to update.

TYPE: str

value

The new value for the component.

TYPE: Any DEFAULT: _UNSET

toolbar_id

The toolbar ID (if applicable).

TYPE: str DEFAULT: None

**attrs

Additional attributes to set on the component: - label/text: Update text content - disabled: Enable/disable the component - variant: Button variant (primary, secondary, danger, etc.) - tooltip/description: Update tooltip text - options: Update dropdown/select options - style: Inline styles (str or dict) - className/class: Add/remove CSS classes - placeholder, min, max, step: Input constraints

TYPE: Any DEFAULT: {}

set_toolbar_values

set_toolbar_values(values: dict[str, Any], toolbar_id: str | None = None) -> None

Set multiple toolbar input values at once.