Cli
Getting Started¶
Diagrams can be defined using the Python DSL or a JSON representation.
Python Example¶
Create a diagram using the Python DSL:
# diagram.py
from c4 import Person, Rel, System, SystemContextDiagram
with SystemContextDiagram() as diagram:
user = Person("User", "System user")
backend = System("Backend API", "Main application backend")
user >> Rel("Uses HTTP API") >> backend
To render the diagram to text (by default, PlantUML source), run:
c4 render diagram.py > diagram.puml
Generated PlantUML source
@startuml
' convert it with additional command line argument -DRELATIVE_INCLUDE="relative/absolute" to use locally
!if %variable_exists("RELATIVE_INCLUDE")
!include %get_variable_value("RELATIVE_INCLUDE")/C4_Context.puml
!else
!include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Context.puml
!endif
Person(user_a467, "User", "System user")
System(backend_api_8c20, "Backend API", "Main application backend")
Rel(user_a467, backend_api_8c20, "Uses HTTP API")
@enduml
To export the diagram to a rendered artifact (by default, PNG format), run:
c4 export diagram.py > diagram.png
This generates the diagram below:
JSON Support¶
Diagrams can also be defined in JSON format.
The same diagram expressed in JSON:
{
"type": "SystemContextDiagram",
"elements": [
{
"type": "Person",
"alias": "user",
"label": "User",
"description": "System user"
},
{
"type": "System",
"alias": "app",
"label": "Backend API",
"description": "Main application backend"
}
],
"relationships": [
{
"type": "REL",
"from": "user",
"to": "app",
"label": "Uses HTTP API"
}
]
}
JSON diagrams are treated the same way as Python diagrams:
c4 render diagram.json— generate textual output (e.g. PlantUML)c4 export diagram.json— generate rendered artifacts (e.g. PNG)c4 convert diagram.json— convert to another representation (e.g. Python)
To convert a JSON diagram into the Python DSL, run:
c4 convert diagram.json --json-to-py > diagram.py
This generates:
# diagram.py
from c4 import (
Person,
Rel,
System,
SystemContextDiagram,
)
with SystemContextDiagram():
user = Person('User', 'System user', alias='user')
app = System('Backend API', 'Main application backend', alias='app')
user >> Rel('Uses HTTP API') >> app
CLI Reference¶
c4 render¶
Render a diagram to text output (by default, PlantUML source).
Usage:
c4 render [-h] [-o OUTPUT] \
[--renderer {plantuml} | --plantuml] \
[--plantuml-use-new-c4-style] \
target
Arguments:
| Name | Type | Description |
|---|---|---|
target |
string | Diagram target: Python file or module (file.py, file.py:diagram, module.path, module.path:diagram) or a JSON file (file.json). |
Options:
| Name | Type | Description | Default |
|---|---|---|---|
--renderer |
choice (plantuml) |
Renderer to use (overrides the diagram's default renderer). | plantuml |
--plantuml |
boolean | Use PlantUML renderer (alias for --renderer plantuml) |
False |
-o, --output |
path | Redirect output to a file. | stdout |
-h, --help |
boolean | Show this help message and exit. | False |
PlantUML Options:
These options apply when using the plantuml renderer.
| Name | Type | Description | Default |
|---|---|---|---|
--plantuml-use-new-c4-style |
boolean | Activates the new C4-PlantUML style. | False |
c4 export¶
Export a diagram to a rendered artifact (e.g., PNG or SVG).
The available formats depend on the selected renderer.
Note
Requires system dependencies.
Usage:
c4 export [-h] [-o OUTPUT] [-f {eps,latex,pdf,png,svg,txt,utxt}] \
[--timeout TIMEOUT] \
[--renderer {plantuml,mermaid} | --plantuml | --mermaid] \
[--plantuml-backend {local,remote}] \
[--plantuml-server-url PLANTUML_SERVER_URL] \
[--plantuml-bin PLANTUML_BIN | --plantuml-jar PLANTUML_JAR] \
[--java-bin JAVA_BIN] \
[--plantuml-skinparam-dpi PLANTUML_SKINPARAM_DPI] \
[--plantuml-use-new-c4-style] \
[--plantuml-use-bundled-c4-plantuml] \
[--mermaid-bin MERMAID_BIN] \
[--mermaid-scale-factor MERMAID_SCALE_FACTOR] \
target
Arguments:
| Name | Type | Description |
|---|---|---|
target |
string | Diagram target: Python file or module (file.py, file.py:diagram, module.path, module.path:diagram) or a JSON file (file.json). |
Options:
| Name | Type | Description | Default |
|---|---|---|---|
-f, --format |
choice (eps, latex, png, svg, txt, utxt) |
Output format (render-specific). Supported formats: plantuml: eps, latex, png, svg, txt, utxt. |
png |
--timeout |
integer | Render timeout in seconds. Can also be set via the RENDERING_TIMEOUT_SECONDS environment variable. |
30 |
--renderer |
choice (plantuml) |
Renderer to use (overrides the diagram's default renderer). | plantuml |
--plantuml |
boolean | Use PlantUML renderer (alias for --renderer plantuml). |
False |
--mermaid |
boolean | Use Mermaid renderer (alias for --renderer mermaid). |
False |
-o, --output |
path | Redirect output to a file. | stdout |
-h, --help |
boolean | Show this help message and exit. | False |
PlantUML Options:
These options apply when using the plantuml renderer.
| Name | Type | Description | Default |
|---|---|---|---|
--plantuml-backend |
choice (local, remote) |
How to run PlantUML: local execution or remote server. | local |
--plantuml-server-url |
string | PlantUML server URL. If not provided, the PLANTUML_SERVER_URL environment variable will be used. |
plantuml.com |
--plantuml-bin |
string (path or command) | PlantUML executable (command name or full path). If not provided, the PLANTUML_BIN environment variable will be used. |
plantuml |
--plantuml-jar |
path | Path to the PlantUML JAR file (runs via Java). If provided, the PLANTUML_BIN environment variable is ignored.Can also be set via the PLANTUML_JAR environment variable. |
None |
--java-bin |
string (path or command) | Java executable to use when running PlantUML via JAR. If not provided, the JAVA_BIN environment variable will be used. |
java |
--plantuml-skinparam-dpi |
integer | Set PlantUML skinparam dpi value to control raster (PNG) resolution.This modifies diagram rendering and affects all output formats. Can also be set via the PLANTUML_SKINPARAM_DPI environment variable. |
None |
--plantuml-use-new-c4-style |
boolean | Activates the new C4-PlantUML style. | False |
--plantuml-use-bundled-c4-plantuml |
boolean | Use bundled C4-PlantUML library files instead of fetching them from remote sources. | True |
Mermaid Options:
These options apply when using the mermaid renderer.
| Name | Type | Description | Default |
|---|---|---|---|
--mermaid-bin |
string (path or command) | Mermaid executable (command name or full path). If not provided, the MERMAID_BIN environment variable will be used. |
mmdc |
--mermaid-scale-factor |
integer | Set Mermaid scale value to control Puppeteer scale factor. Can also be set via the MERMAID_SCALE_FACTOR environment variable. |
1 |
c4 convert¶
Convert a diagram from one representation to another.
Note
Requires additional dependencies.
Usage:
c4 convert [-h] \
[--json-to-py] \
[--from {json} | --from-json] \
[--to {py} | --to-py] \
[-o OUTPUT] \
target
Arguments:
| Name | Type | Description |
|---|---|---|
target |
string | Diagram target. |
Options:
| Name | Type | Description | Default |
|---|---|---|---|
--from |
choice (json) |
Input format. | — |
--to |
choice (py) |
Output format. | — |
--from-json |
flag | Convert from JSON diagram (alias for --from json). |
False |
--to-py |
flag | Convert to Python DSL (alias for --to py). |
False |
--json-to-py |
flag | Shortcut for --from json --to py. |
False |
-o, --output |
path | Redirect output to a file. | stdout |
-h, --help |
flag | Show this help message and exit. | False |