def getHtmlTableFromCsvFile(filePath):
    """ Prepare a html table from a csv file """
    mytbody = tbody()
    with open(filePath, 'r') as f:
        fReader = csv.DictReader(f, dialect=ExcelFR())
        myRow = tr()
        for col in fReader.fieldnames:
            myRow = myRow.append(th(col))
        mytbody = mytbody.append(myRow)
        for row in fReader:
            myRow = tr()
            for col in fReader.fieldnames:
                myRow = myRow.append(td(HTML(row[col]), _class=col))
            mytbody = mytbody.append(myRow)
    return table(mytbody)
def frequency_table(item_counts, name, max_items=10):
    """ create a table of item frequencies """

    # create header
    row = htmltag.tr(htmltag.th(name), htmltag.th('Count'))
    thead = htmltag.thead(row)

    # add items
    trs = []
    items = [(v, k) for (k, v) in item_counts.items()]
    items.sort(reverse=True)
    if len(items) > max_items:
        items = items[:max_items]
    for (value, key) in items:
        row = htmltag.tr(htmltag.td(key), htmltag.td(str(value)))
        trs.append(row)
    tbody = htmltag.tbody(*trs)
    return htmltag.table(thead, tbody)
Beispiel #3
0
def frequency_table(item_counts, name, sort=True, max_items=10):
    """ create a table of item frequencies """

    # create header
    tr = htmltag.tr(htmltag.th(name), htmltag.th('Count'))
    thead = htmltag.thead(tr)

    # add items
    trs = []
    items = [(v, k) for (k, v) in item_counts.items()]
    items.sort(reverse=True)
    if len(items) > max_items:
        items = items[:max_items]
    for (value, key) in items:
        tr = htmltag.tr(htmltag.td(key), htmltag.td(str(value)))
        trs.append(tr)
    tbody = htmltag.tbody(*trs)
    return htmltag.table(thead, tbody)
Beispiel #4
0
def _angled_html_table(column_headers, row_headers, row_data):
    rc = div(table(), _class="angled_table")
    return rc
Beispiel #5
0
def to_html(results):
    PREAMBLE_FILE = os.getenv('LSM_PREAMBLE_FILE', "")
    preamble = ""
    methods = [
        'capabilities', 'systems', 'plugin_info', 'pools', 'job_status',
        'job_free', 'iscsi_chap_auth', 'volumes', 'volume_create',
        'volume_delete', 'volume_resize', 'volume_replicate',
        'volume_replicate_range_block_size', 'volume_replicate_range',
        'volume_enable', 'volume_disable', 'disks', 'target_ports',
        'volume_mask', 'volume_unmask', 'volume_child_dependency',
        'volume_child_dependency_rm', 'access_groups',
        'access_groups_granted_to_volume', 'access_group_create',
        'access_group_delete', 'volumes_accessible_by_access_group',
        'access_groups_granted_to_volume', 'access_group_initiator_add',
        'access_group_initiator_delete', 'fs', 'fs_create', 'fs_delete',
        'fs_resize', 'fs_clone', 'fs_file_clone', 'fs_snapshots',
        'fs_snapshot_create', 'fs_snapshot_delete', 'fs_snapshot_restore',
        'fs_child_dependency', 'fs_child_dependency_rm', 'export_auth',
        'exports', 'export_fs', 'export_remove'
    ]

    ch = []
    row_data = []

    if os.path.isfile(PREAMBLE_FILE):
        with open(PREAMBLE_FILE, 'r') as pm:
            preamble = pm.read()

    #Build column header
    for r in results:
        ch.append(r['SYSTEM']['ID'])

    # Add overall pass/fail for unit tests
    pass_fail = ['Overall Pass/Fail result']
    for r in results:
        if r['META']['ec'] == '0':
            pass_fail.append('P')
        else:
            pass_fail.append('F')
    row_data.append(pass_fail)

    # Append on link for error log
    error_log = ['Error log (click +)']
    for r in results:
        error_log.append('<a href="%s">+</a>' %
                         ('./' + os.path.basename(r['META']['error_file'])))
    row_data.append(error_log)

    for m in methods:
        row = [m]

        for r in results:
            row.append(get_result(r, m))

        row_data.append(row)

    # Build HTML
    text = '<!DOCTYPE html>'
    text += str(
        html(
            head(
                link(rel="stylesheet", type="text/css", href="../../test.css"),
                title("libStorageMgmt test results"),
            ),
            body(
                HTML(
                    h1("%s Results generated @ %s") %
                    (preamble, time.strftime("%c"))),
                div(table(_table_header(ch), _table_body(row_data)),
                    _class="angled_table"),
                div(
                    pre("                  Legend\n"
                        "                  P = Pass (Method called and returned without error)\n"
                        "                  F = Fail (Method call returned an error)\n"
                        "                  U = Unsupported or unable to test due to other errors\n"
                        "                  * = Unable to connect to array or provider totally unsupported\n"
                        "                  + = hyper link to error log")))))

    return bs(text).prettify()
