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>
....... HTML Code
...

{{ report(top_5_df,field, category_list, assets_dir) }}
</body>
</html>
@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'
REPORT_TEMPLATE_DICT = {'Net_Sales_Units_LW':
{'report_name':'_Top_5_Garments_by_Last_Week_Sales',
'html_template':'layout_driver_sales.html',
'df':'top_5_df_Net_Sales_Units_LW.csv'},
'Cover':
{'report_name':'_Top_5_Garments_by_least_cover',
'html_template':'layout_driver_cover.html',
'df':'top_5_df_Cover.csv'}
}
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__':
start()

--

--

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