Skip to main content

Pyladoc

Description

Pyladoc is a Python package for programmatically generating HTML and PDF/LaTeX output. This package specifically targets applications where reports or results with Pandas tables and Matplotlib figures are generated to be displayed as a website and as a PDF document without involving any manual formatting steps.

This package focuses on the "Document in Code" approach for cases where a lot of calculations and data handling is done but not a lot of document text needs to be displayed. The multiline string capability of Python handles this very well. In comparison to "Code in Document" templates, Python tools support this approach out of the box—similar to docstrings.

LaTeX is used as the backend for PDF generation. There are excellent engines for rendering HTML to PDF, but even if there is no requirement for accurate typesetting, placing programmatically generated content of variable composition and element sizes on fixed-size pages without manual intervention is a hard problem where LaTeX is superior.

Example outputs

example output

The documents are generated by the script tests/test_rendering_example1_doc.py.

Supported primitives

  • Text (can be Markdown or HTML formatted)
  • Headings
  • Tables (Pandas, Markdown or HTML)
  • Matplotlib figures
  • LaTeX equations (block or inline)
  • Named references for figures, tables, and equations

Key Features

  • HTML and PDF/LaTeX rendering of the same document
  • Single file output including figures
  • Figure and equation embedding in HTML by inline SVG, SVG in Base64, or PNG in Base64
  • Figure embedding in LaTeX as PGF/TikZ
  • Tested on Linux and Windows

Usage Scenarios

  • Web services
  • Report generation for lab equipment

Installation

It can be installed with pip:

pip install pyladoc

As well as with conda:

conda install conda-forge::pyladoc

Dependencies

Pyladoc depends on the markdown package.

Optional dependencies are:

  • Matplotlib Python package for rendering LaTeX equations for HTML output
  • LaTeX for exporting to PDF or exporting Matplotlib figures to LaTeX (PGF/TikZ rendering)
  • Pandas and Jinja2 for rendering Pandas tables
  • Matplotlib for rendering Matplotlib figures (obviously)

For the included template, the miktex LaTeX distribution works on Windows and the following LaTeX setup works on Ubuntu (both tested in CI):

sudo apt-get update
sudo apt-get install -y texlive-latex-extra texlive-fonts-recommended lmodern texlive-xetex texlive-science

Usage

It is easy to use, as the following example code shows:

import pyladoc
import pandas as pd

doc = pyladoc.DocumentWriter()

doc.add_markdown("""
    # Example
    This is inline LaTeX: $$\\lambda$$

    This is a LaTeX block with a number:
    $$
    \\label{eq:test1}
    \\lambda_{\text{mix}} = \\sum_{i=1}^{n} \\frac{x_i \\lambda_i}{\\sum_{j=1}^{n} x_j \\Phi_{ij}}
    $$

    This is an example table. The table @table:pandas_example shows some random data.
    """)

some_data = {
    'Row1': ["Line1", "Line2", "Line3"],
    'Row2': [120, 100, 110],
    'Row3': ['12 g/km', '> 150 g/km', '110 g/km']
}
df = pd.DataFrame(some_data)
doc.add_table(df, 'This is a pandas example table', 'pandas_example')

html_code = doc.to_html()
print(html_code)

doc.to_pdf('test.pdf')

Contributing

Contributions are welcome; please open an issue or submit a pull request on GitHub.

Developer Guide

To get started with developing the pyladoc package, follow these steps.

First, clone the repository to your local machine using Git:

git clone https://github.com/Nonannet/pyladoc.git
cd pyladoc

It's recommended to set up a venv:

python -m venv .venv
source .venv/bin/activate  # On Windows use `.venv\Scripts\activate`

Install the package and development dependencies while keeping files in the current directory:

pip install -e .[dev]

Ensure that everything is set up correctly by running the tests:

pytest

License

This project is licensed under the MIT License - see the LICENSE file for details.

Release files for pyladoc 1.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyladoc 1.2.4
File Size Uploaded
pyladoc-1.2.4.tar.gz 29.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyladoc 1.2.4
File Interpreter ABI Platform
pyladoc-1.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 47.9 kB

Release files / pyladoc-1.2.4.tar.gz

Download URL pyladoc-1.2.4.tar.gz
Size 29.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9130f39c0525224f6688311a750270532e2bd83326cf6e0d13bc4a1ca4d773af
BLAKE2b-256 checksum
How to use checksums
0ad8b3ee039e4ed3b75e4cea611c09acc057a3b8eb89ab861e7d02bfeadefc3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.13

Release files / pyladoc-1.2.4-py3-none-any.whl

Download URL pyladoc-1.2.4-py3-none-any.whl
Size 19.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
36a238710d8eb5cc1cbb00346c7730f053c3f6f176a3baf1ec7abb7d9e965c8e
BLAKE2b-256 checksum
How to use checksums
d120a34da1df51ce0238d69092e1339bd652baae63e72f39d95aed25cea6fb6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.13

Release history Release notifications | RSS feed

This release

1.2.4 This release

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page