I developed an app that reads csv files and then provides the user with a summary of data in the form of a dataframe and a chart. I would like to provide the user with the option of downloading these results in a pdf format. I found these code examples to be helpful: Creating PDF Reports with Python, Pdfkit, and Jinja2 Templates | by Mark Nagelberg | Towards Data Science GitHub - lcalmbach/report-generator-for-long-tables: project allows to generatate pdf tables with long tables, having repeating headers
However, when I tried to follow these guides, I ended up with the following error " TemplateNotFound"
Full error message:
TemplateNotFound: reportTemplate.html
Traceback:
File "C:\Users\farha\anaconda3\envs\streamlitenv\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
exec(code, module.__dict__)
File "C:\Users\farha\Desktop\GADoha\pages\5_Flow Accumulation Report.py", line 116, in <module>
template = templateEnv.get_template(TEMPLATE_FILE)
File "C:\Users\farha\anaconda3\envs\streamlitenv\lib\site-packages\jinja2\environment.py", line 1010, in get_template
return self._load_template(name, globals)
File "C:\Users\farha\anaconda3\envs\streamlitenv\lib\site-packages\jinja2\environment.py", line 969, in _load_template
template = self.loader.load(self, name, self.make_globals(globals))
File "C:\Users\farha\anaconda3\envs\streamlitenv\lib\site-packages\jinja2\loaders.py", line 126, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\Users\farha\anaconda3\envs\streamlitenv\lib\site-packages\jinja2\loaders.py", line 218, in get_source
raise TemplateNotFound(template)
A minimum Python code example:
import streamlit as st
import pandas as pd
import numpy as np
import altair as alt
import numpy as np
import pdfkit
import jinja2
uploaded_file = st.file_uploader("", accept_multiple_files=False)
df= pd.read_csv(uploaded_file[0])
TEMPLATE_FILE = "reportTemplate.html"
templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
template = templateEnv.get_template(TEMPLATE_FILE)
outputText = template.render(df=df, month='May')
html_file = open('report.html', 'w')
html_file.write(outputText)
html_file.close()
HTML template
<!DOCTYPE html>
<head></head>
<body>
<h1>Flow Accumulation Report: {{ month }}%</h1>
<table>
<tr>
{% for column in df.columns %}
<th>{{ column }}</th>
{% endfor %}
</tr>
{% for idx, row in df.iterrows() %}
<tr>
{% for colname in df.columns %}
<td>${{ row[colname] | round(1) }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</body>
Folder Structure
App folder contains:
-1- app.py
-2- pages folder contains several streamlit pages (e.g., page1.py, page2.py, etc.)
-3- reportTemplate.html
Screenshot of the app folder