Skip to content

ithaka/mdantic

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mdantic

This is an extension to Python-Markdown. It adds a new syntax so that you can reference a pydantic BaseModel and translate its fields to a markdown table. (Get the name?) I originally make this to generate http api parameter tables with mkdocs, but you can use it for other stuffs if suitable.

The code itself is a direct modification to markdown-include since they do similar things.

Notice: This project is still WIP and need more examples to adapt to most of the possible BaseModel models.

Installation

Just use pip:

pip install markdown-mdantic

Usage

Currently this module has only one config option: init_code which will execute any python code in the environment when its __init__ is called. In this way you can do something like adding system path, django setup etc, before run.

Note: Please note that the init_code thing is a huge security issue if you run that with untrusted input. Use it with caution!

mkdocs

example:

markdown_extensions:
    mdantic:
        init_code: |
            import os
            import django
            os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.default")
            django.setup()

Some time ago the extension name used here should be markdown_mdantic, but after some point it seems there will only be one file after installation in python's site-packages directory (mdantic.py) so now we should use the name mdantic.

reference the model

Each reference must be in one line, starts with $pydantic: (notice the single space after the colon), following the model import path which looks lik a.b.c.D where D is the model class itself (which is subclass of BaseModel).

**params**:

$pydantic: src.test.TestGetSchema

customize output

The resulting table is rendered using tabulate, in github style. Currently there is no config option to change this and the table headers. PR is welcomed.

About

Extension to Python-Markdown to translate pydantic's model fields to markdown table

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 97.8%
  • Makefile 2.2%