Translation needed
The content of this page was copied from another page tree and needs to be translated or updated.
When you finish translation, make sure to
-
Replace the label NEEDS-TRANSLATING with TRANSLATED
-
Remove this macro from the page
Create folder
Folder name will be used as report script name
Create config.json file
This file consists of the following parts:
- name - name of the script, should be the same as folder name
- description - default description for report
- rootClass - name of the class of objects used as subtree roots
- classes - list of the classes used in report script
- relations - definitions of relations between classes
'classes' part structure
- name - name of the class
- fields - array of fields used in report
"classes":[ { "name":"report_class_name_1", "fields":[ "field1name", "Custom 1" ] }, { "name":"report_class_name_2", "fields":[] } ]
'relations' part structure
For each class (if required) we have to define list of its children classes.
"relations":{ "report_class_name_1":[ "report_class_name_2" ] }
Prepare python script (report_script.py)
- in this file create class ReportGenerator(ReportGeneratorInterface) - please remember to add following import:
from reporting_service.ReportGeneratorInterface import ReportGeneratorInterface
- ReportGeneratorInterface has following abstract methods which should be implemented (details for these methods below):
def prepare_data_frames(self, data_frames) - main method with all data preparations def get_pdf_template(self) - creates template object for pdf generation def get_pdf_css_file(self) - get path of css file for pdf generation def get_template_vars(self, data_frames) - prepare template variables based on data frames returned by prepare_data_frames method def get_excel_template(self) - get path of file with excel template def get_float_format(self) - get float format in python style, e.g. '%.3f' def get_excel_data_row_styles(self) - get row styles for excel report (not required) def get_excel_columns_width(self) - get columns width for excel report (not required)
get_pdf_template
imports
from pathlib import Path from jinja2 import FileSystemLoader, Environment
example
def get_pdf_template(self): p = Path(__file__).parent template_loader = FileSystemLoader(searchpath = p) # noinspection JinjaAutoinspect template_env = Environment(loader = template_loader) return template_env.get_template("template.html")
get_float_format
def get_float_format(self): return '%.3f'
get_pdf_css_file
def get_pdf_css_file(self): return Path(__file__).parent / "style.css"
get_excel_template
def get_excel_template(self): return Path(__file__).parent / "template.xlsx"
prepare_data_frames(self, data_frames)
Object data_frames contains data from FMA collected as a dictionary. There is one, required data frame "hierarchy" which contains pairs parentId - childId. Other data frames are available under the class names given in the config.json file.
get_excel_data_row_styles
In this method one can set styles for each data set.
Style sections:
- columns - set style for selected columns (by name)
- rows - set style of each row
- evenRow - set style of each even row
- lastRow - set style of selected columns in the last row in data set
Available style properties:
- bgColor - cell background color
- bold - if True, font in cell will be bold
- italic - if True, font in cell will be italic
Example:
def get_excel_data_row_styles(self): return { 'floors': { 'columns': { }, 'rows': { 'bgColor': 'EBEFF2' }, 'evenRow': { 'bgColor': 'D4DCE6' }, 'lastRow': { 'B': { 'bgColor': 'FFFF00', 'bold': True }, 'C': { 'bgColor': 'FFFF00', 'bold': True } } }, 'floors_tenants': { 'columns': { 'A': {} }, 'rows': { 'bgColor': 'EBEFF2' }, 'evenRow': { 'bgColor': 'D4DCE6' }, 'lastRow': { 'E': { 'bgColor': 'FFFF00', 'italic': True } } }, 'rooms': { 'columns': { 'A': {} }, 'rows': { 'bgColor': 'EBEFF2' }, 'evenRow': { 'bgColor': 'D4DCE6' }, 'lastRow': { 'E': { 'bgColor': 'FFFF00', 'italic': True } } } }
get_excel_columns_width
In this method one can set width of selected columns.
def get_excel_columns_width(self): return { 'A': 30, 'B': 30, 'C': 30, 'D': 30, 'E': 30, 'F': 30, 'G': 30, 'H': 30 }
Create Excel template (xlsx file)
We can build an excel template by defining columns and their style, and using macros we can insert different values or the content of data sets in selected places.
Macro is specified by double curly braces. ( {{ macro_name }} )
Create PDF template
HTML template
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <link rel="stylesheet" href="static/style.css"> </head> <body> <h2>Areor för {{ vars.byggnad }} {{ vars.date }}</h2> <table class="properties"> <tr> <td class="firstcol">Ort</td> <td>{{ vars.ort }}</td> </tr> <tr> <td class="firstcol">Campus</td> <td>{{ vars.campus }}</td> </tr> <tr> <td class="firstcol">Byggnad</td> <td>{{ vars.byggnad }}</td> </tr> <tr> <td class="firstcol">Status</td> <td>{{ vars.status }}</td> </tr> </table> <h3>Areasammanställning</h3> {{ vars.floors }} <br/> {{ vars.rooms }} </body> </html>
CSS style file
@page { size: A4 landscape; } body { font-size: 75%; color: #222; background: #fff; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; } /* Headings -------------------------------------------------------------- */ h2,h3 { font-weight: normal; color: #111; } h2 { font-size: 2em; margin-bottom: 0.75em; } h3 { font-size: 1.5em; line-height: 1; margin-bottom: 1em; } table { margin-bottom: 1.4em; width:100%;} th { font-weight: bold; } thead th { background: #c3d9ff; } th,td,caption { padding: 4px 10px 4px 5px; } td.firstcol { font-weight: bold; } /* You can zebra-stripe your tables in outdated browsers by adding the class "even" to every other table row. */ tbody tr {text-align: center } tbody tr:nth-child(even) td, tbody tr.even td { background: #e5ecf9; } .properties { width: 40%; } .properties, .properties td { border: 1px solid black; background: #ffffff }