Skip to content

Structure visualizer

StructureVisualizer

Utility class to visualize a Structure structure

Source code in griptape/utils/structure_visualizer.py
@define
class StructureVisualizer:
    """Utility class to visualize a Structure structure"""

    structure: Structure = field()
    header: str = field(default="graph TD;", kw_only=True)

    def to_url(self) -> str:
        """Generates a url that renders the Workflow structure as a Mermaid flowchart
        Reference: https://mermaid.js.org/ecosystem/tutorials#jupyter-integration-with-mermaid-js

        Returns:
            str: URL to the rendered image
        """
        self.structure.resolve_relationships()

        tasks = "\n\t" + "\n\t".join([self.__render_task(task) for task in self.structure.tasks])
        graph = f"{self.header}{tasks}"

        graph_bytes = graph.encode("utf-8")
        base64_string = base64.b64encode(graph_bytes).decode("utf-8")

        url = f"https://mermaid.ink/svg/{base64_string}"

        return url

    def __render_task(self, task: BaseTask) -> str:
        if task.children:
            return f'{task.id}--> {" & ".join([child.id for child in task.children])};'
        else:
            return f"{task.id};"

header: str = field(default='graph TD;', kw_only=True) class-attribute instance-attribute

structure: Structure = field() class-attribute instance-attribute

__render_task(task)

Source code in griptape/utils/structure_visualizer.py
def __render_task(self, task: BaseTask) -> str:
    if task.children:
        return f'{task.id}--> {" & ".join([child.id for child in task.children])};'
    else:
        return f"{task.id};"

to_url()

Generates a url that renders the Workflow structure as a Mermaid flowchart Reference: https://mermaid.js.org/ecosystem/tutorials#jupyter-integration-with-mermaid-js

Returns:

Name Type Description
str str

URL to the rendered image

Source code in griptape/utils/structure_visualizer.py
def to_url(self) -> str:
    """Generates a url that renders the Workflow structure as a Mermaid flowchart
    Reference: https://mermaid.js.org/ecosystem/tutorials#jupyter-integration-with-mermaid-js

    Returns:
        str: URL to the rendered image
    """
    self.structure.resolve_relationships()

    tasks = "\n\t" + "\n\t".join([self.__render_task(task) for task in self.structure.tasks])
    graph = f"{self.header}{tasks}"

    graph_bytes = graph.encode("utf-8")
    base64_string = base64.b64encode(graph_bytes).decode("utf-8")

    url = f"https://mermaid.ink/svg/{base64_string}"

    return url