Source code for pecos.io.write_dashboard

import pandas as pd
import logging
import datetime
import pecos

logger = logging.getLogger(__name__)
    
[docs]def write_dashboard(filename, column_names, row_names, content, title='Pecos Dashboard', footnote='', logo=False): """ Generate a Pecos report Parameters ---------- filename : string content : pd.DataFrame title : string (optional, default = 'Pecos Dashboard') footnote : string (optional, default = no footer) logo : string (optional, default = no logo) Graphic to be added to the report header """ logger.info("Writing dashboard") # Set pandas display option pd.set_option('display.max_colwidth', -1) pd.set_option('display.width', 40) html_string = _html_template(column_names, row_names, content, title, footnote, logo) # Write html file # filename = join('Results', 'dashboard_' + title + ".html") html_file = open(filename,"w") html_file.write(html_string) html_file.close() logger.info("")
def _html_template(column_names, row_names, content, title, footnote, logo): template = """ <!DOCTYPE html> <html lang="en-US"> <head> <title>""" template = template + title template = template + """ </title> <meta charset="UTF-8" /> </head> <table border="0" width="100%"> <col style="width:70%"> <col style="width:30%"> <tr> <td align="left" valign="center">""" if logo: template = template + """ <img src=\"""" + logo + """\" alt='Logo' />""" template = template + """ </td> <td align="right" valign="center">""" template = template + """ </td> </tr> </table> <hr> <H2>""" template = template + title template = template + """ </H2> <table border="1" class="dataframe"> <thead> <tr> <th></th>""" for column in column_names: template = template + """ <th align="center" valign="middle">""" + column + """</th>""" template = template + """ </tr> </thead> <tbody>""" for row in row_names: template = template + """ <tr>""" template = template + """ <th align="center" valign="middle">""" + row + """</th>""" for column in column_names: template = template + """ <td align="center" valign="middle">""" try: content[row, column] # Add text if content[row, column]['text']: template = template + content[row, column]['text'] + """<br>""" # Add graphics if len(content[row, column]['graphics']) > 0: for im in content[row, column]['graphics']: template = template + """<img src=\"""" + im + """\" alt="Image not loaded" width=\"250"><br>""" # Add table if content[row, column]['table']: template = template + content[row, column]['table'] + """<br>""" # Add link if content[row, column]['link']: template = template + """<A href=\"""" + content[row, column]['link'] + """\">""" + content[row, column]['link text'] + """</A>""" except: pass template = template + """</td>""" template = template + """ </tr>""" template = template + """ </tbody> </table> <br>""" template = template + footnote + """<br><br>""" template = template + """<hr> This report was generated by <A href="https://pypi.python.org/pypi/pecos">Pecos</A> """ date = datetime.datetime.now() datestr = date.strftime('%m/%d/%Y') template = template + pecos.__version__ + ", " + datestr template = template + """ </html>""" template = template + """ </html>""" return template