def _angled_html_table(column_headers, row_headers, row_data):
    rc = div(table(), _class="angled_table")
    return rc
def to_html(results):
    PREAMBLE_FILE = os.getenv('LSM_PREAMBLE_FILE', "")
    preamble = ""
    methods = ['capabilities',
               'systems', 'plugin_info', 'pools', 'job_status', 'job_free',
               'iscsi_chap_auth',
               'volumes', 'volume_create', 'volume_delete', 'volume_resize',
               'volume_replicate', 'volume_replicate_range_block_size',
               'volume_replicate_range', 'volume_enable', 'volume_disable',
               'disks', 'target_ports',
               'volume_mask',
               'volume_unmask',
               'volume_child_dependency',
               'volume_child_dependency_rm',
               'access_groups',
               'access_groups_granted_to_volume',
               'access_group_create',
               'access_group_delete',
               'volumes_accessible_by_access_group',
               'access_groups_granted_to_volume',
               'access_group_initiator_add',
               'access_group_initiator_delete',
               'fs',
               'fs_create',
               'fs_delete',
               'fs_resize',
               'fs_clone',
               'fs_file_clone',
               'fs_snapshots',
               'fs_snapshot_create',
               'fs_snapshot_delete',
               'fs_snapshot_restore',
               'fs_child_dependency',
               'fs_child_dependency_rm',
               'export_auth',
               'exports',
               'export_fs',
               'export_remove'
               ]

    ch = []
    row_data = []

    if os.path.isfile(PREAMBLE_FILE):
        with open(PREAMBLE_FILE, 'r') as pm:
            preamble = pm.read()

    #Build column header
    for r in results:
        ch.append(r['SYSTEM']['ID'])

    # Add overall pass/fail for unit tests
    pass_fail = ['Overall Pass/Fail result']
    for r in results:
        if r['META']['ec'] == '0':
            pass_fail.append('P')
        else:
            pass_fail.append('F')
    row_data.append(pass_fail)

    # Append on link for error log
    error_log = ['Error log (click +)']
    for r in results:
        error_log.append('<a href="%s">+</a>' %
                         ('./' + os.path.basename(r['META']['error_file'])))
    row_data.append(error_log)

    for m in methods:
        row = [m]

        for r in results:
            row.append(get_result(r, m))

        row_data.append(row)

    # Build HTML
    text = '<!DOCTYPE html>'
    text += str(html(
                head(link(rel="stylesheet", type="text/css",
                          href="../../test.css"),
                title("libStorageMgmt test results"), ),
                body(
                    HTML(h1("%s Results generated @ %s") % (preamble, time.strftime("%c"))),
                    div(table(_table_header(ch), _table_body(row_data)),
                         _class="angled_table"),
                    div(pre(
                           "                  Legend\n"
                           "                  P = Pass (Method called and returned without error)\n"
                           "                  F = Fail (Method call returned an error)\n"
                           "                  U = Unsupported or unable to test due to other errors\n"
                           "                  * = Unable to connect to array or provider totally unsupported\n"
                           "                  + = hyper link to error log\n\n\n",
                           HTML('                  Source code for plug-in for this test run <a href=./smis.py.html>is here. </a>'))))
                ))

    return bs(text).prettify()
