use html or python to format a dataframe for dynamic output using jinja to pde file

Viewed 216

I've searched through many solutions but I cant seem to find what I'm looking for. So thanks in advance.

I have a dataframe of data that I use jinja to create a table in a html file. The data is stock price returns held in a dataframe. I want to get this in say 4 decimals, but with sufficient spacing between cells so that it's readable. Right now all the data in the table gets squished (see image below). I'm able to round the data, but not do the spacing. So do I do this in python or do I do this in the html code and how?

Please focus on the dataframe compare_table in the following code as this is the table I'm trying to fix. The python code is:

...
def CreateReport(fidelityPositions,comparePositions,imagefilename):
    fido_table=pd.DataFrame(fidelityPositions)
    compare_table=pd.DataFrame(comparePositions)
    env = Environment(loader=FileSystemLoader('c:\\histdata\\'))
    img_string=image_file_path_to_base64_string(imagefilename)
    compare_table=compare_table.round(decimals=4)

    template = env.get_template('myreport02.html')

    template_vars = {"title" : "Portfolio Report",
                 "fidelity_table": fido_table.to_html(),
                 "compare_table": compare_table.to_html(),
                 "img_string": img_string
                     }

    html_out = template.render(template_vars)
    dfkit.from_string(html_out,"c:\\histdata\\batfiles\\testreport.pdf",
    configuration=config)

The HTML table looks like:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
    <h2>Returns History</h2>
     {{ fidelity_table }}
    <h2>Compare History</h2>  
     {{ compare_table }}
     <div style="break-before:page"></div>
     <h2>Graph Goes Here</h2>
     <img src="data:image/png;base64,{{ img_string }}" height: 100%">
</body>

</html>

The compare_table dataframe looks like

     1 day   1 week   2 weeks  1 month   6 months
AWF  -0.0049  0.0000   0.0080  -0.0220   -0.0667
ENB  -0.0133 -0.0325  -0.0051  -0.0388   -0.1200
HYT  -0.0104 -0.0112   0.0032  -0.0234   -0.1051
KMI  -0.0275 -0.0182   0.0268   0.0517   -0.0816
LAC  -0.0537 -0.1074  -0.1928  -0.3357   -0.0237
MSFT -0.0021 -0.0305  -0.0387  -0.0705   -0.2692
QQQ  -0.0097 -0.0375  -0.0196  -0.0470   -0.1840

But the html ouput in the pdf file looks like

enter image description here

0 Answers
Related