Skip to main content

jinja2 extension to embedd python code directly from module using pdoc

Project description

jinja2-pdoc

PyPI - Python Version PyPI - jinja2_pdoc PyPI - Downloads PyPI - License GitHub - pytest GitHub - Page GitHub tag (with filter) codecov pre-commit


jinja2 extension based on pdoc to embedd python code directly from modules or files into your jinja template.

Lazy loading of docstrings, code and functions directly from python modules into your jinja2 template.

Installation

pip install jinja2_pdoc

Example

Create a markdown file with docstrings and source code from pathlib.Path using jinja2 with jinja2_pdoc extension.

Python

from jinja2_pdoc import jinja2, Jinja2Pdoc

env = jinja2.Environment(extensions=[Jinja2Pdoc])

s = """
    # jinja2-pdoc

    embedd python code directly from pathlib using a jinja2 extension based on pdoc

    ## docstring from pathlib.Path

    {% pdoc pathlib:Path:docstring %}

    ## source from pathlib.Path.open

    ```python
    {% pdoc pathlib:Path.open:source.indent -%}
    ```
    """

code = env.from_string(textwrap.dedent(s)).render()

Path("example.md").write_text(code)

Markdown

output of the python code above.

# jinja2-pdoc

embedd python code directly from pathlib using a jinja2 extension based on pdoc

## docstring from pathlib.Path

PurePath subclass that can make system calls.

Path represents a filesystem path but unlike PurePath, also offers
methods to do system calls on path objects. Depending on your system,
instantiating a Path will return either a PosixPath or a WindowsPath
object. You can also instantiate a PosixPath or WindowsPath directly,
but cannot instantiate a WindowsPath on a POSIX system or vice versa.

## source from pathlib.Path.open

```python
def open(self, mode='r', buffering=-1, encoding=None,
         errors=None, newline=None):
  """
  Open the file pointed by this path and return a file object, as
  the built-in open() function does.
  """
  if "b" not in mode:
    encoding = io.text_encoding(encoding)
  return io.open(self, mode, buffering, encoding, errors, newline)
```

Syntax

{% pdoc<module>:<object>:<pdoc_attr[.str_attr]>%}

<module>

module name or path to python file, e.g.:

  • pathlib
  • examples/example.py

Example:

{% pdoc pathlib %}

<object>

class and/or function names, eg. from pathlib:

  • Path
  • Path.open

Example:

{% pdoc pathlib:Path %}

<pdoc_attr>

pdoc attributes:

  • docstring - docstring of the object
  • source - source code of the object
  • code - plane code from functions, without def and docstring

Example:

{% pdoc pathlib:Path:docstring %}

[.str_attr]

optional str functions can be added to <pdoc_attr> with a dot

  • dedent - removes common leading whitespace, see textwrap.dedent
  • indent - format code with 2 spaces for indentation, see autopep8.fix_code
  • upper - converts to upper case
  • lower - converts to lower case

Example:

{% pdoc pathlib:Path.open:code.dedent %}

Command Line Interface

>>> jinja2pdoc --help

Usage: jinja2pdoc [OPTIONS] [FILES]...

  Render jinja2 one or multiple template files, wildcards in filenames are
  allowed, e.g. `examples/*.jinja2`.

  If no 'filename' is provided in the frontmatter section of your file, e.g.
  '<!--filename: example.md-->'. All files are written to `output` directory
  and `suffixes` will be removed.

  To ignore the frontmatter section use the `--no-meta` flag.

Options:
  -o, --output PATH           output directory for files, if no 'filename' is
                              provided in the frontmatter.  [default: cwd]
  -e, --encoding TEXT         encoding of the files  [default: utf-8]
  -s, --suffixes TEXT         suffixes which will be removed from templates,
                              if no 'filename' is provided in the frontmatter
                              [default: .jinja2, .j2]
  --fail-fast                 exit on first error when rendering multiple file
  --meta / --no-meta          parse frontmatter from the template, to search
                              for 'filename'  [default: meta]
  --rerender / --no-rerender  Each file is rendered only once.  [default: no-
                              rerender]
  --silent                    suppress console output
  --help                      Show this message and exit.
>>> jinja2pdoc .\examples\*.jinja2
rendered         examples\example.md.jinja2......................   .\example.md

pre-commit hook

Per default the hook is not registered to any type or files!

To render all template files from docs using .pre-commit-config.yaml add the following.

You may add a frontmatter section at the beginning of in your templates to specify output directory and filename, e.g. <!--filename: example.md-->. If no metadata are at the beginning of the template, the rendered file is written to the output directory which is default the current working direktory.

repos:
  - repo: https://github.com/d-chris/jinja2_pdoc/
    rev: v1.0.0
    hooks:
      - id: jinja2pdoc
        files: docs/.*\.jinja2$

Dependencies

PyPI - autopep8 PyPI - click PyPI - jinja2 PyPI - pdoc Pypi - PyYAML


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

jinja2_pdoc-1.0.1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

jinja2_pdoc-1.0.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file jinja2_pdoc-1.0.1.tar.gz.

File metadata

  • Download URL: jinja2_pdoc-1.0.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for jinja2_pdoc-1.0.1.tar.gz
Algorithm Hash digest
SHA256 7ee8f47b2b996cc607803e2857d1f79b8b86c57ef25de85e898dd13a566de6bb
MD5 1fa757cc7953b0e3553bb401f6893b5a
BLAKE2b-256 fe899acda2e242809599b56752bad3817be949634164fe719f7a7a7db1f99ef9

See more details on using hashes here.

File details

Details for the file jinja2_pdoc-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: jinja2_pdoc-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for jinja2_pdoc-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eb4b5d62feba8fd9fe27f281c6fd8108be937ee1cfc43710a5be79f6c7957dda
MD5 1a7a06b2604a959a86e01b9ef73ba963
BLAKE2b-256 bb25432fca28254c352275e3e3fbc4bd5f81ca03d5980ac82d4c700ab755a7b1

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