Skip to main content

A Python package for generating customizable HTML and PDF reports from Pandas DataFrames and Jinja2 templates

Project description

Pandas Publisher

Pandas Publisher allows you to generate HTML and PDF reports using Jinja2 templates and Pandas dataframes. The generated reports can be saved as HTML, PDF, or returned as an HTML string.

Dependencies

  • pandas
  • Jinja2
  • WeasyPrint

To install run the following after cloning this repo:

pip install -e .

Usage

import pandas as pd
from pandaspublisher import generate

# Create some sample DataFrames
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'C': [5, 6], 'D': [7, 8]})

# Use a dictionary to pass the DataFrames and their variable names
dataframes = {'dataframe1': df1, 'dataframe2': df2}

# Use the full path to the template file
template_path = "/Users/jasonflittner/code/PandasPublisher/examples/templates/modern_template.html"
output_file = 'output.html'
output_format = 'html'  # The desired output format for the report, either 'html', 'pdf', or 'html_string'.
context = {
    "title": "Sample Report",
    "content": "This is a sample report generated using Pandas Publisher."
}

generate.report(template_path=template_path, dataframes=dataframes, output_format=output_format, output_filename=output_file, context=context)

The output_format parameter accepts the following values:

  • "html": Save the report as an HTML file.
  • "pdf": Save the report as a PDF file.
  • "html_string": Return the rendered HTML content as a string.

Template Creation

Create your Jinja2 templates using standard HTML and Jinja2 syntax. Use the keys from your dataframes dictionary to reference the respective dataframes in the template. For example:

<!DOCTYPE html>
<html>
<head>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
        }
        th, td {
            border: 1px solid black;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
    <h1>{{ title }}</h1>
    <p>{{ content }}</p>

    <h2>DataFrame 1</h2>
    <table>
        <thead>
            <tr>
                {% for column in dataframe1.columns %}
                    <th>{{ column }}</th>
                {% endfor %}
            </tr>
        </thead>
        <tbody>
            {% for _, row in dataframe1.iterrows() %}
                <tr>
                    {% for cell in row %}
                        <td>{{ cell }}</td>
                    {% endfor %}
                </tr>
            {% endfor %}
        </tbody>
    </table>

    <h2>DataFrame 2</h2>
    <table>
        <thead>
            <tr>
                {% for column in dataframe2.columns %}
                    <th>{{ column }}</th>
                {% endfor %}
            </tr>
        </thead>
        <tbody>
            {% for _, row in dataframe2.iterrows() %}
                <tr>
                    {% for cell in row %}
                        <td>{{ cell }}</td>
                    {% endfor %}
                </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>

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

pandaspublisher-0.1.1.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pandaspublisher-0.1.1-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file pandaspublisher-0.1.1.tar.gz.

File metadata

  • Download URL: pandaspublisher-0.1.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for pandaspublisher-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d0e319765cce1a6311e8589569f6c2fb83249761c514dcdcde2bccb0d5c9df47
MD5 fb6237a31568e1515f667bfd048a1487
BLAKE2b-256 43bb49268c6b9b0f6f53c35762b8a0900b9f1104952ddf0e1bf48ecd6c9ff149

See more details on using hashes here.

File details

Details for the file pandaspublisher-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pandaspublisher-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d41945ab083d20bc60377296a454dfddcf5b330df6ef6539472b186211d47444
MD5 4903754c5c395361bed168772463f79e
BLAKE2b-256 f87eb8e65fef49a2a5a1922f935c3a471281d59eea97e387cfa7cca5b0b6b871

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page