def generateMTurkFile(startGID,endGID,outFile,prodFileWrite = False):
    imageID = genImageID(startGID,endGID)
    links = ["http://pachy.cs.uic.edu:5000/api/image/src/"+str(i)+"/?resize_pix_w=500" for i in imageID[0:maxImgs]]
    imgTags = []
    radioShare = HT.input
    for url in links:
        imgTags.append(HT.img(src = url,alt = "Unavailable"))

    # logic to create the radio buttons and the hidden form fields
    shareRadio = []
    notShareRadio = []
    hiddenField = []
    for i in range(maxImgs):
        hiddenField.append(HT.input(type='hidden',name=imageID[i],value=imageID[i]))
        shareRadio.append(HT.input(type='radio',value='share',name=imageID[i]) + "Share")
        notShareRadio.append(HT.input(type='radio',value='noShare',name=imageID[i]) + "Do Not Share")

    tdTags = []
    for i in range(maxImgs):
        tdTags.append(HT.td(HT.center(HT.HTML(hiddenField[i]),HT.HTML(shareRadio[i]),HT.HTML(notShareRadio[i])),HT.HTML(imgTags[i])))

    trTags = []
    for i in range(0,maxImgs,2):
        trTags.append(HT.tr(HT.HTML(tdTags[i]),HT.HTML(tdTags[i+1])))

    bodyTxt = HT.table(HT.HTML('\n'.join(trTags)),border="1")

    headFile = open("files/header.txt","r")
    tailFile = open("files/tail.txt","r")
    outFileDev = outFile + ".question"
    outputFile = open(outFileDev,"w")

    for line in headFile:
        outputFile.write(line)
        
    outputFile.write(bodyTxt)

    for line in tailFile:
        outputFile.write(line)

    headFile.close()
    tailFile.close()
    outputFile.close()

    if prodFileWrite:
        headFile = open("files/header_prod.txt","r")
        tailFile = open("files/tail_prod.txt","r")
        outFileProd = outFile + "_prod.question"
        outputFile = open(outFileProd,"w")
        for line in headFile:
            outputFile.write(line)
        
        outputFile.write(bodyTxt)

        for line in tailFile:   
            outputFile.write(line)

        headFile.close()
        tailFile.close()
        outputFile.close()

    return imageID
def table(df, sortable=False, last_row_is_footer=False, col_format=None):
    """ generate an HTML table from a pandas data frame
        Args:
            df (df): pandas DataFrame
            col_format (dict): format the column name (key)
                                using the format string (value)
        Returns:
            HTML (str)
    """
    if col_format is None:
        col_format = {}
    row_count = len(df)

    # default column formatting
    default_format = {
        'align': 'right',
        'decimal_places': 2,
        'commas': True,
        'width': None,
    }

    def get_format(col, attribute):
        """ helper function to get column formatting
            Args:
                col: column name (key in the col_format)
                attribute (str)
            Returns:
                format (str) for the specified col
        """
        if col in col_format and attribute in col_format[col_name]:
            value = col_format[col_name][attribute]
        elif '*' in col_format and attribute in col_format['*']:
            value = col_format['*'][attribute]
        else:
            value = default_format[attribute]
        return value

    # create header
    items = []
    # if there's a hierarchical index for the columns, span the top level; only suppots 2 levels
    if isinstance(df.columns[0], tuple):
        headers = []
        prev_header = df.columns[0][0]
        span = 0
        for i, col_name in enumerate(df.columns):
            h1 = col_name[0]
            h2 = col_name[1]
            if h1 == prev_header:
                span += 1
                if i == (len(df.columns)-1):
                    headers.append(htmltag.th(h1, colspan=span, _class='centered'))
            else:
                headers.append(htmltag.th(prev_header, colspan=span, _class='centered'))
                if i == (len(df.columns)-1):
                    headers.append(htmltag.th(h1, colspan=1))
                else:
                    prev_header = h1
                    span = 1

            if get_format(col_name, 'align') == 'right':
                items.append(htmltag.th(h2, _class='alignRight'))
            else:
                items.append(htmltag.th(h2))
        thead = htmltag.thead(htmltag.tr(*headers), htmltag.tr(*items))

    else:
        for col_name in df.columns:
            if get_format(col_name, 'align') == 'right':
                if sortable:
                    items.append(htmltag.th(col_name, **{'class':'alignRight', 'data-sortable':'true'}))
                else:
                    items.append(htmltag.th(col_name, _class='alignRight'))
            else:
                if sortable:
                    items.append(htmltag.th(col_name, **{'data-sortable':'true'}))
                else:
                    items.append(htmltag.th(col_name))

        thead = htmltag.thead(htmltag.tr(*items))

    # create body (and optionally footer)
    tfoot = ''
    rows = []
    for i, row in df.iterrows():
        values = row.tolist()
        items = []
        for j, v in enumerate(values):
            col_name = df.columns[j]
            if is_numeric(v):
                decimal_places = get_format(col_name, 'decimal_places')
                if get_format(col_name, 'commas'):
                    pattern = '{:,.' + str(decimal_places) + 'f}'
                else:
                    pattern = '{:.' + str(decimal_places) + 'f}'
                v = pattern.format(v)
            if get_format(col_name, 'align') == 'right':
                items.append(htmltag.td(v, _class='alignRight'))
            # TODO - need to implement width control
            #width = get_format(col_name, 'width')
            #if is_numeric(width):
            #    style='width:' + str(width) + 'px'
            #    items.append(htmltag.td(v, style=style ))
            else:
                items.append(htmltag.td(v))
        if last_row_is_footer and i == row_count - 1:
            tfoot = htmltag.tfoot(htmltag.tr(*items))
        else:
            rows.append(htmltag.tr(*items))
    tbody = htmltag.tbody(*rows)
    # if sortable, apply the bootstrap-table tab, bs-table
    if sortable:
        if row_count > 15:
            return htmltag.table(thead, tbody, tfoot, **{'class':'bs-table', 'data-striped':'true', 'data-height':'600'})
        else:
            return htmltag.table(thead, tbody, tfoot, **{'class':'bs-table', 'data-striped':'true'})
    else:
        return htmltag.table(thead, tbody, tfoot, **{'class':'table-striped'})
