drawpyo Documentation
repository·main·Indexed 19 days ago
https://github.com/merrimanind/drawpyoA Python library for programmatically generating Diagrams.net/Draw.io charts. It enables developers to create, style, and position diagram objects, write them to XML-based .drawio files, and load existing diagrams for modification. The library supports basic object placement, external shape library registration, and high-level diagram types with automated layout, including TreeDiagram, BarChart, PieChart, and BinaryTreeDiagram.
What's inside drawpyo
- drawpyo is a Python library designed for the programmatic generation of Draw.io charts. It allows developers to create diagram objects, place and style elements, and export the resulting diagrams to files. This is particularly useful for versioning diagrams alongside code in a repository, as Draw.io files are XML-based and plaintext-compatible.
Create basic diagrams
mainDrawpyo's core functionality mirrors the Draw.io app. You can:
- Create files containing one or more pages.
- Add objects (shapes, containers, or edges) to pages.
- Position objects within the diagram.
- Style objects using built-in shape libraries, manual styling, or style strings.
- Save diagrams in formats compatible with the Draw.io app.
For detailed implementation steps, refer to the
Basic Diagrams - Usageguide.Understand the color resolution hierarchy
mainDrawpyo resolves colors using a specific hierarchy. When an object needs a color, it checks sources in this order of priority:
- Object-specific colors: Colors set directly on the object (e.g.,
fill_color,stroke_color, orfontColorviatext_format) have the highest priority and override everything else. - Color Scheme: If no object-specific colors are set, Drawpyo uses the values defined in the object's assigned
ColorScheme. - Defaults: If neither an object-specific color nor a color scheme provides a value, Drawpyo falls back to internal defaults (e.g., Draw.io defaults).
Hierarchy Summary:
Object-specific colors>Color scheme colors>Default colors- Object-specific colors: Colors set directly on the object (e.g.,
Create and manage ColorScheme objects
mainA
ColorSchemeis a reusable object that groupsfill_color,stroke_color, andfont_color. Each color value in a scheme can beNone, a hex string (format#RRGGBB), or aStandardColorenum value. Invalid hex strings will raise aValueError.from drawpyo import ColorScheme, StandardColor scheme = ColorScheme( fill_color=StandardColor.BLUE5, stroke_color="#FF0000", ) # You can also update components after creation scheme.set_fill_color("#ABCDEF") scheme.set_stroke_color(StandardColor.GRAY7) scheme.set_gradient(None)Style edge labels using TextFormat
mainStyling for an edge's label is managed via aTextFormatobject. You can access and configure these parameters through theEdge.text_formatattribute. For detailed text styling options (like font, size, etc.), refer to theTextFormatdocumentation.How Binary Tree Diagrams work
mainBinary trees in
drawpyoare managed using two specialized classes:BinaryTreeDiagramandBinaryNodeObject.BinaryTreeDiagram: ExtendsTreeDiagramto provide binary-friendly layout defaults and helper methods. It manages the overall structure and layout.BinaryNodeObject: A subclass ofNodeObjectthat enforces the strict binary tree rule (at most two children). It provides dedicatedleftandrightproperties to manage child slots.
This pairing ensures that the tree structure remains valid (no node has more than two children) and provides intuitive ways to navigate and link nodes.
from drawpyo.diagram_types import BinaryTreeDiagram, BinaryNodeObject tree = BinaryTreeDiagram(file_path="path/to/diagram", file_name="Binary Tree.drawio") root = BinaryNodeObject(tree=tree, value="Root")Configure autosizing for parent containers
mainYou can make a parent object automatically expand or contract to fit its children. This behavior is disabled by default.
To enable it, use the following parameters:
autosize_to_children: Set toTrueto allow the parent to expand to fit its contents.autosize_margin: An integer defining the margin (in pixels) to maintain around child objects. Note that this margin is inclusive of the container's title block.autocontract: Set toTrueif you want the parent to also contract when children are removed or resized. IfFalse, the parent will expand but never shrink.
Manual Triggering: You can manually trigger the autofit logic at any time by calling the
resize_to_children()method on the object. This method respects the configuredautosize_marginandautocontractsettings.# Example configuration parent_container = drawpyo.diagram.Object( autosize_to_children=True, autosize_margin=20, autocontract=True, page=page ) # Manually trigger resize parent_container.resize_to_children()Rules and constraints for BinaryNodeObject
mainThe
BinaryNodeObjectenforces several structural guarantees:- Child Slot Normalization: Nodes always maintain exactly two slots:
[left, right]. If you provide only one child during creation (e.g.,BinaryNodeObject(tree_children=[child])), the second slot is automatically set toNone([child, None]). - Strict Child Limit: Providing more than two children to
tree_children(e.g.,BinaryNodeObject(tree_children=[a, b, c])) will raise aValueError. - Parent Safety: A node cannot have more than one parent, and it cannot occupy both the
leftandrightslots of a parent at the same time. Assigning a node to a new parent automatically detaches it from its old one.
- Child Slot Normalization: Nodes always maintain exactly two slots:
Navigate nested objects and hierarchies
mainDrawpyo preserves the parent-child relationships used by Draw.io containers, swimlanes, and groups. You can navigate the hierarchy by accessing the
.objectsattribute of a container object, which returns its direct children. Drawpyo applies absolute positioning recursively, so child coordinates are relative to their parent.# Access a parent container parent_container = diagram.get_by_id("2292288764992") # List all child objects for child in parent_container.objects: print(f"- {child.value} at ({child.geometry.x}, {child.geometry.y})")What are Extended Objects in drawpyo
mainExtended objects are specialized classes that inherit from the baseObjectclass. They provide convenience for complex Draw.io objects that are actually small groups of objects (like a container with multiple children). While they offer more functionality than a standardObject, they are not as complex as full custom diagram types. Currently, theListis the primary extended object implemented.Configure edge color, shading, and effects
mainYou can customize the visual appearance of an edge using color, shading, and boolean effects.
Color and Shading
Parameter Effect opacityThe opacity of the edge (0-100) strokeColorThe color of the edge or the stroke around the edge shape ('default', 'none', or a hex color code) strokeWidthThe width of the edge or the stroke around the edge shape (1-999) fillColorThe fill color of the edge shape ('default', 'none', or a hex color code) Effects
Set these boolean parameters to enable specific visual effects:
roundedshadowsketchflowAnimation(animates in Draw.io)
Customize Pie Chart slice labels
mainBy default,
PieChartlabels slices using the format:"{label}: {percentage:.1f}%".You can override this behavior by providing a
label_formattercallable to thePieChartconstructor. The callback must accept three arguments:category(the string label),value(the numerical value), andtotal(the sum of all values in the dataset).def label_formatter(category: str, value: float, total: float) -> str: return f"{category}: {value/total*100:.1f}%" chart = PieChart(data, label_formatter=label_formatter)