render_ipysigma

render_ipysigma class

Reactively render a graph with ipysigma (sigma.js).

Usage Source

render_ipysigma(
    _fn: Optional[ValueFn[ValueT]] = None,
    *,
    width: Optional[str] = None,
    height: Optional[str] = None,
    fill: Optional[bool] = None,
    fillable: Optional[bool] = None,
    rate_policy: Optional[RatePolicy] = None,
    rate_delay: Optional[float] = None,
)

@render_ipysigma is a decorator that creates a render function – a function that returns the graph to display and re-runs when the reactive values it reads change:

import networkx as nx

from shinywidgets import render_ipysigma


@render_ipysigma
def sigma():
    return nx.karate_club_graph()

The render function may return either a networkx graph (which is coerced to an ipysigma.Sigma widget with default options; requires pip install 'shinywidgets[ipysigma]') or an ipysigma.Sigma widget, which is the way to customize node and edge appearance (e.g., Sigma(G, node_size=G.degree, node_color="community")). This is also the answer for pyvis users: ipysigma renders larger networks more smoothly and, being a widget, can report the selected node back to Python. In a filling layout, the graph fills its container, unless the widget has an explicit height or raw_height (a height of 500px, sigma’s default, or one set globally with Sigma.set_defaults(height=...), counts as unset and fills).

Note

Accessing the rendered widget: the render function gains two read-only attributes: .value, the value it most recently returned, and .widget, the rendered Sigma instance.

Responding to user interaction: read the user’s interactions reactively with sigma.read() (e.g., sigma.read("selected_node"); also "selected_edge", "camera_state" and "layout").

Mutating the widget: ipysigma only reads its options when the graph is first drawn, so setting traits on .widget afterwards does not update the browser. To change what’s shown, re-render, passing any state you want to keep (e.g., layout=, camera_state=) to the new Sigma.

Parameters

width : Optional[str] = None

A CSS length unit (e.g., "400px" or "100%") for the output’s width.

height : Optional[str] = None

A CSS length unit for the output’s height. When None (the default), the output is designed to fill the height of its parent container (see fill and fillable).

fill : Optional[bool] = None

Whether the output should grow or shrink to fit its parent container. When None (the default), this is True if height is not specified.

fillable : Optional[bool] = None

Whether the output container should fill available space (scrolling its contents on overflow). When None (the default), this is True if height is not specified.

See also

output_widget(): in Shiny Core, the UI needs this to hold the output (Shiny Express displays it where the render function is defined).