rustworkx.PyGraph.to_dot#

PyGraph.to_dot(node_attr=None, edge_attr=None, graph_attr=None, filename=None)#

Generate a dot file from the graph

Parameters:
  • node_attr – A callable that will take in a node data object and return a dictionary of attributes to be associated with the node in the dot file. The key and value of this dictionary must be a string. If they’re not strings rustworkx will raise TypeError (unfortunately without an error message because of current limitations in the PyO3 type checking)

  • edge_attr – A callable that will take in an edge data object and return a dictionary of attributes to be associated with the node in the dot file. The key and value of this dictionary must be a string. If they’re not strings rustworkx will raise TypeError (unfortunately without an error message because of current limitations in the PyO3 type checking)

  • graph_attr (dict) – An optional dictionary that specifies any graph attributes for the output dot file. The key and value of this dictionary must be a string. If they’re not strings rustworkx will raise TypeError (unfortunately without an error message because of current limitations in the PyO3 type checking)

  • filename (str) – An optional path to write the dot file to if specified there is no return from the function

Returns:

A string with the dot file contents if filename is not specified.

Return type:

str

Using this method enables you to leverage graphviz to visualize a rustworkx.PyGraph object. For example:

import os
import tempfile

import pydot
from PIL import Image

import rustworkx as rx

graph = rx.undirected_gnp_random_graph(15, .25)
dot_str = graph.to_dot(
    lambda node: dict(
        color='black', fillcolor='lightblue', style='filled'))
dot = pydot.graph_from_dot_data(dot_str)[0]

with tempfile.TemporaryDirectory() as tmpdirname:
    tmp_path = os.path.join(tmpdirname, 'dag.png')
    dot.write_png(tmp_path)
    image = Image.open(tmp_path)
    os.remove(tmp_path)
image
../_images/rustworkx.PyGraph.to_dot_0_0.png