Skip to content

Console Graph

ZoomSpec dataclass

ZoomSpec(x: float, y: float)

x instance-attribute

x: float

Scaling along the x-axis.

y instance-attribute

y: float

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

ConsoleGraph(
    graph: DiGraph,
    layout_engine: core.LayoutEngine = core.SugiyamaLayout(
        core.LayoutDirection.TOP_DOWN
    ),
    console: Console = Console(),
    viewport: Region | None = None,
    max_width: int | None = None,
    max_height: int | None = None,
    zoom: float
    | tuple[float, float]
    | ZoomSpec
    | AutoZoom = 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 digraph object (networkx.DiGraph).

required
layout_engine LayoutEngine[G]

The layout engine used.

SugiyamaLayout(TOP_DOWN)
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

full_viewport property

full_viewport: Region

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

zoom property writable

zoom: ZoomSpec | AutoZoom

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: Region

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

max_width property writable

max_width: int | None

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

max_height property writable

max_height: int | None

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

reset_viewport

reset_viewport() -> None

add_node

add_node(
    node: Hashable,
    position: FloatPoint | None = None,
    data: dict[str, Any] | None = None,
) -> 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: Hashable,
    position: FloatPoint | None = None,
    data: dict[str, Any] | None = None,
    update_data: bool = True,
) -> None

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: Hashable) -> None

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: Hashable,
    v: Hashable,
    data: dict[str, Any] | None = None,
) -> 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: Hashable,
    v: Hashable,
    data: dict[str, Any],
    update_data: bool = True,
    update_layout: bool = True,
) -> None

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: Hashable, v: Hashable) -> None

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: Point) -> FloatPoint

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: FloatPoint) -> Point

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.