Skip to main content

A Python library for programmatically generating Draw.io charts.

Project description

drawpyo

Drawpyo is a Python library for programmatically generating Diagrams.net/Draw.io charts. It enables creating a diagram object, placing and styling objects, then writing the object to a file.

History/Justification

I love Draw.io! Compared to expensive and heavy commercial options like Visio and Miro, Draw.io's free and lightweight app allows wider and more universal distribution of diagrams. Because the files are stored in plaintext they can be versioned alongside code in a repository as documentation. The XML-based file format makes these diagrams semi-portable, and could easily be ported to other applications if Draw.io ever failed you. For these reason, I think it's one of the best options for documentation diagrams.

When I had a need to generate heirarchical tree diagrams of requirement structures I was surprised to find there wasn't even a single existing Python library for working with these files. I took the project home and spent a weekend building the initial functionality. I've been adding functionality, robustness, and documentation intermittently since.

Full Documentation

Available here!

https://merrimanind.github.io/drawpyo/

Basic Usage

The basic mode of interacting with drawpyo is to manually create, style, and place objects just like you would using the Draw.io UI. There are a number of ways to style objects and you can write your own functionality for automatically handling style or placement.

Make a new file

import drawpyo
file = drawpyo.File()
file.file_path = r"C:\drawpyo"
file.file_name = "Test Generated Edges.drawio"
# Add a page
page = drawpyo.Page(file=file)

Add an object

item = drawpyo.diagram.Object(page=page, value="new object")
item.position = (0, 0)

Create an object from the base style libraries available in the Draw.io UI

item_from_lib = drawpyo.diagram.object_from_library(
    page=page,
    library="general",
    obj_name="process",
    value="New Process",
    )

Style an object from a string

item_from_stylestr = drawpyo.diagram.Object(page=page)
item_from_stylestr.apply_style_string("rounded=1;whiteSpace=wrap;html=1;fillColor=#6a00ff;fontColor=#ffffff;strokeColor=#000000;gradientColor=#FF33FF;strokeWidth=4;")

Write the file

file.write()

Usage with a diagram type

There is also functionality available in drawpyo that extends what can be done in Draw.io's app! These diagram types allow for easy and automatic creation of specific diagrams.

The only diagram type that's released is the tree diagram. Varying level of conceptual work has been started for:

  • Automatic class/object/inheritance diagrams of a python module

  • Flowcharts

  • Process diagrams

Working with TreeDiagrams

Create a new tree diagram:

from drawpyo.diagram_types import TreeDiagram, NodeObject

tree = TreeDiagram(
    file_path = path.join(path.expanduser('~'), "Test Drawpyo Charts"),
    file_name = "Coffee Grinders.drawio",
    direction = "down",
    link_style = "orthogonal",
    )

The direction property sets which way the leaf nodes grow from the root: up, down, left, or right. The link_style can be orthogonal, straight, or curved.

Create some NodeObjects:

# Top object
grinders = NodeObject(tree=tree, value="Appliances for Grinding Coffee", base_style="rounded rectangle")

# Main categories
blade_grinders = NodeObject(tree=tree, value="Blade Grinders", tree_parent=grinders)
burr_grinders = NodeObject(tree=tree, value="Burr Grinders", tree_parent=grinders)
blunt_objects = NodeObject(tree=tree, value="Blunt Objects", tree_parent=grinders)

Note that the base_style was manually declared for the first object. But NodeObjects will default to "rounded rectangle" so it's not necessary for every one. Any NodeObject can be a parent, so you can keep adding objects down the tree:

# Other
elec_blade = NodeObject(tree=tree, value="Electric Blade Grinder", tree_parent=blade_grinders)
mnp = NodeObject(tree=tree, value="Mortar and Pestle", tree_parent=blunt_objects)

# Conical Burrs
conical = NodeObject(tree=tree, value="Conical Burrs", tree_parent=burr_grinders)
elec_conical = NodeObject(tree=tree, value="Electric", tree_parent=conical)
manual_conical = NodeObject(tree=tree, value="Manual", tree_parent=conical)

Important Note: TreeDiagrams do not currently support NodeObjects with multiple parents! It may not ever as this seriously complicates the auto layout process. However, you can add links between any two objects in the tree and render them in the diagram. They just may look ugly until you manually rearrange the diagram.

Finally, before writing the diagram you'll want to run the magic penultimate step: auto layout.

tree.auto_layout()
tree.write()

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

drawpyo-0.2.1.tar.gz (29.5 kB view details)

Uploaded Source

Built Distribution

drawpyo-0.2.1-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

Details for the file drawpyo-0.2.1.tar.gz.

File metadata

  • Download URL: drawpyo-0.2.1.tar.gz
  • Upload date:
  • Size: 29.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.9

File hashes

Hashes for drawpyo-0.2.1.tar.gz
Algorithm Hash digest
SHA256 cc2f147d4c10832600019cb276b2445c3dd32a96fbabf2951dbf7155a1f7a956
MD5 87e8ca1e70d0468b08e84f9eda986e4d
BLAKE2b-256 133cdf9adf68c06c13109efa31721042920ebe8fcd283ba661161d1a0d1dc63e

See more details on using hashes here.

File details

Details for the file drawpyo-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: drawpyo-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 33.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.9

File hashes

Hashes for drawpyo-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 230d508c6ad730076d345e5852b10370b316b437e063112f9e69ae10719b1330
MD5 7fb6c9f32d5ae80e14eb1c4609d31dff
BLAKE2b-256 6fcaa8d2ff5e216a2c568e6e9cfbc22a9e9086ad8bd02e5eae51c461acb7ceb9

See more details on using hashes here.

Supported by

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