easily create mermaid diagrams with python, generate class diagrams from python ast, more languages support coming
Project description
syrenka Python Library
syrenka is mermaid markdown generator python library
Description
The aim of this project is to provide easy to use classes for generating mermaid charts and diagrams.
Features
- Mermaid generation:
- Flowchart
- Classdiagram
- Class diagram generation
- Languages supported:
- Python
- Languages supported:
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 -hfor 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.
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"))
# 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:
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
Sample flowchart
Here is the sample flowchart:
and the code behind it:
"""Example SyrenkaFlowchart usage."""
from io import StringIO
from pathlib import Path
import syrenka.flowchart as sf
from syrenka.generate import generate_diagram_image
flowchart = sf.SyrenkaFlowchart(
"",
sf.FlowchartDirection.TOP_TO_BOTTOM,
nodes=[
sf.Subgraph(
"one",
nodes=[
sf.Node("a1"),
sf.Node("a2"),
],
),
sf.Subgraph(
"two",
direction=sf.FlowchartDirection.LEFT_TO_RIGHT,
nodes=[
sf.Node("b1"),
sf.Node("b2"),
],
),
sf.Subgraph(
"three",
direction=sf.FlowchartDirection.BOTTOM_TO_TOP,
nodes=[
sf.Node("c1"),
sf.Node("c2"),
],
),
],
)
flowchart.connect_by_id("c1", "a2").connect_by_id("a1", "a2")
flowchart.connect_by_id("b1", "b2").connect_by_id("c1", "c2")
flowchart.connect_by_id("one", "two").connect_by_id("three", "two").connect_by_id("two", "c2")
out = StringIO()
flowchart.to_code(file=out)
print(out.getvalue())
generate_diagram_image(out.getvalue(), Path("out.svg"), overwrite=True)
Simple flowchart
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file syrenka-0.5.7.tar.gz.
File metadata
- Download URL: syrenka-0.5.7.tar.gz
- Upload date:
- Size: 188.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51189899dd1e131d6fbb8d0324d3e17402204a7aef032ead7de48e530baf1f2e
|
|
| MD5 |
a771d24bbead85c3b8ff3e94c4771919
|
|
| BLAKE2b-256 |
dbf04a0d0c2a1016ab1d5706362ac60cd22da837d54d30e1519b7a119a1d5a8a
|
Provenance
The following attestation bundles were made for syrenka-0.5.7.tar.gz:
Publisher:
python-publish.yml on bartlomiejcieszkowski/syrenka
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
syrenka-0.5.7.tar.gz -
Subject digest:
51189899dd1e131d6fbb8d0324d3e17402204a7aef032ead7de48e530baf1f2e - Sigstore transparency entry: 301905069
- Sigstore integration time:
-
Permalink:
bartlomiejcieszkowski/syrenka@1855f420bd0bfcb033d61dad4054e282d3f7480f -
Branch / Tag:
refs/tags/v0.5.7 - Owner: https://github.com/bartlomiejcieszkowski
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@1855f420bd0bfcb033d61dad4054e282d3f7480f -
Trigger Event:
release
-
Statement type:
File details
Details for the file syrenka-0.5.7-py3-none-any.whl.
File metadata
- Download URL: syrenka-0.5.7-py3-none-any.whl
- Upload date:
- Size: 18.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f0c083fb269e9e5e62e23831d47529246b24eaf16f2f54be27193a43c6f4e939
|
|
| MD5 |
b1468cf1a89033c7e9eebb71d270ca0d
|
|
| BLAKE2b-256 |
5663e02a387ac149d30a8e945c2b4aa601ece13e3ef2805479034fbc16f7535a
|
Provenance
The following attestation bundles were made for syrenka-0.5.7-py3-none-any.whl:
Publisher:
python-publish.yml on bartlomiejcieszkowski/syrenka
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
syrenka-0.5.7-py3-none-any.whl -
Subject digest:
f0c083fb269e9e5e62e23831d47529246b24eaf16f2f54be27193a43c6f4e939 - Sigstore transparency entry: 301905084
- Sigstore integration time:
-
Permalink:
bartlomiejcieszkowski/syrenka@1855f420bd0bfcb033d61dad4054e282d3f7480f -
Branch / Tag:
refs/tags/v0.5.7 - Owner: https://github.com/bartlomiejcieszkowski
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@1855f420bd0bfcb033d61dad4054e282d3f7480f -
Trigger Event:
release
-
Statement type: