Skip to main content

Pydot _flow_ makes it a little easier to assemble simple flowing graphs using Pydot.

Project description

Pydot flow

PyPI version Python versions License: MIT

A flowing application programming interface for creating Graphviz visualizations using Pydot.

Introduction

Pydot flow makes it a little easier to assemble simple flowing graphs using Pydot.

You can, for example, use flow to construct a graph programmatically at each step of a data transformation pipeline - visually documenting a data transformation.

Example

In this hypothetical copy-and-paste example a hypothetical graph is constructed using the flow API.

Instructions

  1. Import dependencies.
  2. Define a function for creating a random color code.
  3. Create a Pydot flow Chart.
  4. Set Node defaults.
  5. Create the first Node and name it Node 1.
  6. Flow south from Node 1 to Node 2a.
  7. Flow east from Node 2a to Node 2b; step into a new subgraph with rank set to same.
  8. Flow south from Node 2b to Node 3c; step back into the parent graph - break rank.
  9. Return to Node 2a and flow south from Node 2a to Node 3a.
  10. Return again to Node 2a and flow south from Node 2a to Node 3b.
  11. Save the graph image to a file.
  12. Output an image in a Jupyter Notebook.

Implementation

# 0. Import dependencies.
import random
import pydot
from pydot_flow import Chart
from IPython.display import Image


# 1. Define a function for creating a random color code.
def random_color():
    "Generate a random color."
    return "#" + hex(random.randint(0, 0xFFFFFF))[2:].rjust(6, "0")


# 2. Create a Pydot flow Chart.
chart = Chart(
    rankdir="TB", splines="false", label="Chart", labelloc="t", fontname="Sans", pad=0.2
)

# 3. Set Node defaults.
chart.get_graph().set_node_defaults(style="filled")

# 4. Create the first Node and name it Node 1.
node_1 = chart.create_node(src_node_attrs={"label": "Node 1", "color": random_color()})

# 5. Flow south from Node 1 to Node 2a.
node_2a = node_1.flow(
    src_port="s",
    dst_node_attrs={"label": "Node 2a", "shape": "diamond", "color": random_color()},
    edge_attrs={"label": "Edge 1-2"},
)

# 6. Flow east from Node 2a to Node 2b; step into a new subgraph with rank set to same.
node_2b = node_2a.flow(
    src_port="e",
    dst_node_attrs={"label": "Node 2b", "color": random_color()},
    edge_attrs={"label": "Edge 2a-2b", "minlen": 2},
    graph=pydot.Subgraph(rank="same"),
)

# 7. Flow south from Node 2b to Node 3c; step back into the parent graph - break rank.
node_3c = node_2b.flow(
    src_port="s",
    dst_node_attrs={"label": "Node 3c", "color": random_color()},
    edge_attrs={"label": "Edge 2b-3c", "minlen": 2},
    graph=chart.get_graph(),
)

# 8. Return to Node 2a and flow south from Node 2a to Node 3a.
node_3a = node_2a.flow(
    src_port="s",
    dst_node_attrs={"label": "Node 3a", "shape": "box", "color": random_color()},
    edge_attrs={"label": "Edge 2a-3a", "minlen": 2, "labeldistance": 2},
)

# 9. Return again to Node 2a and flow south from Node 2a to Node 3b.
node_3b = node_2a.flow(
    src_port="s",
    dst_node_attrs={"label": "Node 3b", "shape": "box", "color": random_color()},
    edge_attrs={"label": "Edge 2a-3b", "minlen": 2},
)

# 10. Save the graph image to a file.
chart.get_graph().write_png("output.png")

# 11. Output an image in a Jupyter Notebook.
Image(chart.get_graph().create_png())

Output

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydot_flow-0.3.0.tar.gz (126.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydot_flow-0.3.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file pydot_flow-0.3.0.tar.gz.

File metadata

  • Download URL: pydot_flow-0.3.0.tar.gz
  • Upload date:
  • Size: 126.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for pydot_flow-0.3.0.tar.gz
Algorithm Hash digest
SHA256 037fb267352ea3d35e0339839a6d097fbf59d20eab795a3ae3dc352f7a9f2642
MD5 762c3ce0cbbe48ee1cd53d7fd268034b
BLAKE2b-256 94541a5df3ab91608eefa1796ebdde0ffb61954863e8f682d896db3f54f3dfee

See more details on using hashes here.

File details

Details for the file pydot_flow-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pydot_flow-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for pydot_flow-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c8528c0c6791941525f61dcefc18b8ada347a623c75fddf5d102b48aba9c4b4
MD5 3c4bc28063294cd457a0742c56b61855
BLAKE2b-256 6cc79d1d5e54ec53bfcc3e6d4bb1b90624f8e92ecc1d4da81f893cdfa8a15447

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page