Skip to main content

PDF reporting made easy.

Project description

Pydfy: PDF Generation Made Simple

Pre-commit Tests License

Pydfy makes creating beautiful PDF reports as simple as you'd hope it to be. In practice, this is often not the case as:

  • Not the whole team knows LaTeX
  • The support for this in BI tooling is lacking
  • Webpages are usually not tuned for PDF exports
  • PDF builders are too complex to get something out the door by the end of the week

Installation

First, make sure tailwindcss is in your PATH, which for Linux means:

curl -sLO https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-linux-x64
chmod +x tailwindcss-linux-x64
mv tailwindcss-linux-x64 /usr/local/bin/tailwindcss

Check https://github.com/tailwindlabs/tailwindcss/releases/latest to find the matching binary for your system.

Also make sure to install Chromium:

apt-get update && apt-get install -y chromium  # Example using apt-get
brew install --cask chromium                   # Homebrew example for macOS

Now run:

pip install pydfy

And you should be good to go. Optionally install the extra dependencies as well:

pip install "pydfy[pandas,matplotlib]"

Usage

Example

import pydfy.models as pf
import pandas as pd

sales_df = pd.read_csv("sales.csv")
orders_last_week = sales_df[sales_df["week"] == 10]
avg_price = sales_df["price"].mean()

pdf = pf.PDF(
    pf.Title(text="Sales Report"),
    [pf.Number(orders_last_week, title="Orders last week"), pf.Number(avg_price, title="Average price")],
    [pf.Table(sales_df, "Sales table"), sales_df.plot("hist")],
)
pdf.render()

The main entrypoint is defined, in this context, by the pf.PDF function, which accepts the rows of the PDF as arguments and wraps a pf._PDF model for ease of use. A row here can either be a single component or an iterable of components, as shown in the example above.

Overview of Components

Component(template_path=..., col_span=...)  # Abstract component, args apply to all other components

Table(rows=..., title=..., headers=...)
Number(number=..., tiele=...)

Image(path=...)
Figure(figure=...)

Title(text=...)
Section(text=...)
Paragraph(content=...)
PageBreak(...)

Output location

By default, this renders a PDF file called out.pdf in the current working directory. To control the location of the build files, you can do:

# Change the name of the rendered report and store the intermediate html and css files in the /build directory
pdf.render(out="report.pdf", build_dir="/build")

# Save everything in the /data directory
pdf.render(out="/data/report.pdf", build_dir="/data")

# Equivalent to the previous line
os.environ["PYDFY_BUILD_DIR"] = "/data"
pdf.render(out="/data/report.pdf")

Custom Components

You can create custom components with custom html templates as follows:

from dataclasses import dataclass, field

import pydfy.models as pf


@dataclass
class TwoNumbers(pf.Component):
    number_1: int
    number_2: int
    title: str

    template_path: str = field(default="two_numbers.html", kw_only=True)

Where the contents of two_numbers.html is:

<div class="pf-two-numbers border-solid border border-gray-100">
    <div class="pf-two-numbers-title bg-gray-200 text-lg font-bold text-center align-top">{{ component.title }}</div>
    <div class="pf-two-numbers-content mt-2 text-center">
        <div class="grid grid-cols-2">
            <div class="text-3xl mb-2">{{ "{:,}".format(component.number_1) }}</div>
            <div class="text-3xl mb-2">{{ "{:,}".format(component.number_2) }}</div>
        </div>
    </div>
</div>

To see a working example, check out examples/custom.

Custom CSS

You can add custom CSS to the compilation step:

pdf.render(css="blue.css")

Content of my.css, where the path should point to your pydfy install:

@import "../../pydfy/template/src/base.css";

.pf-number-content {
    color: blue;
}

To see a working example, check out examples/iris.

Using Docker

You could use Docker in the development process as follows:

docker build -t pydfy .
cd examples/iris
docker run -v $PWD/:/data pydfy /data/main.py  # The docker image is configured with PYDFY_BUILD_DIR=/data

Contributing

poetry install --with dev --all-extras

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

pydfy-0.1.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

pydfy-0.1.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file pydfy-0.1.0.tar.gz.

File metadata

  • Download URL: pydfy-0.1.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.8.10 Linux/5.15.0-107-generic

File hashes

Hashes for pydfy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dfcee70ac8d165aad8357a8d8f79460162ece0bdd67a24215e80e6aa4c547c0e
MD5 bd2f1074c95ca88330dca0434fc30317
BLAKE2b-256 ad62fc2a326d34ab5a35d085a40de8ef8d1a4d3012bcff4385b6b7229ba71bd9

See more details on using hashes here.

File details

Details for the file pydfy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pydfy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.8.10 Linux/5.15.0-107-generic

File hashes

Hashes for pydfy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9357a65837afde06aaed5fb9f330164fb4dbf345a4be85cab2983c90d10b81f4
MD5 607eaace70a1aa2d3bec64b4510b0b3d
BLAKE2b-256 933ff226724e5c43cc5d003a5a98b1bdd46d5fb9bfef0a225d77b55ab13238d6

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