Creating pixel perfect PDF reports using using Plotly, HTML, CSS, WeasyPrint and Jinja2 — Reporting system part 3

{% macro report(top_5_df,field, category_list, assets_dir) %}
{% for category in category_list %}
<article id="summary" style="page-break-before: always"> <h2> {{ category }} -
Top 5 - Ordered by {{ field }} - Summary </h2>
{% for _,row in
top_5_df[top_5_df['Category']==category].iterrows() %}
{% from "macros-flask.html" import report %}
....... HTML Code

{{ report(top_5_df,field, category_list, assets_dir) }}
@page {
size: A4 landscape;
}@media screen and (max-width: 575px) {
main {
flex-direction: column;
.sidebar-left {
order: 2;
# TEMPLATE = 'layout_driver_sales.html'
CSS = 'print-landscape.css'
def start():for key, value in REPORT_TEMPLATE_DICT.items():print('Generating Reports',REPORT_TEMPLATE_DICT[key]['report_name'])OUTPUT_FILENAME = date_str+REPORT_TEMPLATE_DICT[key]['report_name']+'.pdf'env = Environment(loader=FileSystemLoader(TEMPLAT_SRC))template = env.get_template(REPORT_TEMPLATE_DICT[key]['html_template'])css = os.path.join(CSS_SRC, CSS)
df_dir = '../Data/csv_reports'
df_date = date_str
df_name = REPORT_TEMPLATE_DICT[key]['df']
df_path = os.path.join(df_dir,df_date,df_name)top_5_df = pd.read_csv(df_path)# variables
template_vars = { 'assets_dir': 'file://' + ASSETS_DIR , 'top_5_df':top_5_df , 'date_str':date_str, 'field': str(key), 'category_list':category_list}
# rendering to html string
rendered_string = template.render(template_vars)
html = weasyprint.HTML(string=rendered_string)
report = os.path.join(DEST_DIR, OUTPUT_FILENAME)
html.write_pdf(report, stylesheets=[css])
print('file is generated successfully and under {}', DEST_DIR)
if __name__ == '__main__':

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Go(lang) and AWS Lambda: Trigger a Lambda Function Through an API Gateway

What is Knative? Intro to Canary and Blue-Green Deployments with Dashboards (no YAML)

Scala Meetup in Berlin| Functional Programming

Regular Expression And Rails

How to install TA-LIB in Python

GitFlow Workflow in a Nutshell

BunnyPark Weekly Events: Bounty Scheme Launched + Dvision In-game Airdrops for BSC 1st Anniversary…

BunnyPark Weekly Events: Bounty Scheme Launched + Dvision In-game Airdrops for BSC 1st Anniversary Distributed + CoinMarketCap Airdrop Campaign ended + BP Repurchase Program from Team’s Fund

How to Survive Embedded Linux: How to Compile

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


More from Medium

Visualizing Hierarchical Data with a JavaScript Treemap Chart

Getting started with React and Chart.js (bar chart)

How To Create Instances With Associations In Sequelize

Introduction to basics of Application Framework | JavaScript, Version Controlling