Skip to main content

Manipulate Graphviz graphs in Python prior to rendering

Graphviz wrapper that allows to manage graph objects in Python prior to translating them into dot language.

Rationale

Unfortunately, both graphviz and diagrams packages do not allow any operations on graphs besides adding nodes and edges. If you want to loop through all graph nodes or edges to calculate some extra attributes - you're out of luck.

This small library builds graphs as Python objects prior to translating them into dot language. That means you can manipulate your graphs programmatically in any way you want.

Usage

Basic usage

This is nothing special, diagrams offers a lot more possibilities here.

from graphviz_managed import Graph
graph = Graph(label='<<b>Sample Graph #1</b>>')
node = graph.node
foo = node(label='Foo!')
bar = node(label='BAR', fontcolor='red', penwidth=1.5)
foo >> bar >> foo
graph.render('basic.svg')

sample graph output

Change node style based on number of incoming/outgoing edges

Dynamic graph manipulation is impossible with graphviz or diagrams, but is rather straightforward with this library:

from graphviz_managed import Graph
graph = Graph(label='Highlight graph entry points', rankdir='LR')
node = graph.node

# Define a larger graph
a = node(label='a')
b = node(label='b')
c = node(label='c')
d = node(label='d')
e = node(label='e')
f = node(label='f')
a >> [b, e]
c >> [b, e]
d >> a
e >> f
f >> [b, a]

# Highlight nodes with no incoming edges
from collections import defaultdict
incoming_count = defaultdict(int)
for edge in graph.edges:
    incoming_count[edge.end] += 1
for node in graph.nodes:
    if incoming_count[node] == 0:
        node.attrs.color = 'darkgreen'
        node.attrs.fontcolor = 'darkgreen'
        node.attrs.style = 'filled'
        node.attrs.fillcolor = 'beige'

# Save output
graph.render('count.svg')

sample graph output

Break long labels into multiple lines

Providing custom factories for nodes and edges allows for some interesting customizations.

from graphviz_managed import Graph
from graphviz_managed.custom import WrapLongLabelNode
graph = Graph(node_cls=WrapLongLabelNode, rankdir='LR', node_attrs=dict(shape='box'))
a = graph.node(label='Short text')
b = graph.node(label='Long label that will be wrapped into multiple lines')
c = graph.node(label='CantWrapSpecialCamelCaseWordsWithoutSpaces')
d = graph.node(label='CantWrapSpecialCamelCaseWordsWithoutSpaces but can wrap elsewhere')
a >> b >> c >> d
graph.render('labels.svg')

sample graph output

Wrapper for diagrams package

graphviz-managed can be extended to support other graph rendering libraries. Here is how it's done for diagrams. Resulting objects may be manipulated in the same way, enjoying all pre-rendering freedoms demonstrated above.

from graphviz_managed.diagrams import Diagram
diag = Diagram(label='Fancy node templates from https://pypi.org/project/diagrams/', pad=0.1)
node = diag.node
lb = node(kind='aws.network.ELB', label='lb')
web = node(kind='aws.compute.EC2', label='web')
db = node(kind='aws.database.RDS', label='db')
store = node(kind='aws.storage.S3', label='store')
lb >> web >> db >> store
diag.render('diag.png')

sample graph output

More samples can be found in tests/ directory.

Support and contributing

If you need help with using this library or including it into your project, please create an issue. Issues are also the primary venue for reporting bugs and posting feature requests. General discussion related to this project is also acceptable and very welcome!

In case you wish to contribute code or documentation, feel free to open a pull request. That would certainly make my day!

I'm open to dialog and I promise to behave responsibly and treat all contributors with respect. Please try to do the same, and treat others the way you want to be treated.

If for some reason you'd rather not use the issue tracker, contacting me via email is OK too. Please use a descriptive subject line to enhance visibility of your message. Also please keep in mind that public discussion channels are preferable because that way many other people may benefit from reading past conversations. My email is visible under the GitHub profile and in the commit log.

Copyright 2021 Vitaly Potyarkin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Metadata

Release files for graphviz-managed 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graphviz-managed 1.0.0
File Size Uploaded Provenance
graphviz-managed-1.0.0.tar.gz 62.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for graphviz-managed 1.0.0
File Interpreter ABI Platform
graphviz_managed-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 74.9 kB

Release files / graphviz-managed-1.0.0.tar.gz

Download URL graphviz-managed-1.0.0.tar.gz
Size 62.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f41ec3850b61fe1052ed8852965f203b65f2ed91c062077682a51a4ee5f23961
BLAKE2b-256 checksum
How to use checksums
f07676879dddbf215484796037991e074cfc0cf41955d0e57dfa6ca9ce279869
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.7

Release files / graphviz_managed-1.0.0-py3-none-any.whl

Download URL graphviz_managed-1.0.0-py3-none-any.whl
Size 12.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
89b1a28da8245f5b53c3e16ddf1d54a95f750da85aebf91866c521896bebce29
BLAKE2b-256 checksum
How to use checksums
6577aabf52829af9225c23e1096d86517cb4b6d1202c0e12ad4fcb2577527bad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.7

Security for graphviz-managed 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page