Skip to content

Console Graph

G module-attribute

G = TypeVar('G', Graph, DiGraph)

ZoomSpec dataclass

x instance-attribute

x

Scaling along the x-axis.

y instance-attribute

y

Scaling along the y-axis.

AutoZoom

Bases: Enum

FIT class-attribute instance-attribute

FIT = 'fit'

Fit the graph into the viewport.

FIT_PROPORTIONAL class-attribute instance-attribute

FIT_PROPORTIONAL = 'fit_proportional'

Fit the graph into the viewport, but keep the aspect ratio.

ConsoleGraph

Bases: Generic[G]

full_viewport property

full_viewport

The full viewport of the graph that is spanned by the whole graph.

zoom property writable

zoom

The zoom level of the graph.

Can be set either to a float, a tuple of zoom in x and y direction or a zoom spec / auto zoom mode. Defaults to 1.0, always returns a ZoomSpec or AutoZoom, so a float or tuple is converted to a ZoomSpec.

viewport property writable

viewport

The viewport that is set by the user (or the full viewport if none is set).

max_width property writable

max_width

The maximum width of the graph in characters or None if no maximum width is set.

max_height property writable

max_height

The maximum height of the graph in characters as integer or None if no maximum height is set.

__init__

__init__(graph, layout_engine=GrandalfSugiyamaLayout[G](), console=Console(), viewport=None, max_width=None, max_height=None, zoom=1.0)

A console representation of a networkx graph.

The class conforms to the rich console protocol and can be printed as any other rich renderable. You can pass a layout engine and a specific console that will be used for rendering.

Rendering of nodes and edges happens on object creation and the object size is determined by the graph (no reactive rendering).

Parameters:

Name Type Description Default
graph G

A networkx graph object (see networkx.Graph or networkx.DiGraph).

required
layout_engine LayoutEngine[G]

The layout engine used.

GrandalfSugiyamaLayout[G]()
console Console

The rich console driver used to render.

Console()
viewport Region

The viewport to render. Defaults to the whole graph.

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

The zoom level, either a float, a tuple of zoom in x and y direction or a zoom spec / auto zoom mode. Defaults to 1.0.

1.0

reset_viewport

reset_viewport()

add_node

add_node(node, position=None, data=None)

Add a node to the console graph.

Parameters:

Name Type Description Default
node Hashable

The node to add.

required
position FloatPoint | None

If set add the node at the specific position in graph space coordinates.

None
data dict[str, Any] | None

The data and attributes of the node.

None

update_node

update_node(node, position=None, data=None, update_data=True)

Update a node position or attributes (data).

Parameters:

Name Type Description Default
node Hashable

The node to update.

required
position FloatPoint | None

A new position if the node should be moved, by default None.

None
data dict[str, Any] | None

A new or updated data dictionary, by default None.

None
update_data bool

Whether to replace or update the data dictionary, by default True.

True

Returns:

Type Description
None

None

remove_node

remove_node(node)

Removes the specified node from the graph, along with any edges that are connected to it.

Parameters:

Name Type Description Default
node Hashable

The node to remove.

required

Returns:

Type Description
None

None

add_edge

add_edge(u, v, data=None)

Add an edge between existing nodes in the graph.

Parameters:

Name Type Description Default
u Hashable

The source node.

required
v Hashable

The target node.

required
data dict[str, Any] | None

The data and attributes of the edge.

None

Raises:

Type Description
ValueError

Raised if one of the edges does not exist in the graph.

update_edge

update_edge(u, v, data, update_data=True, update_layout=True)

Update edge attributes (data).

Parameters:

Name Type Description Default
u Hashable

The source node of the edge.

required
v Hashable

The target node of the edge.

required
data dict[str, Any]

The new or updated data dictionary.

required
update_data bool

Whether to replace or update the data dictionary, by default True.

True
update_layout bool

Whether to update the layout of the edge, by default True.

True

Raises:

Type Description
RuntimeError

If the zoom factor has not been computed yet.

Returns:

Type Description
None

None

remove_edge

remove_edge(u, v)

Removes an edge from the graph.

Parameters:

Name Type Description Default
u Hashable

The source node of the edge.

required
v Hashable

The target node of the edge.

required

Returns:

Type Description
None

None

to_graph_coordinates

to_graph_coordinates(p)

Converts a point from view coordinates to graph coordinates.

This means applying the inverse zoom and offset to the point.

Parameters:

Name Type Description Default
p Point

The point to convert.

required

Returns:

Name Type Description
FloatPoint FloatPoint

The converted point in graph coordinates.

to_view_coordinates

to_view_coordinates(p)

Converts a point from graph coordinates to view coordinates.

This means applying the zoom and offset to the point.

Parameters:

Name Type Description Default
p FloatPoint

The point to convert.

required

Returns:

Name Type Description
Point Point

The converted point in view coordinates.