Skip to main content

Generator HTML from pandas via Jinja2

Project description

reportlib

Generator HTML from pandas via Jinja2

Changelog for version 3.0.0

  • Styler now cannot access via df.style. Use Styler(df) instead

  • Renamed Generator -> Report, EnvironParser -> ConfigParser

  • Add default options (provided via Styler.set_option or Styler.init) for Styler.format_number()

  • Add html_ouput, email_config, email_env in Report constructor instead of hidden environ variables

  • Add reportlib.add_template_dir

Usage

Project structure

root/ (or root/src/)
 |-+-templates/
 | |-styles.css
 |-report.py
 |-email_config.yml
 |-metadata.yml
 ...

Basic usage

# Setup environ before running code
export REPORT_DATE='2019-06-30'
export EMAIL_ENV='dev'
"""report.py"""

import os
import pandas as pd
import numpy as np
from datetime import datetime
from dateutil.relativedelta import relativedelta

from reportlib import Report, ConfigParser, Styler

# Parse options from env
config = ConfigParser(config=os.environ)
report_date = config.date('REPORT_DATE', default='yesterday')

# Prepare data
df = pd.DataFrame(np.random.randn(8, 4), index=['index'], columns=['a', 'b', 'c', 'd'])

# Config report
reportlib.add_template_dir('templates')
Styler.set_option('precision', 2)
Styler.set_option('fillna', '-')
Styler.set_option('fillinf', '-')
Styler.set_option('fillzero', '-')

# Initial generator
generator = Generator(
  styles='styles.css',
  title='Report Demo',
  context={
    'report_date': report_date
  },
  html_output='report_output.html',
  email_config='email_config.yml',
  email_env=config.get('EMAIL_ENV'),
)

# Styling data
style = (
  Styler(df)
  .add_class('bold highlight', subset=pd.IndexSlice[0:1, style.data.columns])  # Bold and Highlight some row by using class `highlight`
  .add_class('text-right', columns=style.data.columns)  # Align right columns
)

# Add tables
generator.add_table(style)

# Run generator
generator.run()
# This is `dev` env
dev: 
  smtp:
    username: 'your.email@your.domain'
    pwd: 'yourpassword'
  from: 'From'  
  subject: 'Report Demo - {report_date:%d/%m/%Y}'  # Report Demo - 30/06/2019
  to: 
    - 'you@your.domain'
  cc:
    - 'your.boss@your.domain'
  bcc:
    - 'some.body@other.domain'
  files:
    - 'relative_path/some_attachtments.txt'
    - '/home/users/you/absolute_path/attactment.txt'
# ... Your other env

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

reportlib-3.1.2.tar.gz (14.1 kB view hashes)

Uploaded Source

Built Distribution

reportlib-3.1.2-py3-none-any.whl (14.6 kB view hashes)

Uploaded Python 3

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