Convert pydantic classes to markdown mermaid class charts
Project description
Pydantic Mermaid
To convert Pydantic models to Mermaid charts, you can use the pydantic-mermaid
library. This library allows you to generate Mermaid charts from your Pydantic models. Here are the steps to install and use pydantic-mermaid:
Use in terminal
Install the pydantic-mermaid
library using pip:
pip install pydantic-mermaid
Use command line
python -m pydantic-mermaid --module models.py --output models.md
Use in code
- Import your Pydantic models into your Python script.
- Create an instance of the MermaidGenerator class from the
pydantic-mermaid
module 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 County {
name: str
population: int
}
class Region {
name: str
population: int
counties: List[County]
}
class Province {
name: str
population: int
regions: List[Region]
}
class City {
name: str
population: int
counties: List[County]
}
class Country {
name: str
population: int
provinces: List[Province]
cities: List[City]
}
Region ..> County
Province ..> Region
City ..> County
Country ..> City
Country ..> Province
Region --|> Place
County --|> Place
City --|> Place
Country --|> Place
Province --|> Place
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
Hashes for pydantic_mermaid-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f0fdb6801cb8c2fe43a6bbba3247fbef9e230202225f56b57044a5740600485 |
|
MD5 | 084e92a630e408c88b90198202c55dc5 |
|
BLAKE2b-256 | 91e8bed6f9080b8cc81a65b8556908301481eec18b8e188f687caea5afe6d404 |