langchain_core.runnables.graph_png.PngDrawer

class langchain_core.runnables.graph_png.PngDrawer(fontname: Optional[str] = None, labels: Optional[LabelsDict] = 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 (Optional[str]) – The font to use for the labels

  • labels (Optional[LabelsDict]) –

    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.

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_node(viz, node)

Adds a node to the graph.

add_nodes(viz, 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)

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

Initializes the PNG drawer.

Parameters
  • fontname (Optional[str]) – The font to use for the labels

  • labels (Optional[LabelsDict]) –

    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.

Return type

None

add_edge(viz: Any, source: str, target: str, label: Optional[str] = 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 (Optional[str]) – 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]
Parameters
  • viz (Any) –

  • graph (Graph) –

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]
Parameters
  • viz (Any) –

  • graph (Graph) –

Return type

None

draw(graph: Graph, output_path: Optional[str] = None) Optional[bytes][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 (Optional[str]) –

Return type

Optional[bytes]

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]
Parameters
  • viz (Any) –

  • graph (Graph) –

Return type

None