Skip to main content

Generate the string which represents a Mermaid diagram.

Project description

mermaid_diagram

Python package for generating the string which represents a Mermaid diagram.

It provides methods to build your diagram. The resulting diagram is returned as a string, either bare or enclosed within a Markdown code block. This package cannot produce image files of the diagrams.

Use cases:

  • Include Mermaid diagrams (generated from data) in a markdown file which gets created using Python script.

Installation

You can install the package using pip:

pip install mermaid_diagram

Supported diagrams

This package supports the following Mermaid diagrams:

  • Packet diagram (packet-beta).

Usage

Packet diagram

In this first example we build a diagram of the TCP packet, using the add_segment_bytes and add_segment_bits methods. To include the total packet size in the title, it uses the size_in_bytes and size_in_bits getters. Finally, the diagram string is returned as markdown code block.

from mermaid_diagram import Packet

d = Packet()
d.add_segment_bytes(2, "Source Port")
d.add_segment_bytes(2, "Destination Port")
d.add_segment_bytes(4, "Sequence Number")
d.add_segment_bytes(4, "Acknowledgment Number")
d.add_segment_bits(4, "Data Offset")
d.add_segment_bits(6, "Reserved")
d.add_segment_bits(1, "URG")
d.add_segment_bits(1, "ACK")
d.add_segment_bits(1, "PSH")
d.add_segment_bits(1, "RST")
d.add_segment_bits(1, "SYN")
d.add_segment_bits(1, "FIN")
d.add_segment_bytes(2, "Window")
d.add_segment_bytes(2, "Checksum")
d.add_segment_bytes(2, "Urgent Pointer")
d.add_segment_bytes(4, "(Options and Padding)")
d.add_segment_bytes(8, "Data (variable length)")
d.title = f"TCP Packet - {d.size_in_bytes} bytes ({d.size_in_bits} bits)"
print(d.diagram_markdown)

# ```mermaid
# ---
# title: "TCP Packet - 32 bytes (256 bits)"
# ---
# packet-beta
# 0-15 : "Source Port"
# 16-31 : "Destination Port"
# 32-63 : "Sequence Number"
# 64-95 : "Acknowledgment Number"
# 96-99 : "Data Offset"
# 100-105 : "Reserved"
# 106 : "URG"
# 107 : "ACK"
# 108 : "PSH"
# 109 : "RST"
# 110 : "SYN"
# 111 : "FIN"
# 112-127 : "Window"
# 128-143 : "Checksum"
# 144-159 : "Urgent Pointer"
# 160-191 : "(Options and Padding)"
# 192-255 : "Data (variable length)"
# ```

Which renders as:

example-TCP-packet

In the next example we activate the use_number_prefix option, so that every segment gets a number prefix (e.g. 1) First segment). This can be helpful if the diagram will be accompanied by a numbered list where the segments are explained in detail.

from mermaid_diagram import Packet

d = Packet()
d.use_number_prefix = True
d.add_segment_bytes(2, "Source Port")
d.add_segment_bytes(2, "Destination Port")
d.add_segment_bytes(2, "Length")
d.add_segment_bytes(2, "Checksum")
d.add_segment_bytes(4, "Data (variable length)")
d.title = f"UDP Packet - {d.size_in_bytes} bytes ({d.size_in_bits} bits)"
print(d.diagram_markdown)

# ```mermaid
# ---
# title: "UDP Packet - 12 bytes (96 bits)"
# ---
# packet-beta
# 0-15 : "1) Source Port"
# 16-31 : "2) Destination Port"
# 32-47 : "3) Length"
# 48-63 : "4) Checksum"
# 64-95 : "5) Data (variable length)"
# ```

Which renders as:

example-UDP-packet

Known issues

  • No input validation or error handling.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mermaid_diagram-0.1.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mermaid_diagram-0.1.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file mermaid_diagram-0.1.1.tar.gz.

File metadata

  • Download URL: mermaid_diagram-0.1.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mermaid_diagram-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4c87a0282e7861db81dd621e8d1e78d1d59ae69c5ef965c9363aae4b6969d183
MD5 47567405133854a3fc2eb9415942cfa2
BLAKE2b-256 a0e7073a5851c1b6237c87d1ac30944e4346de4135b1ec723d97d38abe9a6bb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for mermaid_diagram-0.1.1.tar.gz:

Publisher: build.yml on mrBrutus/mermaid_diagram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mermaid_diagram-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mermaid_diagram-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 979caeb2eb0249758cdd52dd700aeffdf9964150e418a39f3d66d52dc68ba9db
MD5 b08d72555479c48f21dbe1e782e4b7fd
BLAKE2b-256 d802f3c1cd5ffdfde883e520a9cd75b8808591b7889f9ce7cbf4dbe7bd9dfd33

See more details on using hashes here.

Provenance

The following attestation bundles were made for mermaid_diagram-0.1.1-py3-none-any.whl:

Publisher: build.yml on mrBrutus/mermaid_diagram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page