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

This article follows on from my previous post

So once the star schema is loaded into our database(I’m using google big query — in the following post I will go over how I loaded that in the airflow section)

We will generate the assets /charts for the reports — I’ll post a link to the following github where the full code can be seen.

Whilst generating the charts I learnt about the library shutil and also that I could store plotly plots in a dictionary which helped me generate the plots in a loop and write and name them to a folder at the same time.

After generating the charts we will need to generate the HTML reports using the WeasyPrint Flask driver in order to produce the reports.

For those not familiar with flask and Jinja2 , the basic concept is that you can pass variables to a template to generate dynamic HTML, an example of how I used it is below, im passing my dataframe as a variable , along with a list of categories that I used to filter the page and the assets directory to put the images in.

You can then embed this macro into a main template , in my example I had one macro that would generate two reports per category and then sandwhich that in between a front cover and ending page

CSS posed a challenge here as I was new to bootstrap and flexbox, I also had an issue generating CSS for print , however this could of been due to the flask weasy print driver. However the following code helped.

To generate the actual reports we had to install WeasyPrint (Which can be found in the documentation) and then create the driver script that will be used to generate the reports.

The following youtube channel really helped me get started with WeasyPrint

The code for WeasyPrint itself is fairly simple , the main code is here , I had two reports to generate so I created a dictionary to pass the variables through.

I will update this article soon with an example report using fake data

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