Я использую jinja2 для создания отчета, который включает три таблицы (созданные из фреймов данных) с указанным идентификатором. Вывод html показывает, что идентификатор установлен правильно, например
<table border="1" class="dataframe" id="some_id_here">
<thead>some_head_content</thead>
<tbody>some_body_content</tbody>
</table>
, но, похоже, не применяет функциональные возможности DataTables, то есть без сортировки, поиска и т. Д. c.
Ниже приведен код генерации отчета вместе с кодом шаблона:
generator code
#Import templating engine
from jinja2 import FileSystemLoader, Environment
import os
#Configure Jinja and ready the environment
env_dir = 'report-builder-files'
env = Environment(
loader = FileSystemLoader(searchpath=f'{env_dir}/templates')
)
def debug(text):
print(text)
return ''
env.filters['debug']=debug
report_template = env.get_template('brs-report-template.html')
table_template = env.get_template('table_template.html')
chart_template = env.get_template('chart_template.html')
cpr_summary_template = env.get_template('contact_pass_review_summary_template.html')
def main(report_content_holder, review_start, review_end):
#Content to be published
title = f'Backroom Support Report: {review_start} to {review_end}'
sections = list()
sorted_inds = [report_content_holder[i]['display_index'] for i in range(len(report_content_holder))]
report_content_holder = [c for _, c in sorted(zip(sorted_inds, report_content_holder))]
table_id_holder = []
#Iterate over report_content_holder and build sections
for c in range(len(report_content_holder)):
#Build section summaries
if 'section_summary' in report_content_holder[c]:
if report_content_holder[c]['section_title'] == 'Contact Pass Review':
summary_template = cpr_summary_template
sections.append(summary_template.render(
section_summary=report_content_holder[c]['section_summary']))
#Build section content
if report_content_holder[c]['content_type'] == 'table':
sections.append(table_template.render(
table_title=report_content_holder[c]['section_title'],
table=report_content_holder[c]['section_content']))
#Append table_ids (content_id) to table_id_holder fo use with DataTables
table_id_holder.append(report_content_holder[c]['content_id'])
print(table_id_holder)
#Build charts
elif report_content_holder[c]['content_type'] == 'chart':
sections.append(chart_template.render(
chart_title=report_content_holder[c]['section_title'],
chart=report_content_holder[c]['section_content']
))
#Define report output file path
output_file_path = 'output-files/reports'
filename = f'brs-report_{review_start}_{review_end}.html'
#Check if output file path exists. If not, create it
if not os.path.exists(output_file_path):
print(f'Output file path {output_file_path} does not exist. Creating it...')
os.mkdir(output_file_path)
with open(f'{output_file_path}/{filename}', 'w') as f:
f.write(report_template.render(
title=title,
sections=sections,
table_id_holder=table_id_holder
))
return f'{output_file_path}/{filename}'
if __name__ == "__main__":
main()
template code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<link rel="stylesheet" type="text/css" href="../../report-builder-files/css/brs-report-stylesheet.css" />
<link rel="stylesheet" href="//cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="//cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script>
{% for table_id in table_id_holder %}
{{table_id|debug}}
$(document).ready(function() {
$('#{{ table_id }}').DataTable();
} );
{% endfor %}
</script>
</head>
<body>
<img id="logo" src="../../report-builder-files/img/BlackSky_logo_white-yellow.png" />
{% set title = title.split(':') %}
<h1><span id="title-preface">{{ title[0] }}:</span> {{title[1]}}</h1>
<p>This report was automagically generated.</p>
<!-- {{ intro }} -->
{% for section in sections %}
{{ section }}
{% endfor %}
</body>
</html>
Есть мысли о том, что я делаю неправильно?
Большое спасибо за помощь.