简体   繁体   English

Python Dictionary在Flask中转换为HTML

[英]Python Dictionary convert to HTML in flask

I have python dictionary that have a list of dictionaries inside. 我有里面有字典列表的python字典。 I am trying to convert this to an HTML table that I can give to Flask render_template 我正在尝试将其转换为可以提供给Flask render_template的HTML表

My dictionary format is: 我的字典格式是:

{'sentiment_analysis_result': [{'article_title': u'These Digital Locks Help Keep Tabs on Tenants', 'score': u'0.139613', 'type': u'positive'}, {'article_title': u'You Can Get a $50 Phone From Amazon, If You Don\u2019t Mind the Ads', 'score': u'0.239663', 'type': u'positive'}]}

I want the key to be title and values to be values. 我希望键是标题,值是值。 Any help would be much apprecited! 任何帮助将不胜感激!

After trying @EliasMP 's answer the format of the table is: 尝试@EliasMP的答案后,表的格式为:

在此处输入图片说明

Just pass the dictionary from controller to template and loop it twice. 只需将字典从控制器传递到模板,然后将其循环两次即可。 First one for recovering each element of dictionary (key and value respectly), and the second one for recovering each element of each list (value recovered before), paint them using html (table tag, div formatted as a table would be the proper way, table´s tag are being obsolete) 第一个用于恢复字典的每个元素(分别为键和值),第二个用于恢复每个列表的每个元素(之前恢复的值),使用html(表标记,将div格式化为表)进行绘制是正确的方法,表格的标签已过时)

<table>
    <tr>
        <th>name_of_list</th>
        <th>values</th>
    </tr>

    {% for key, values in your_dictionary.items() %}
    <tr>
        <td>{{key}}</td>
        <td>
        <table>
           <tr>
             <th>article_title</th>
             <th>score</th>
             <th>type</th>
           </tr>
           <tr>
           {% for articles in values %}
               <td>{{article.article_title}}</td>
               <td>{{article.score}}</td>
               <td>{{article.type}}</td>
           {% endfor %}
           </tr>
        </td>
     </tr>
    {% endfor %}

</table>

Not the most efficient but gets the job done if you want to just pass the table already rendered as html as a variable to the view. 不是最有效的方法,但是如果您只想将已经呈现为html的表作为变量传递给视图,就可以完成工作。 A better way would be to pass just the data then have the template use template logic to loop over and output variables in the places you want. 更好的方法是只传递数据,然后让模板使用模板逻辑进行循环并在所需位置输出变量。

data = {'sentiment_analysis_result': [{'article_title': u'These Digital Locks Help Keep Tabs on Tenants', 'score': u'0.139613', 'type': u'positive'}, {'article_title': u'You Can Get a $50 Phone From Amazon, If You Don\u2019t Mind the Ads', 'score': u'0.239663', 'type': u'positive'}]}

table_string = '<table>'
for key, value in data.iteritems():
    table_string += '<thead>'
    table_string += '<th>' + key + '</th>'
    table_string += '</thead>'
    table_string += '<tbody>'
    for i, d in enumerate(value):
        if i == 0:
            table_string += '<tr>'
            for k in d.iterkeys():
                table_string += '<td>' + k + '</td>'
            table_string += '</tr>'
        table_string += '<tr>'
        for v in d.itervalues():
            table_string += '<td>' + v + '</td>'
        table_string += '</tr>'
    table_string += '</tbody>'
table_string += '</table>'

print(table_string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM