Home / Class/ PngDrawer Class — langchain Architecture

PngDrawer Class — langchain Architecture

Architecture documentation for the PngDrawer class in graph_png.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  c9382098_d87a_624c_2283_8ccc4d86a51e["PngDrawer"]
  7df647ad_c83b_a340_6b52_55758b436999["graph_png.py"]
  c9382098_d87a_624c_2283_8ccc4d86a51e -->|defined in| 7df647ad_c83b_a340_6b52_55758b436999
  23ace460_5419_fe64_759e_fe8d9ec357c2["__init__()"]
  c9382098_d87a_624c_2283_8ccc4d86a51e -->|method| 23ace460_5419_fe64_759e_fe8d9ec357c2
  f662567f_3d78_af4e_8634_2e273fff6703["get_node_label()"]
  c9382098_d87a_624c_2283_8ccc4d86a51e -->|method| f662567f_3d78_af4e_8634_2e273fff6703
  d7193855_726f_e279_29af_238a2d271daa["get_edge_label()"]
  c9382098_d87a_624c_2283_8ccc4d86a51e -->|method| d7193855_726f_e279_29af_238a2d271daa
  f36cd01a_ee4a_456e_5602_fdd5654d92b3["add_node()"]
  c9382098_d87a_624c_2283_8ccc4d86a51e -->|method| f36cd01a_ee4a_456e_5602_fdd5654d92b3
  2be6db79_8136_7d0b_f51b_d6a545a8e87e["add_edge()"]
  c9382098_d87a_624c_2283_8ccc4d86a51e -->|method| 2be6db79_8136_7d0b_f51b_d6a545a8e87e
  6330b7ee_29e7_61d7_1c2a_17c28e09b333["draw()"]
  c9382098_d87a_624c_2283_8ccc4d86a51e -->|method| 6330b7ee_29e7_61d7_1c2a_17c28e09b333
  5e47cff4_feec_a39b_186c_adefa4ff8e20["add_nodes()"]
  c9382098_d87a_624c_2283_8ccc4d86a51e -->|method| 5e47cff4_feec_a39b_186c_adefa4ff8e20
  eccfda41_8f99_f1c7_32b2_3c33e1b785ef["add_subgraph()"]
  c9382098_d87a_624c_2283_8ccc4d86a51e -->|method| eccfda41_8f99_f1c7_32b2_3c33e1b785ef
  7d1d27fa_5e17_8d67_8f76_7c44866cb910["add_edges()"]
  c9382098_d87a_624c_2283_8ccc4d86a51e -->|method| 7d1d27fa_5e17_8d67_8f76_7c44866cb910
  a3024aca_13f2_7a70_1127_d25affda4b00["update_styles()"]
  c9382098_d87a_624c_2283_8ccc4d86a51e -->|method| a3024aca_13f2_7a70_1127_d25affda4b00

Relationship Graph

Source Code

libs/core/langchain_core/runnables/graph_png.py lines 16–215

class PngDrawer:
    """Helper class to draw a state graph into a PNG file.

    It requires `graphviz` and `pygraphviz` to be installed.

    Example:
        ```python
        drawer = PngDrawer()
        drawer.draw(state_graph, "graph.png")
        ```
    """

    def __init__(
        self, fontname: str | None = None, labels: LabelsDict | None = None
    ) -> None:
        """Initializes the PNG drawer.

        Args:
            fontname: The font to use for the labels. Defaults to "arial".
            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.

        """
        self.fontname = fontname or "arial"
        self.labels = labels or LabelsDict(nodes={}, edges={})

    def get_node_label(self, label: str) -> str:
        """Returns the label to use for a node.

        Args:
            label: The original label.

        Returns:
            The new label.
        """
        label = self.labels.get("nodes", {}).get(label, label)
        return f"<<B>{label}</B>>"

    def get_edge_label(self, label: str) -> str:
        """Returns the label to use for an edge.

        Args:
            label: The original label.

        Returns:
            The new label.
        """
        label = self.labels.get("edges", {}).get(label, label)
        return f"<<U>{label}</U>>"

    def add_node(self, viz: Any, node: str) -> None:
        """Adds a node to the graph.

        Args:
            viz: The graphviz object.
            node: The node to add.
        """
        viz.add_node(
            node,
            label=self.get_node_label(node),
            style="filled",
            fillcolor="yellow",
            fontsize=15,
            fontname=self.fontname,
        )

    def add_edge(
        self,
        viz: Any,

Frequently Asked Questions

What is the PngDrawer class?
PngDrawer is a class in the langchain codebase, defined in libs/core/langchain_core/runnables/graph_png.py.
Where is PngDrawer defined?
PngDrawer is defined in libs/core/langchain_core/runnables/graph_png.py at line 16.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free