Python library
The lineout module renders diagram JSON from your own code, with the same renderer as the lineout command.
from pathlib import Path
from lineout import render
png = render(Path("architecture.json").read_text(), "png")
Path("architecture.png").write_bytes(png)Install lineout-cli, with the jpg extra for JPG output, into your environment. It needs Python 3.10 or newer.
pip install "lineout-cli[jpg]"render#
render(document, format="svg", *, theme="light", transparent=False, scale=None) -> bytes| Argument | Type | Description |
|---|---|---|
document | str, bytes or dict | One page, { "nodes": [...], "arrows": [...] }, as JSON text, UTF-8 or a dict. |
format | "svg", "png" or "jpg" | The output format. Defaults to "svg". |
theme | "light" or "dark" | Keyword-only. Defaults to "light". |
transparent | bool | Keyword-only. Leaves out the background of an SVG or PNG. |
scale | float or None | Keyword-only. Pixels per unit for PNG and JPG, 2 when None. |
Returns bytes: the SVG as UTF-8, or the PNG or JPG file. Images wider or taller than 16384 pixels are drawn at a smaller scale, and otherwise they're the same as the command's. Pass one page per call: a document with pages doesn't render.
Examples#
Build a page in code and write an SVG:
from pathlib import Path
from lineout import render
page = {
"nodes": [
{"id": "fn", "icon": "AWS-Lambda", "position": {"x": 0, "y": 0}, "label": "Orders handler"},
{"id": "table", "icon": "Amazon-DynamoDB", "position": {"x": 192, "y": 0}, "label": "Orders table"},
],
"arrows": [
{
"id": "a1",
"start": {"nodeId": "fn", "side": "right", "offset": 0},
"end": {"nodeId": "table", "side": "left", "offset": 0},
}
],
}
Path("orders.svg").write_bytes(render(page))A dark PNG at three times the size, without a background, and the SVG as a string:
png = render(page, "png", theme="dark", transparent=True, scale=3)
svg = render(page).decode()Errors#
render raises lineout.LineoutError when the document isn't valid JSON or doesn't match the format, with the path of the problem, such as nodes[2].position.x must be a number, when the page is empty (diagram is empty), when format is unknown, and when JPG is asked for without the jpg extra.
from lineout import LineoutError, render
try:
png = render(page, "png")
except LineoutError as error:
print(f"Can't render the diagram: {error}")An unknown icon raises a UserWarning, such as icon not found: AWS-Lamda, and the node is drawn without it. To make that an error:
import warnings
with warnings.catch_warnings():
warnings.simplefilter("error")
svg = render(page)Module contents#
| Name | Description |
|---|---|
lineout.render | The function above. |
lineout.LineoutError | The exception it raises. |
lineout.__version__ | The installed lineout-cli version. |
The library works offline and never checks for updates. The first render call in a process starts the renderer and takes longer, and later calls reuse it.