Skip to main content

Convert pydantic 2.0.3+ classes to markdown mermaid class charts

Project description

Pydantic 2 Mermaid

CI Coverage pypi downloads versions license

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

  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_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:

Administrative Divisions of the United States of America

classDiagram
    class Municipality {
        name: str
        population: int
    }

    class MinorCivilDivision {
        name: str
        population: int
    }

    class County {
        name: str
        population: int
        municipalities: list[Municipality]
        minor_civil_divisions: list[MinorCivilDivision]
    }

    class CountyEquivalant {
        name: str
        population: int
    }

    class State {
        name: str
        population: int
        counties: list[County]
        county_equivalants: list[CountyEquivalant]
    }

    class FederalDistrict {
        name: str
        population: int
    }

    class InhabitedTerritory {
        name: str
        population: int
    }

    class HabitedTerritory {
        name: str
        population: int
    }

    class Federal {
        name: str
        population: int
        federal_distric: FederalDistrict
        states: list[State]
        Inhabited_territories: list[InhabitedTerritory]
        Habited_territories: list[HabitedTerritory]
    }


    County ..> MinorCivilDivision
    County ..> Municipality
    State ..> CountyEquivalant
    State ..> County
    Federal ..> InhabitedTerritory
    Federal ..> HabitedTerritory
    Federal ..> State
    Federal ..> FederalDistrict

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.7.3.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

pydantic_2_mermaid-0.7.3-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_2_mermaid-0.7.3.tar.gz.

File metadata

  • Download URL: pydantic_2_mermaid-0.7.3.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pydantic_2_mermaid-0.7.3.tar.gz
Algorithm Hash digest
SHA256 b4ba43942b0df2ba48eb7b2c7d09e57648973252025ddf1fce9fe9fa6125ae35
MD5 bddb4485a8e8ed67375a9d580890860d
BLAKE2b-256 f4eaa63fada15ea962122972c2ff86e042418562b852a6967c7ce43b46a5ed9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_2_mermaid-0.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 665ed9d5f23c251bcc8eef2d9e9f1af4ea3c978e99676fecc0fbccb2d1aead62
MD5 bacf928264b67802ea0c8e1f60476af6
BLAKE2b-256 eb8f4f64f4e7929a49bb87698152f0a925854de865d319e8f5b5e5e948663d62

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page