Python PlantUML Client

0.3.0 · active · verified Thu Apr 16

The `plantuml` Python library provides a client to generate PlantUML diagrams from Python code. It can interact with a local PlantUML JAR file (requiring Java) or a remote PlantUML server (including the public one). The current version is 0.3.0, with updates occurring infrequently but the project is actively maintained for compatibility and minor enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the PlantUML client and generate a basic PNG diagram using the default public PlantUML server. The generated image data is then saved to a file.

from plantuml import PlantUML

# Initialize PlantUML client.
# By default, it uses the public server: http://www.plantuml.com/plantuml
# For a local server, provide the jar_path or custom url:
# pu = PlantUML(jar_path='/path/to/plantuml.jar')
# pu = PlantUML(url='http://localhost:8080/plantuml')
pu = PlantUML() # Uses the default public server

puml_code = """
@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
Alice -> Bob: Another authentication Request
Alice <-- Bob: Another authentication Response
@enduml
"""

try:
    # Generate the diagram image data (PNG by default)
    image_data = pu.processes(puml_code)

    # Define the output filename
    output_filename = "quickstart_diagram.png"

    # Save the image data to a file
    with open(output_filename, "wb") as f:
        f.write(image_data)

    print(f"Successfully generated PlantUML diagram to {output_filename}")

except Exception as e:
    print(f"An error occurred during diagram generation: {e}")
    print("Please ensure you have an internet connection if using the default server,")
    print("or that your local PlantUML setup (Java + JAR) is correct.")

view raw JSON →