langchain_core.runnables.graph_png.PngDrawer

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

辅助类,用于将状态图绘制到PNG文件中。

需要安装graphviz和pygraphviz。 :param fontname: 用于标签的字体 :param labels: 标签覆盖的字典。该字典应具有以下格式:

{
“nodes”: {

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

}, “edges”: {

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

}

} 键是原始标签,值是新标签。

用法:

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

初始化PNG绘图器。

参数:

fontname:用于标签的字体 labels:标签覆盖的字典。该字典应具有以下格式:

{
“nodes”: {

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

}, “edges”: {

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

}

} 键是原始标签,值是新标签。

Methods

__init__([fontname, labels])

初始化PNG绘图器。

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

将边添加到图中。

add_edges(viz, graph)

add_node(viz, node)

将一个节点添加到图中。

add_nodes(viz, graph)

draw(graph[, output_path])

将给定的状态图绘制成一个PNG文件。

get_edge_label(label)

返回用于边的标签。

get_node_label(label)

返回用于节点的标签。

update_styles(viz, graph)

Parameters
  • fontname (Optional[str]) –

  • labels (Optional[LabelsDict]) –

Return type

None

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

初始化PNG绘图器。

参数:

fontname:用于标签的字体 labels:标签覆盖的字典。该字典应具有以下格式:

{
“nodes”: {

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

}, “edges”: {

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

}

} 键是原始标签,值是新标签。

Parameters
  • fontname (Optional[str]) –

  • labels (Optional[LabelsDict]) –

Return type

None

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

将边添加到图中。

参数:

viz:graphviz对象 source:源节点 target:目标节点 label:边的标签。默认为None。 conditional:边是否是有条件的。默认为False。

返回:

None

Parameters
  • viz (Any) –

  • source (str) –

  • target (str) –

  • label (Optional[str]) –

  • conditional (bool) –

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]

将一个节点添加到图中。

参数:

viz: graphviz对象 node: 要添加的节点

返回:

Parameters
  • viz (Any) –

  • node (str) –

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]

将给定的状态图绘制成一个PNG文件。

需要安装graphviz和pygraphviz。 :param graph: 要绘制的图 :param output_path: 保存PNG文件的路径。如果为None,则返回PNG字节。

Parameters
  • graph (Graph) –

  • output_path (Optional[str]) –

Return type

Optional[bytes]

get_edge_label(label: str) str[source]

返回用于边的标签。

参数:

label:原始标签

返回:

新标签。

Parameters

label (str) –

Return type

str

get_node_label(label: str) str[source]

返回用于节点的标签。

参数:

label:原始标签

返回:

新标签。

Parameters

label (str) –

Return type

str

update_styles(viz: Any, graph: Graph) None[source]
Parameters
  • viz (Any) –

  • graph (Graph) –

Return type

None