Skip to content

Textual Widget

GraphView

GraphView(
    nodes: dict[Hashable, dict[str, Any]] | None = None,
    edges: Any = None,
    *,
    name: str | None = None,
    id: str | None = None,
    classes: str | None = None,
    disabled: bool = False,
    zoom: float
    | tuple[float, float]
    | ZoomSpec
    | AutoZoom = 1.0,
    scroll_via_viewport: bool = False,
    **console_graph_kwargs: Any,
)

Bases: ScrollView

Initializes a new instance of the GraphView class.

Parameters:

Name Type Description Default
nodes dict[Hashable, dict[str, Any]] | None

A dict mapping node ids to data dicts.

None
edges Any

An iterable of 2-tuples (u, v) or 3-tuples (u, v, data_dict).

None
name str | None

A string representing the name of the widget (optional).

None
id str | None

A string representing the ID of the widget (optional).

None
classes str | None

A string representing the CSS classes of the widget (optional).

None
disabled bool

A boolean indicating whether the widget is disabled (optional).

False
zoom float | tuple[float, float] | ZoomSpec | AutoZoom

A float or tuple of floats representing the zoom level of the widget (optional).

1.0
scroll_via_viewport bool

A boolean indicating whether the widget should scroll via the viewport (optional).

False
console_graph_kwargs Any

Additional keyword arguments to be passed to the ConsoleGraph constructor.

{}

Raises:

Type Description
ValueError

If both viewport and scroll_via_viewport are specified.

from_networkx classmethod

from_networkx(graph: Any, **kwargs: Any) -> GraphView

Create a GraphView from a networkx DiGraph.

networkx is optional and is only needed to create the graph object passed to this method (pip install netext[networkx]).

Parameters:

Name Type Description Default
graph Any

A networkx DiGraph (or Graph) object.

required
**kwargs Any

Additional keyword arguments passed to the GraphView constructor.

{}

Returns:

Type Description
GraphView

A new GraphView instance.

set_graph

set_graph(
    nodes: dict[Hashable, dict[str, Any]] | None = None,
    edges: Any = None,
) -> None

Replace the graph data and re-render.

Parameters:

Name Type Description Default
nodes dict[Hashable, dict[str, Any]] | None

A dict mapping node ids to data dicts.

None
edges Any

An iterable of 2-tuples (u, v) or 3-tuples (u, v, data_dict).

None

node_properties

node_properties(node: Hashable) -> NodeProperties

Returns the properties of a node.

Parameters:

Name Type Description Default
node Hashable

The node to get the properties of.

required

Returns:

Type Description
NodeProperties

dict[str, Any]: The properties of the node.

add_node

add_node(
    node: Hashable,
    position: FloatPoint | None = None,
    data: dict[str, Any] | None = None,
) -> None

Adds a node to the graph widget.

Parameters:

Name Type Description Default
node Hashable

The node to add to the graph.

required
position FloatPoint | None

The position of the node in the graph (optional). If set add the node at the specific position in graph space coordinates. If not set the node will be added and the layout will be recomputed.

None
data dict[str, Any] | None

Optional dictionary of node attributes, see ConsoleGraph.add_node (optional).

None

Returns:

Type Description
None

None

update_node

update_node(
    node: Hashable,
    position: Offset | None = None,
    data: dict[str, Any] | None = None,
    update_data: bool = True,
) -> None

Updates a node in the graph and reflects the changes in the console graph.

See ConsoleGraph.update_node for comparison.

Parameters:

Name Type Description Default
node Hashable

The node to update.

required
position Offset | None

The new position of the node. Defaults to None.

None
data dict[str, Any] | None

The new data to associate with the node. Defaults to None.

None
update_data bool

Whether to merge the data associated with the node. Defaults to True.

True

remove_node

remove_node(node: Hashable) -> None

Remove a node from the graph and updates the console graph and the internal graph.

Parameters:

Name Type Description Default
node Hashable

The node to remove from the graph.

required

Returns:

Type Description
None

None

add_edge

add_edge(
    u: Hashable,
    v: Hashable,
    data: dict[str, Any] | None = None,
) -> None

Adds an edge to the graph widget.

Parameters:

Name Type Description Default
u Hashable

The source node of the edge.

required
v Hashable

The destination node of the edge.

required
data dict[str, Any] | None

Optional dictionary of edge attributes, see ConsoleGraph.add_edge (optional).

None

Returns:

Type Description
None

None

update_edge

update_edge(
    u: Hashable,
    v: Hashable,
    data: dict[str, Any],
    update_data: bool = True,
    update_layout: bool = True,
) -> None

Updates the edge between nodes u and v with the given data.

See ConsoleGraph.update_edge for comparison.

Parameters:

Name Type Description Default
u Hashable

The source node of the edge.

required
v Hashable

The destination node of the edge.

required
data dict[str, Any]

The data to update the edge with.

required
update_data bool

Whether to merge the data of the edge. Defaults to True.

True
update_layout bool

Accepted for API compatibility. The edge is currently rerendered whether this is true or false.

True

Returns:

Type Description
None

None

remove_edge

remove_edge(u: Hashable, v: Hashable) -> None

Removes an edge from the graph and updates the console graph and the internal graph.

Parameters:

Name Type Description Default
u Hashable

The source node of the edge.

required
v Hashable

The destination node of the edge.

required

Returns:

Type Description
None

None

attach_widget_to_node

attach_widget_to_node(
    widget: Widget, node: Hashable, size: Size | None = None
) -> None

Attaches a widget to a node in the console graph.

Parameters:

Name Type Description Default
widget Widget

The textual widget to attach.

required
node Hashable

The node to attach the widget to.

required
size Size | None

The size of the widget, by default uses the node size (optional).

None

detach_widget_from_node

detach_widget_from_node(node: Hashable) -> None

Detach a widget from a node.

Parameters:

Name Type Description Default
node Hashable

The node to detach the widget from.

required

Returns:

Type Description
None

None

to_graph_coordinates

to_graph_coordinates(p: Point | Offset) -> FloatPoint

Converts a point or offset to graph coordinates.

Parameters:

Name Type Description Default
p Point | Offset

The point or offset to convert.

required

Returns:

Name Type Description
FloatPoint FloatPoint

The converted point in graph coordinates.

view_to_widget_coordinates

view_to_widget_coordinates(p: Point) -> Offset

Converts a point in the view's coordinate system to a point in the widget's coordinate system.

Parameters:

Name Type Description Default
p Point

The point to convert.

required

Returns:

Name Type Description
Offset Offset

The converted point in the widget's coordinate system.

graph_to_widget_coordinates

graph_to_widget_coordinates(p: FloatPoint) -> Offset

Converts a point in graph coordinates to widget coordinates.

Parameters:

Name Type Description Default
p FloatPoint

The point in graph coordinates.

required

Returns:

Name Type Description
Offset Offset

The point in widget coordinates.

widget_to_view_coordinates

widget_to_view_coordinates(offset: Offset) -> Point

Converts widget coordinates to view coordinates.

Parameters:

Name Type Description Default
offset Offset

The offset to convert.

required

Returns:

Name Type Description
Point Point

The converted point.