有没有一种现有的方法可以让json.dumps()
输出在ipython notebook中显示为“漂亮的”格式的JSON?
发布于 2013-09-18 21:08:03
json.dumps
有一个indent
参数,打印结果应该足够了:
print(json.dumps(obj, indent=2))
发布于 2018-07-31 23:09:26
这可能与OP所要求的稍有不同,但是您可以使用IPython.display.JSON
以交互方式查看JSON/dict
对象。
from IPython.display import JSON
JSON({'a': [1, 2, 3, 4,], 'b': {'inner1': 'helloworld', 'inner2': 'foobar'}})
编辑:这适用于H2和JupyterLab,但不适用于Jupyter Notebook或IPython终端。
Hydrogen内幕
发布于 2016-05-10 03:47:15
import uuid
from IPython.display import display_javascript, display_html, display
import json
class RenderJSON(object):
def __init__(self, json_data):
if isinstance(json_data, dict):
self.json_str = json.dumps(json_data)
else:
self.json_str = json_data
self.uuid = str(uuid.uuid4())
def _ipython_display_(self):
display_html('<div id="{}" style="height: 600px; width:100%;"></div>'.format(self.uuid), raw=True)
display_javascript("""
require(["https://rawgit.com/caldwell/renderjson/master/renderjson.js"], function() {
document.getElementById('%s').appendChild(renderjson(%s))
});
""" % (self.uuid, self.json_str), raw=True)
要以可折叠格式输出数据:
RenderJSON(your_json)
从此处粘贴的副本:https://www.reddit.com/r/IPython/comments/34t4m7/lpt_print_json_in_collapsible_format_in_ipython/
https://stackoverflow.com/questions/18873066
复制相似问题