A Python DSL inspired by Structurizr, intended for generating C4 diagrams

0.1.3 · active · verified Thu Apr 16

PyStructurizr provides a Python DSL (Domain Specific Language) inspired by Structurizr, specifically designed for generating C4 diagrams. It aims to overcome limitations of the Structurizr DSL by leveraging Python's full power and flexibility for defining architectural models. The library also includes a CLI for converting Python code to Structurizr DSL, generating SVG diagrams (via Kroki.io), or uploading to cloud storage. Its latest version is 0.1.3, released in July 2023, and it maintains a somewhat active but not rapid release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a basic C4 model with a software system, containers, and relationships using PyStructurizr's DSL. It includes a basic `Workspace`, `Model`, `Person`, `SoftwareSystem`, `Container`, and `ContainerView`. The `with` syntax is used for hierarchical structuring.

from pystructurizr.dsl import Workspace

with Workspace(name="MyArchitecture") as workspace:
    with workspace.Model(name="model") as model:
        user = model.Person("User", description="A human user.")
        
        with model.SoftwareSystem("Software System", description="My awesome software system.") as software_system:
            webapp = software_system.Container("Web Application", description="Serves the user interface.")
            db = software_system.Container("Database", technology="PostgreSQL", description="Stores data.")

        user.uses(webapp, "Uses")
        webapp.uses(db, "Reads from and writes to")
    
    # Create a view onto the model
    workspace.ContainerView(
        software_system,
        "My Container View",
        "The container view of our simple software system."
    )

    # To save the workspace as Structurizr DSL (requires Structurizr CLI or compatible tool to render)
    # workspace.save(format='dsl', path='./output') 
    # To save as SVG (requires an internet connection for Kroki.io)
    # workspace.save(format='svg', path='./output')
    
    print("Workspace 'MyArchitecture' created successfully. You can extend it or use the CLI to save/generate diagrams.")

view raw JSON →