Python dictionary in to html table

后端 未结 7 1283
刺人心
刺人心 2020-12-23 21:33

Is there any way to print the python dictionary in to a table in HTML. I have a python dictionary and am sending to HTML by using

return render_template(\'i         


        
相关标签:
7条回答
  • 2020-12-23 22:21

    so I wanted an easy way to generate html from python dictionary with only inline styles (because emails) and couldn't find anything that I was satasfied with so I wrote this,

    it's very simple to use and easy to add styles to

    <table style="margin: 3px">
        <tr style="background-color: #7cc3a97d">
            <th style="color: white">col1</th>
            <th style="color: white">col2</th>
            <th style="color: white">col3</th>
            <th style="color: white">col4</th>
        </tr>
    
        <tr style="background-color: aliceblue">
            <td style="padding: 1rem">value11</td>
            <td style="padding: 1rem">value21</td>
            <td style="padding: 1rem">value31</td>
            <td style="padding: 1rem">value41</td>
        </tr>
        <tr style="background-color: #c2d4e4">
            <td style="padding: 1rem">value12</td>
            <td style="padding: 1rem">value22</td>
            <td style="padding: 1rem">value32</td>
            <td style="padding: 1rem">value42</td>
        </tr>
        <tr style="background-color: aliceblue">
            <td style="padding: 1rem">value13</td>
            <td style="padding: 1rem">value23</td>
            <td style="padding: 1rem">value33</td>
            <td style="padding: 1rem">value43</td>
        </tr>
    </table>

    lets say you have the following dictionary

    myDict = {
        'col1' : ['value11', 'value12', 'value13'],
        'col2' : ['value21', 'value22', 'value23'],
        'col3' : ['value31', 'value32', 'value33'],
        'col4' : ['value41', 'value42', 'value43'],
    }
    

    it can be converted with

    class HTML:
    
        def __init__(self, Header, tableStyles = {}, trStyles = {}, thStyles = {}):
            self.tableStyles = HTML._styleConverter(tableStyles)
            trStyles = HTML._styleConverter(trStyles)
            thStyles = HTML._styleConverter(thStyles)
            self.rows = []
            self.Header= f'<tr {trStyles} >'
            for th in Header:
                self.Header += f'\n<th {thStyles} >{th}</th>'
            self.Header += '\n</tr>'
    
        @staticmethod
        def _styleConverter(styleDict : dict):
            if styleDict == {}:
                return ''
            styles = ''
            for [style, value] in styleDict.items():
                styles +=f'{style}: {value};'
            return f'style="{styles}"'
    
        def addRow(self, row, trStyles = {}, tdStyles = {}):
            trStyles = HTML._styleConverter(trStyles)
            tdStyles = HTML._styleConverter(tdStyles)
            temp_row = f'\n<tr {trStyles} >'
            for td in row:
                temp_row += f'\n<td {tdStyles} >{td}</td>'
            temp_row += '\n</tr>'
            self.rows.append(temp_row)
    
    
        def __str__(self):
    
    
            return \
    f'''
    <table {self.tableStyles} >
    {self.Header}
    {''.join(self.rows)}
    </table>
    '''
    
    
    
    def dictionaryToHTMLTable(dict : dict):
        html = HTML(Header = dict.keys(),
                    tableStyles={'margin': '3px'},
                    trStyles={'background-color': '#7cc3a97d'},
                    thStyles={ 'color': 'white'})
        for i, row in enumerate(zip(*dict.values())):
            print(row)
            if i%2 == 0:
                BGC = 'aliceblue'
            else:
                BGC = '#c2d4e4'
            html.addRow(row, trStyles={'background-color' : BGC}, tdStyles={'padding': '1rem'})
        return html
    

    and to output

    print(dictionaryToHTMLTable(myDict))
    
    
    0 讨论(0)
提交回复
热议问题