Beispiel #10
0
def table(df, sortable=False, last_row_is_footer=False, format=None):
    """ generate an HTML table from a pandas data frame """
    if not format:
        format = {}
    rowCount = len(df)

    # default column formatting
    default_format = {
        'align': 'right',
        'decimal_places': 2,
        'commas': True,
        'width': None,
    }

    # a helper function to get column formatting
    def get_format(col_name, attribute):
        if col_name in format and attribute in format[col_name]:
            value = format[col_name][attribute]
        elif '*' in format and attribute in format['*']:
            value = format['*'][attribute]
        else:
            value = default_format[attribute]
        return value

    # create header
    items = []
    if isinstance(df.columns[0], tuple):
        headers = []
        prev_header = df.columns[0][0]
        span = 0
        for i, col_name in enumerate(df.columns):
            h1 = col_name[0]
            h2 = col_name[1]
            if h1 == prev_header:
                span += 1
                if i == (len(df.columns) - 1):
                    headers.append(
                        htmltag.th(h1, colspan=span, _class='centered'))
            else:
                headers.append(
                    htmltag.th(prev_header, colspan=span, _class='centered'))
                if i == (len(df.columns) - 1):
                    headers.append(htmltag.th(h1, colspan=1))
                else:
                    prev_header = h1
                    span = 1

            if get_format(col_name, 'align') == 'right':
                items.append(htmltag.th(h2, _class='alignRight'))
            else:
                items.append(htmltag.th(h2))
        thead = htmltag.thead(htmltag.tr(*headers), htmltag.tr(*items))

    else:
        for col_name in df.columns:
            if get_format(col_name, 'align') == 'right':
                if sortable:
                    items.append(
                        htmltag.th(
                            col_name, **{
                                'class': 'alignRight',
                                'data-sortable': 'true'
                            }))
                else:
                    items.append(htmltag.th(col_name, _class='alignRight'))
            else:
                if sortable:
                    items.append(
                        htmltag.th(col_name, **{'data-sortable': 'true'}))
                else:
                    items.append(htmltag.th(col_name))

        thead = htmltag.thead(htmltag.tr(*items))

    # create body (and optionally footer)
    tfoot = ''
    rows = []
    for i, row in df.iterrows():
        values = row.tolist()
        if False:  #
            tf = t.tfoot
            tr = tf.tr
        items = []
        for j, v in enumerate(values):
            col_name = df.columns[j]
            if is_numeric(v):
                decimal_places = get_format(col_name, 'decimal_places')
                if get_format(col_name, 'commas'):
                    pattern = '{:,.' + str(decimal_places) + 'f}'
                else:
                    pattern = '{:.' + str(decimal_places) + 'f}'
                v = pattern.format(v)
            if get_format(col_name, 'align') == 'right':
                items.append(htmltag.td(v, _class='alignRight'))
            #width = get_format(col_name, 'width')
            #if is_numeric(width):
            #    style='width:' + str(width) + 'px'
            #    items.append(htmltag.td(v, style=style ))
            else:
                items.append(htmltag.td(v))
        if last_row_is_footer and i == rowCount - 1:
            tfoot = htmltag.tfoot(htmltag.tr(*items))
        else:
            rows.append(htmltag.tr(*items))
    tbody = htmltag.tbody(*rows)
    if sortable:
        if rowCount > 15:
            return htmltag.table(
                thead, tbody, tfoot, **{
                    'class': 'bs-table',
                    'data-striped': 'true',
                    'data-height': '600'
                })
        else:
            return htmltag.table(
                thead, tbody, tfoot, **{
                    'class': 'bs-table',
                    'data-striped': 'true'
                })
    else:
        return htmltag.table(thead, tbody, tfoot, **{'class': 'table-striped'})