Skip to main content

easily create mermaid diagrams with python, generate class diagrams from python ast, more languages support coming

Project description

syrenka

syrenka is mermaid markdown generator

Description

The aim of this project is to provide easy to use classes for generating mermaid charts and diagrams.

Installation

pip install syrenka

CLI

To use syrenka from command line try:

python -m syrenka -h

for example, to generate python classdiagram:

python -m syrenka classdiagram <path/to/folder>

to generate python class diagram for project with src/ structure we can either do:

python -m syrenka classdiagram <project_dir>/src

or

python -m syrenka classdiagram <project_dir> --detect-project-dir
  • there is also option to filter files, see python -m syrenka classdiagram -h for more details
  • there is now an option to put global functions/assignments in pseudo-class globals per module with flag --globals-as-class

Example

Diagrams displayed here are generated from mermaid markdown generated by syrenka converted with mmdc into svg.

SyrenkaAstClassDiagram

This is example is using python ast approach for generating the class diagram.

SyrenkaAstClassDiagram

And the code snippet used to generate it:

"""Example SyrenkaClassDiagram with ast backend."""

# from io import StringIO
import sys
from pathlib import Path

from syrenka.base import ThemeNames
from syrenka.classdiagram import SyrenkaClassDiagram, SyrenkaClassDiagramConfig
from syrenka.lang.python import PythonModuleAnalysis

class_diagram = SyrenkaClassDiagram(
    "syrenka class diagram", SyrenkaClassDiagramConfig().theme(ThemeNames.NEUTRAL)
)
class_diagram.add_classes(
    PythonModuleAnalysis.classes_in_path(
        Path(__file__).parent.parent / "src"
    )  # , recursive==True)
)

# file can be anything that implements TextIOBase
# out = StringIO() # string buffer in memory
out = sys.stdout  # stdout
# out = open("syrenka.md", "w") # write it to file

class_diagram.to_code(file=out)

# StringIO
# out.seek(0)
# print(out.read())

SyrenkaClassDiagram

This example uses importlib.import_module + ast approach. Here are current classes in syrenka module - syrenka generated mermaid diagram, rendered to svg with use of mmdc:

SyrenkaClassDiagram

So how do we get it? This is a code snippet that does it:

"""Example SyrenkaClassDiagram."""

# from io import StringIO
import sys

from syrenka.base import ThemeNames
from syrenka.classdiagram import SyrenkaClassDiagram, SyrenkaClassDiagramConfig
from syrenka.lang.python import PythonModuleAnalysis

class_diagram = SyrenkaClassDiagram(
    "syrenka class diagram", SyrenkaClassDiagramConfig().theme(ThemeNames.NEUTRAL)
)
class_diagram.add_classes(
    PythonModuleAnalysis.classes_in_module(module_name="syrenka", nested=True)
)

# file can be anything that implements TextIOBase
# out = StringIO() # string buffer in memory
out = sys.stdout  # stdout
# out = open("syrenka.md", "w") # write it to file

class_diagram.to_code(file=out)

# StringIO
# out.seek(0)
# print(out.read())

SyrenkaFlowchart

Here is the simple flowchart:

---
title: Simple Flowchart
---
flowchart TB
    1 --> 2
    2 -.-> 3
    3 --> 4
    4 ==> s
    1["First"]
    subgraph s["Subgraph"]
        2["Second"]
        3["Third"]
    end
    4["Fourth"]

and the code behind it:

"""Example Simple SyrenkaFlowchart."""

import sys

import syrenka.flowchart as sf

fl = sf.SyrenkaFlowchart(
    title="Simple Flowchart", direction=sf.FlowchartDirection.TOP_TO_BOTTOM
)
fl.add(sf.Node(identifier="1", text="First"))
sub = sf.Subgraph(identifier="s", text="Subgraph")
sub.add(sf.Node(identifier="2", text="Second"))
sub.add(sf.Node(identifier="3", text="Third"))
fl.add(sub)
fl.add(sf.Node(identifier="4", text="Fourth"))

fl.connect_by_id("1", "2")
fl.connect_by_id(source_id="2", target_id="3", edge_type=sf.EdgeType.DOTTED_LINK)
fl.connect_by_id("3", "4").connect_by_id("4", "s", sf.EdgeType.THICK_LINK)

fl.to_code(file=sys.stdout)

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

syrenka-0.5.3.tar.gz (183.6 kB view details)

Uploaded Source

Built Distribution

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

syrenka-0.5.3-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file syrenka-0.5.3.tar.gz.

File metadata

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

File hashes

Hashes for syrenka-0.5.3.tar.gz
Algorithm Hash digest
SHA256 ac713ee2f0b26a6a568f3f5578cdb23a2ed0c67e482fcd4a07b4807a02246c48
MD5 e194765c0c63e0ea2e0df661ff9ed7f1
BLAKE2b-256 145742c0548c488172a9bb22fbd3a3ff4fdb8e7d817e4d5f9fbb347ea93611eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for syrenka-0.5.3.tar.gz:

Publisher: python-publish.yml on bartlomiejcieszkowski/syrenka

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

File details

Details for the file syrenka-0.5.3-py3-none-any.whl.

File metadata

  • Download URL: syrenka-0.5.3-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for syrenka-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 956fdd2a15db6404c1393e4257ed1b5ec7ac9c2c58777564cb8c304a2f90d820
MD5 70cbdf0c4e96c08f48509676d2743d49
BLAKE2b-256 5833a6a47ddbf9d6b373a2d518b5a7a9801221c7cc69218182bbe578f1cdcf08

See more details on using hashes here.

Provenance

The following attestation bundles were made for syrenka-0.5.3-py3-none-any.whl:

Publisher: python-publish.yml on bartlomiejcieszkowski/syrenka

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