PngDrawer#

class langchain_core.runnables.graph_png.PngDrawer(fontname: str | None = None, labels: LabelsDict | None = None)[source]#

Helper class to draw a state graph into a PNG file.

It requires graphviz and pygraphviz to be installed. :param fontname: The font to use for the labels :param labels: A dictionary of label overrides. The dictionary

should have the following format: {

“nodes”: {

“node1”: “CustomLabel1”, “node2”: “CustomLabel2”, “__end__”: “End Node”

}, “edges”: {

“continue”: “ContinueLabel”, “end”: “EndLabel”

}

} The keys are the original labels, and the values are the new labels.

Usage:

drawer = PngDrawer() drawer.draw(state_graph, ‘graph.png’)

Initializes the PNG drawer.

Parameters:
  • fontname (str | None) – The font to use for the labels. Defaults to “arial”.

  • labels (LabelsDict | None) –

    A dictionary of label overrides. The dictionary should have the following format: {

    ”nodes”: {

    “node1”: “CustomLabel1”, “node2”: “CustomLabel2”, “__end__”: “End Node”

    }, “edges”: {

    ”continue”: “ContinueLabel”, “end”: “EndLabel”

    }

    } The keys are the original labels, and the values are the new labels. Defaults to None.

Methods

__init__([fontname, labels])

Initializes the PNG drawer.

add_edge(viz, source, target[, label, ...])

Adds an edge to the graph.

add_edges(viz, graph)

Add edges to the graph.

add_node(viz, node)

Adds a node to the graph.

add_nodes(viz, graph)

Add nodes to the graph.

draw(graph[, output_path])

Draw the given state graph into a PNG file.

get_edge_label(label)

Returns the label to use for an edge.

get_node_label(label)

Returns the label to use for a node.

update_styles(viz, graph)

Update the styles of the entrypoint and END nodes.

__init__(fontname: str | None = None, labels: LabelsDict | None = None) None[source]#

Initializes the PNG drawer.

Parameters:
  • fontname (str | None) – The font to use for the labels. Defaults to “arial”.

  • labels (LabelsDict | None) –

    A dictionary of label overrides. The dictionary should have the following format: {

    ”nodes”: {

    “node1”: “CustomLabel1”, “node2”: “CustomLabel2”, “__end__”: “End Node”

    }, “edges”: {

    ”continue”: “ContinueLabel”, “end”: “EndLabel”

    }

    } The keys are the original labels, and the values are the new labels. Defaults to None.

Return type:

None

add_edge(viz: Any, source: str, target: str, label: str | None = None, conditional: bool = False) None[source]#

Adds an edge to the graph.

Parameters:
  • viz (Any) – The graphviz object.

  • source (str) – The source node.

  • target (str) – The target node.

  • label (str | None) – The label for the edge. Defaults to None.

  • conditional (bool) – Whether the edge is conditional. Defaults to False.

Returns:

None

Return type:

None

add_edges(viz: Any, graph: Graph) None[source]#

Add edges to the graph.

Parameters:
  • viz (Any) – The graphviz object.

  • graph (Graph) – The graph to draw.

Return type:

None

add_node(viz: Any, node: str) None[source]#

Adds a node to the graph.

Parameters:
  • viz (Any) – The graphviz object.

  • node (str) – The node to add.

Returns:

None

Return type:

None

add_nodes(viz: Any, graph: Graph) None[source]#

Add nodes to the graph.

Parameters:
  • viz (Any) – The graphviz object.

  • graph (Graph) – The graph to draw.

Return type:

None

draw(graph: Graph, output_path: str | None = None) bytes | None[source]#

Draw the given state graph into a PNG file.

Requires graphviz and pygraphviz to be installed. :param graph: The graph to draw :param output_path: The path to save the PNG. If None, PNG bytes are returned.

Parameters:
  • graph (Graph)

  • output_path (str | None)

Return type:

bytes | None

get_edge_label(label: str) str[source]#

Returns the label to use for an edge.

Parameters:

label (str) – The original label.

Returns:

The new label.

Return type:

str

get_node_label(label: str) str[source]#

Returns the label to use for a node.

Parameters:

label (str) – The original label.

Returns:

The new label.

Return type:

str

update_styles(viz: Any, graph: Graph) None[source]#

Update the styles of the entrypoint and END nodes.

Parameters:
  • viz (Any) – The graphviz object.

  • graph (Graph) – The graph to draw.

Return type:

None