Skip to main content

Convert pydantic 2 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

python -m pydantic-2-mermaid --module models.py --output models.md

Use in code

  1. Import your Pydantic models into your Python script.
  2. Create an instance of the MermaidGenerator class from the pydantic-2-mermaid module and pass in your Pydantic models as arguments.
from pydantic_2_mermaid import MermaidGenerator

import my_module

generator = MermaidGenerator(my_module)
  1. Call the generate_chart() method of the MermaidGenerator instance to generate the Mermaid chart.
chart = generator.generate_chart()
  1. 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


Download files

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

Source Distribution

pydantic-2-mermaid-0.3.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

pydantic_2_mermaid-0.3.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file pydantic-2-mermaid-0.3.0.tar.gz.

File metadata

  • Download URL: pydantic-2-mermaid-0.3.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for pydantic-2-mermaid-0.3.0.tar.gz
Algorithm Hash digest
SHA256 23b8547c8b109d0790428b0b3fbcce80a87227fcb8cee1b77faeb4ea10c2c3c1
MD5 d85295bdcbbd7f0603fc65d8e6b1e33b
BLAKE2b-256 779d874e0b47ae374b035ca7e5342379ad16bf9111b78d062ed1918c1b9cf8a0

See more details on using hashes here.

File details

Details for the file pydantic_2_mermaid-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_2_mermaid-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43b3a83a6e509554e8b6ca518b05a540fa293d902729df23a62480cd2637cd83
MD5 c015da18df686427825948d3d4aa00f9
BLAKE2b-256 a69bf41880a34bbba5a3feff0b2f5363fbc7d08989f222e286bf107845c4a1bd

See more details on using hashes here.

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