Convert pydantic 2.1 classes to markdown mermaid class charts
Project description
Pydantic 2 Mermaid
To convert Pydantic 2 models to Mermaid charts, you can use the pydantic-2-mermaid library. This library allows you to generate Mermaid charts from your Pydantic models. Here are the steps to install and use pydantic-mermaid:
If you are using pydantic 1, please visit https://github.com/EricWebsmith/pydantic_mermaid
Use in terminal
Install the pydantic-2-mermaid library using pip:
pip install pydantic-2-mermaid
Use command line
pydantic-mermaid --module models.py --output models.md
You can run the following comamand get help for the command.
pydantic-mermaid --help
Use in code
- Import your Pydantic models into your Python script.
- Create an instance of the MermaidGenerator class from the
pydantic-2-mermaidmodule and pass in your Pydantic models as arguments.
from pydantic_mermaid import MermaidGenerator
import my_module
generator = MermaidGenerator(my_module)
- Call the generate_chart() method of the MermaidGenerator instance to generate the Mermaid chart.
chart = generator.generate_chart()
- Use the chart variable to display or save the Mermaid chart.
print(chart)
This will print the Mermaid chart as a string. You can also save the chart to a file by writing the chart string to a file:
with open("chart.mmd", "w") as f:
f.write(chart)
This will save the Mermaid chart to a file called chart.mmd. You can then use a tool like the Mermaid Live Editor to visualize and edit the chart.
examples
Inheritance:
classDiagram
class Animal {
}
class Fish {
gill: str
}
class Beast {
lags: int
}
class Bird {
winds: int
}
class Dog {
}
class Cat {
}
class Salmon {
}
class Eagle {
}
Animal <|-- Fish
Animal <|-- Bird
Animal <|-- Beast
Beast <|-- Cat
Beast <|-- Dog
Fish <|-- Salmon
Bird <|-- Eagle
dependencies:
classDiagram
class Place {
name: str
population: int
}
class County {
}
class Region {
counties: List[County]
}
class Province {
regions: List[Region]
}
class City {
counties: List[County]
}
class Country {
provinces: List[Province]
cities: List[City]
}
Region ..> County
Province ..> Region
City ..> County
Country ..> Province
Country ..> City
Place <|-- City
Place <|-- County
Place <|-- Country
Place <|-- Province
Place <|-- Region
For details, check examples/ folder.
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 pydantic_2_mermaid-0.5.3.tar.gz.
File metadata
- Download URL: pydantic_2_mermaid-0.5.3.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd4b5e3a2f3bce5aec20225ca6c18747c19543962ee646677b37e2cb9812da64
|
|
| MD5 |
e53b1f2041dbf157ab533b690a5c3c5d
|
|
| BLAKE2b-256 |
1d790034e15a56ace2fb56277ac18cfe0cb5ef72e69f7726587fc6b3b073e480
|
File details
Details for the file pydantic_2_mermaid-0.5.3-py3-none-any.whl.
File metadata
- Download URL: pydantic_2_mermaid-0.5.3-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0a4d965192b4627a2266934991c48c61442660ac8409afcb905f7ca82588025
|
|
| MD5 |
336ceb60c9aabc7bc0d86c1a8da22f34
|
|
| BLAKE2b-256 |
405991f0f66fe27efea8bd64aa3dc484ef9006b3593a4d7120d937026d11f268
|