Python中json转成html table

最近,项目中有个需求,将json数据转成html table的形式。起初打算自己写个模块的,但是一搜索别人都已经写好了,完全满足需求,下面介绍一下方法。

安装json2html

1
pip install json2html

用法举例

例1,基础用法:

1
2
3
4
5
6
from json2html import *
input = {
"name": "json2html",
"description": "Converts JSON to HTML tabular representation"
}
json2html.convert(json = input)

输出结果:

1
2
3
4
5
6
7
8
9
10
<table border="1">
<tr>
<th>name</th>
<td>json2html</td>
</tr>
<tr>
<th>description</th>
<td>converts JSON to HTML tabular representation</td>
</tr>
</table>

例2,为table设置自定义属性:

1
2
3
4
5
6
from json2html import *
input = {
"name": "json2html",
"description": "Converts JSON to HTML tabular representation"
}
json2html.convert(json = input, table_attributes="id=\"info-table\" class=\"table table-bordered table-hover\"")

输出结果:

1
2
3
4
5
6
7
8
9
10
<table id="info-table" class="table table-bordered table-hover">
<tr>
<th>name</th>
<td>json2html</td>
</tr>
<tr>
<th>description</th>
<td>Converts JSON to HTML tabular representation</td>
</tr>
</table>

其它更详细更高级用法,访问:https://github.com/softvar/json2html

大师兄 wechat
欢迎关注我的微信公众号:Python大师兄