Skip to main content

kroki

Access kroki from python.

To install: pip install kroki

Read the overview below, or view/download/play this demo notebook to see how to use this package.

The kroki python package is a convenience package for you to be able to generate diagrams, through kroki, getting your images as bytes or as ipython objects via python functions or doing some "cell magic" in your jupyter notebooks.

I suggest you have a look at the kroki's excellent homepage where you'll be able to try different diagram types out. Don't miss the nice cheatsheet there.

Overview

You can get the bytes of an image of a diagram like so:

from kroki import diagram_image_bytes

svn_bytes = diagram_image_bytes("Bob->Alice : Hello!")
# which you can then save in a file...

When in a jupyter notebook though, you have a more convenient function that will put those bytes into an IPython display object, which you can then display (will automatically call display if it's the last command of the cell), amongst other things.

from kroki import diagram_image

diagram_image("Bob->Alice : Hello!")
image

In both those functions, the default is diagram_type='plantuml' and output_format='svg'.

But you have other choices.

diagram_image("digraph D {Alice -> Bob, Charles -> Darwin}", diagram_type="graphviz")
image
png_bytes = diagram_image_bytes("Bob->Alice : Hello!", output_format="png")
png_bytes[:7]
b'\x89PNG\r\n\x1a'

And then, there's magic, which will allow you to write your diagram source directly in a notebook's cell.

%load_ext kroki
%%kroki

Bob->Alice : Hello!
image

The default diagram_type is still plantuml, but you can specify another type if you want.

%%kroki graphviz

digraph D {
    Alice -> Bob, Charles -> Darwin
}
image

SVG diagrams are shown as an image (<img src="data:image/svg+xml;...">) rather than inlined into the page. An inlined SVG's <style> block is page CSS, and mermaid's would otherwise restyle the notebook's own output text, e.g. turning it dark gray in dark mode. The raw SVG is still there, as .data on the returned object; kroki.bytes_to_image(svg_bytes, isolate_svg=False) gives the old inlined display.

The image costs the SVG's interactivity: no links, no <title> tooltips, no text selection, and no web fonts. When you need those and trust the diagram's CSS, inline it: %%kroki graphviz --inline for one cell, or kroki.ISOLATE_SVG = False for every diagram in the notebook (then --isolate forces the image again). From Python, kroki.bytes_to_image(svg_bytes, isolate_svg=False) does the same.

A third mode keeps the interactivity without the leak: %%kroki mermaid --scoped (or bytes_to_image(svg_bytes, svg_display="scoped")) inlines the SVG, but rewrites every rule of its <style> blocks to apply only inside the diagram. It gives the SVG root a random id and prefixes each selector with it. A leading :root, html, body or svg, and mermaid's own #mermaid-N selectors, become that root. @media and @supports blocks are scoped inside, and @font-face and @keyframes are kept as they are. @import and @page are dropped, because neither can be scoped. The rewrite works on CSS tokens (kroki.scoped_css), not on regular expressions.

To see what diagram types kroki supports through the diagram_types variable.

from kroki import diagram_types

print(diagram_types)
{'svgbob', 'vega', 'packetdiag', 'mermaid', 'structurizr', 'bytefield', 'vegalite', 'c4plantuml', 'pikchr', 'rackdiag', 'ditaa', 'wavedrom', 'nwdiag', 'graphviz', 'excalidraw', 'plantuml', 'erd', 'umlet', 'actdiag', 'nomnoml', 'bpmn', 'seqdiag', 'blockdiag'}

Not all output_format values are supported for all diagram types. To which are supported for which types, you can have a look at the output_formats dictionary, which which contains both the choices of diagram_type (as keys) and the corresponding output_format each support (as values).

from kroki import output_formats

output_formats["plantuml"]
['png', 'svg', 'jpeg', 'base64']
output_formats["mermaid"]
['svg']
output_formats["seqdiag"]
['png', 'svg', 'pdf']

Misc

The kroki function is just an alias of diagram_image which itself, is the composition of diagram_image_bytes and bytes_to_image to get an iPython image object as your output (which results in displaying the image when it's the last command of your cell).

The default diagram_type of diagram_image is plantuml. We also made some ready-to-use mermaid_image and graphviz_image by fixing diagram_type with functools.partial.

diagram_image = wrap(diagram_image_bytes, egress=bytes_to_image)
mermaid_image = wrap(
    partial(diagram_image_bytes, diagram_type="mermaid"), egress=bytes_to_image
)
graphviz_image = wrap(
    partial(diagram_image_bytes, diagram_type="graphviz"), egress=bytes_to_image
)
kroki = diagram_image  # alias

You can import these directory from kroki.

Metadata

Release files for kroki 0.1.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kroki 0.1.12
File Size Uploaded Provenance Actions
kroki-0.1.12.tar.gz 99.1 kB Without provenance Details for kroki-0.1.12.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kroki 0.1.12
File Interpreter ABI Platform Actions
kroki-0.1.12-py3-none-any.whl Python 3 none any Details for kroki-0.1.12-py3-none-any.whl

Total release size: 125.3 kB

Release files / kroki-0.1.12.tar.gz

Download URL kroki-0.1.12.tar.gz
Size 99.1 kB
Tags Source
SHA-256 checksum
How to use checksums
bf533dd773b3c4c3066618c61002b117dd422672299c66efce21169c7141b054
BLAKE2b-256 checksum
How to use checksums
2466c51502e00b9b92ce6905089e511ce01e14fea0ab610498ac8104ef93047e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.3 {"installer":{"name":"uv","version":"0.13.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / kroki-0.1.12-py3-none-any.whl

Download URL kroki-0.1.12-py3-none-any.whl
Size 26.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ba8ccb4fb8da6b8971ed23ca7fa476af1e0816b358a387b6a8a60ca8300fd7f0
BLAKE2b-256 checksum
How to use checksums
a184051390d222698373e337a75bb5fe8e2efff0762ff1a9baa6273adfe16aa2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.3 {"installer":{"name":"uv","version":"0.13.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for kroki 0.1.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page