.md →
GraphWidget API#
Bases: AnyWidget
Programmatic force-directed graph widget.
GraphWidget renders nodes and edges supplied from Python. Nodes may be
strings, numbers, or dicts. Edges may be (source, target) pairs or dicts
with source and target keys.
width=None (the default) makes the widget fill its container's width
and reflow when the container resizes. Passing an integer width pins
the SVG to that exact pixel size. height is always an exact pixel
height (default 400).
Example
GraphWidget(nodes=["Alpha", "Beta"], edges=[("Alpha", "Beta")])
Source code in wigglystuff/graph_widget.py
add_edge #
add_edge(source: Any, target: Any, *, id: Any = None, name: Any = None, width: int | float | None = None, color: str | None = None, data: Any = None, **attrs: Any) -> str
Add an edge and return its normalized id.
Source code in wigglystuff/graph_widget.py
add_node #
add_node(name: Any = None, *, id: Any = None, size: int | float | None = None, color: str | None = None, data: Any = None, **attrs: Any) -> str
Add a node and return its normalized id.
Source code in wigglystuff/graph_widget.py
attach_node #
attach_node(source: Any, name: Any = None, *, id: Any = None, edge_id: Any = None, edge_name: Any = None, size: int | float | None = None, color: str | None = None, data: Any = None, edge_width: int | float | None = None, edge_color: str | None = None, edge_data: Any = None, **attrs: Any) -> tuple[str, str]
Attach a node to an existing source node.
If id or name resolves to an existing node, only the edge is added.
Otherwise a new node is created first.
Returns:
| Type | Description |
|---|---|
tuple[str, str]
|
The normalized |
Source code in wigglystuff/graph_widget.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
clear_selection #
detach_node #
Remove all edges attached to a node.
Set delete=True to remove the node as well.
Source code in wigglystuff/graph_widget.py
get_adjacency_matrix #
Return an adjacency matrix for the current graph.
Source code in wigglystuff/graph_widget.py
get_selected_edge_data #
Return full edge dicts for currently selected edges.
get_selected_node_data #
Return full node dicts for currently selected nodes.
remove_edge #
Remove an edge by id or index.
Source code in wigglystuff/graph_widget.py
remove_node #
Remove a node by id, unique name, or index, including incident edges.
Synced traitlets#
| Traitlet | Type | Notes |
|---|---|---|
nodes |
list[dict] |
Node dicts with normalized id and optional name, size, color, and data. |
edges |
list[dict] |
Edge dicts with normalized id, source, target, and optional name, width, color, and data. |
directed |
bool |
Draw directed edges when true. |
bounded |
bool |
Keep nodes inside the visible SVG bounds when true. Disable it for graphs that should spread beyond the viewport and be explored with pan/zoom. |
width |
int \| None |
Canvas width in pixels. None (default) makes the widget fill its container's width and reflow when the container resizes. |
height |
int |
Canvas height in pixels (default 400). |
selected_nodes |
list[str] |
IDs of currently selected nodes. |
selected_edges |
list[str] |
IDs of currently selected edges. |
Layout Notes#
GraphWidget preserves browser-side node positions when nodes or edges
change. Newly connected nodes are initialized near the existing endpoint they
attach to. Use attach_node(source, name, ...) when adding one new node plus
its connecting edge from Python; if name or id already resolves to a node,
only the edge is added. Use detach_node(node) to remove all edges attached to
a node while keeping the node visible, or detach_node(node, delete=True) to
remove the node too.