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

how to send push notification to particular user using fcm (firebase Massaging) and Flutter

mobile notifications image

Comment on Tasks & Projects

Using Athena Views As A Source In Glue

Introduction to python dictionaries

Deploy Deep Learning Model Using Docker, Tensorflow Serving, Nginx and Flask (Part 3)

tiptop customer care number/8584892730//8584892738/tiptop customer care…

🚫 Users Only: Quickstart for creating a SaaS pt. 1 — User Management

Unity Development — Collisions

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

Create Sparkline Charts with JavaScript

Integration of OpenCV with Visual Studio

Deploying your Dash Application with Heroku

Inkscape generated image of whitenoise by MLW