2025-03-28 12:30:08 +00:00
|
|
|
# Pyladoc
|
|
|
|
|
|
|
|
## Description
|
2025-06-25 06:36:32 +00:00
|
|
|
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
|
2025-04-14 19:16:43 +00:00
|
|
|
formatting steps.
|
2025-03-28 12:30:08 +00:00
|
|
|
|
|
|
|
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
|
2025-04-14 19:16:43 +00:00
|
|
|
document text needs to be displayed. The multiline string capability of Python
|
2025-06-25 06:36:32 +00:00
|
|
|
handles this very well. In comparison to "Code in Document" templates,
|
|
|
|
Python tools support this approach out of the box—similar to docstrings.
|
2025-03-28 12:30:08 +00:00
|
|
|
|
2025-06-25 06:36:32 +00:00
|
|
|
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
|
2025-04-14 19:16:43 +00:00
|
|
|
is a hard problem where LaTeX is superior.
|
2025-03-28 12:30:08 +00:00
|
|
|
|
2025-03-28 12:48:52 +00:00
|
|
|
## Example outputs
|
2025-04-15 08:37:09 +00:00
|
|
|
|
2025-05-26 07:45:58 +00:00
|
|
|
[](https://raw.githubusercontent.com/Nonannet/pyladoc/refs/heads/main/tests/out/test_latex_render1.pdf)
|
2025-04-15 08:37:09 +00:00
|
|
|
|
|
|
|
- HTML: [test_html_render1.html](https://html-preview.github.io/?url=https://github.com/Nonannet/pyladoc/blob/main/tests/out/test_html_render1.html) ([code](https://github.com/Nonannet/pyladoc/blob/main/tests/out/test_html_render1.html))
|
|
|
|
- PDF: [test_latex_render1.pdf](https://raw.githubusercontent.com/Nonannet/pyladoc/refs/heads/main/tests/out/test_latex_render1.pdf) ([code](https://github.com/Nonannet/pyladoc/blob/main/tests/out/test_html_render1.tex))
|
2025-03-28 12:48:52 +00:00
|
|
|
|
2025-04-14 19:16:43 +00:00
|
|
|
The documents are generated by the script [tests/test_rendering_example1_doc.py](tests/test_rendering_example1_doc.py).
|
|
|
|
|
2025-04-13 22:44:30 +00:00
|
|
|
### Supported primitives
|
2025-03-28 12:30:08 +00:00
|
|
|
- Text (can be Markdown or HTML formatted)
|
|
|
|
- Headings
|
|
|
|
- Tables (Pandas, Markdown or HTML)
|
|
|
|
- Matplotlib figures
|
2025-04-14 19:16:43 +00:00
|
|
|
- LaTeX equations (block or inline)
|
2025-06-25 06:36:32 +00:00
|
|
|
- Named references for figures, tables, and equations
|
2025-03-28 12:30:08 +00:00
|
|
|
|
|
|
|
### Key Features
|
2025-04-13 10:18:28 +00:00
|
|
|
- HTML and PDF/LaTeX rendering of the same document
|
2025-03-28 12:30:08 +00:00
|
|
|
- Single file output including figures
|
2025-06-25 06:36:32 +00:00
|
|
|
- Figure and equation embedding in HTML by inline SVG, SVG in Base64, or PNG in Base64
|
2025-04-13 10:18:28 +00:00
|
|
|
- Figure embedding in LaTeX as PGF/TikZ
|
2025-04-14 19:16:43 +00:00
|
|
|
- Tested on Linux and Windows
|
2025-03-28 12:30:08 +00:00
|
|
|
|
|
|
|
### Usage Scenarios
|
2025-06-25 06:36:32 +00:00
|
|
|
- Web services
|
2025-03-28 12:30:08 +00:00
|
|
|
- Report generation for lab equipment
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
It can be installed with pip:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
pip install pyladoc
|
|
|
|
```
|
|
|
|
|
2025-06-25 06:36:32 +00:00
|
|
|
As well as with conda:
|
|
|
|
```bash
|
|
|
|
conda install conda-forge::pyladoc
|
|
|
|
```
|
|
|
|
|
2025-04-14 19:16:43 +00:00
|
|
|
## Dependencies
|
|
|
|
Pyladoc depends on the markdown package.
|
|
|
|
|
|
|
|
Optional dependencies are:
|
2025-06-25 06:36:32 +00:00
|
|
|
- Matplotlib Python package for rendering LaTeX equations for HTML output
|
2025-04-14 19:16:43 +00:00
|
|
|
- LaTeX for exporting to PDF or exporting Matplotlib figures to LaTeX (PGF/TikZ rendering)
|
2025-06-25 06:36:32 +00:00
|
|
|
- Pandas and Jinja2 for rendering Pandas tables
|
|
|
|
- Matplotlib for rendering Matplotlib figures (obviously)
|
2025-04-14 19:16:43 +00:00
|
|
|
|
2025-06-25 06:36:32 +00:00
|
|
|
For the included template, the `miktex` LaTeX distribution works on Windows
|
2025-05-26 10:58:15 +00:00
|
|
|
and the following LaTeX setup works on Ubuntu (both tested in CI):
|
2025-05-13 09:12:21 +00:00
|
|
|
|
|
|
|
```bash
|
|
|
|
sudo apt-get update
|
2025-05-26 10:58:15 +00:00
|
|
|
sudo apt-get install -y texlive-latex-extra texlive-fonts-recommended lmodern texlive-xetex texlive-science
|
2025-05-13 09:12:21 +00:00
|
|
|
```
|
|
|
|
|
2025-03-28 12:30:08 +00:00
|
|
|
## Usage
|
2025-06-25 06:36:32 +00:00
|
|
|
It is easy to use, as the following example code shows:
|
2025-03-28 12:30:08 +00:00
|
|
|
|
|
|
|
```python
|
|
|
|
import pyladoc
|
2025-04-13 22:44:30 +00:00
|
|
|
import pandas as pd
|
2025-03-28 12:30:08 +00:00
|
|
|
|
|
|
|
doc = pyladoc.DocumentWriter()
|
|
|
|
|
|
|
|
doc.add_markdown("""
|
|
|
|
# Example
|
2025-04-13 22:44:30 +00:00
|
|
|
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.
|
2025-03-28 12:30:08 +00:00
|
|
|
""")
|
|
|
|
|
|
|
|
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)
|
2025-04-13 22:44:30 +00:00
|
|
|
doc.add_table(df, 'This is a pandas example table', 'pandas_example')
|
2025-03-28 12:30:08 +00:00
|
|
|
|
|
|
|
html_code = doc.to_html()
|
2025-04-13 22:44:30 +00:00
|
|
|
print(html_code)
|
2025-03-28 12:30:08 +00:00
|
|
|
|
|
|
|
doc.to_pdf('test.pdf')
|
|
|
|
```
|
|
|
|
|
|
|
|
## Contributing
|
2025-06-25 06:36:32 +00:00
|
|
|
Contributions are welcome; please open an issue or submit a pull request on GitHub.
|
2025-03-28 12:30:08 +00:00
|
|
|
|
|
|
|
## Developer Guide
|
|
|
|
To get started with developing the `pyladoc` package, follow these steps.
|
|
|
|
|
|
|
|
First, clone the repository to your local machine using Git:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git clone https://github.com/Nonannet/pyladoc.git
|
|
|
|
cd pyladoc
|
|
|
|
```
|
|
|
|
|
2025-06-25 06:36:32 +00:00
|
|
|
It's recommended to set up a venv:
|
2025-03-28 12:30:08 +00:00
|
|
|
|
|
|
|
```bash
|
2025-05-26 10:58:15 +00:00
|
|
|
python -m venv .venv
|
|
|
|
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
|
2025-03-28 12:30:08 +00:00
|
|
|
```
|
|
|
|
|
2025-06-25 06:36:32 +00:00
|
|
|
Install the package and development dependencies while keeping files in the
|
2025-03-28 12:30:08 +00:00
|
|
|
current directory:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
pip install -e .[dev]
|
|
|
|
```
|
|
|
|
|
|
|
|
Ensure that everything is set up correctly by running the tests:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
pytest
|
|
|
|
```
|
2025-04-14 19:16:43 +00:00
|
|
|
|
2025-03-28 12:30:08 +00:00
|
|
|
## License
|
|
|
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|