Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IPython笔记本中漂亮的JSON格式_Python_Json_Ipython Notebook - Fatal编程技术网

IPython笔记本中漂亮的JSON格式

IPython笔记本中漂亮的JSON格式,python,json,ipython-notebook,Python,Json,Ipython Notebook,是否有一种现有的方法可以使json.dumps()输出在ipython笔记本中显示为“漂亮”格式的json json.dumps有一个indent参数,打印结果就足够了: print(json.dumps(obj, indent=2)) 我发现这个页面正在寻找一种方法来消除输出中的文字\ns。我们正在用Jupyter做一个编码访谈,我想用一种方法来显示函数的结果,就像perty一样。我的Jupyter版本(4.1.0)不会将它们渲染为实际的换行符。我提出的解决方案是(我有点希望这不是最好的方法

是否有一种现有的方法可以使
json.dumps()
输出在ipython笔记本中显示为“漂亮”格式的json

json.dumps
有一个
indent
参数,打印结果就足够了:

print(json.dumps(obj, indent=2))

我发现这个页面正在寻找一种方法来消除输出中的文字
\n
s。我们正在用Jupyter做一个编码访谈,我想用一种方法来显示函数的结果,就像perty一样。我的Jupyter版本(4.1.0)不会将它们渲染为实际的换行符。我提出的解决方案是(我有点希望这不是最好的方法,但是…)

我希望这对别人有帮助

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)
从此处粘贴的副本:


Github:

这可能与OP要求的略有不同,但您可以使用
IPython.display.JSON
以交互方式查看JSON/
dict
对象

from IPython.display import JSON
JSON({'a': [1, 2, 3, 4,], 'b': {'inner1': 'helloworld', 'inner2': 'foobar'}})
编辑:这适用于氢气和JupyterLab,但不适用于Jupyter笔记本或IPython终端

内部:


我只是将扩展变量添加到@Kyle Barron答案中:

from IPython.display import JSON
JSON(json_object, expanded=True)

输出中的颜色如何?然而,在这种方法中,汉字与Unicode弄乱了。这根本不是真的。Python的
json.dumps
默认为
确保ascii=True
,它转义中文(或实际上:任何非ascii)代码点。使用
print(json.dumps(obj,indent=2,确保_ascii=False))
您将使它们不被替换。我无法让它在Jupyter 1.0上工作。知道为什么吗?Javascript控制台显示:“SyntaxError:意外标记代码中存在问题。第10行应更改为
self.json\u str=json\u data
。它解决了@user474491报告的问题。@Hassan:已修复!谢谢你的建议。我已经更新了一些问题,并在最新的Jupyter笔记本上实现了这一点,包括@user474491和@Hassan的建议,我还在
\u init\u()
中添加了对
\u ipython\u display()
的显式调用,以确保我们可以安全地交错调用python的本机
print()
RenderJSON()
并且仍然可以使用它,如果它接受
列表
类型,它会更好,因为
考德威尔/RenderJSON
完全能够呈现json数组它对我不起作用
类型错误:\uu init\uuuu()得到了一个意外的关键字参数“扩展”
这对我来说很好。使用“expanded”关键字时没有错误。我的IPython.版本显示“7.8.0”。我从未如此快乐过!让我困惑的是,这在Jupyter实验室有效,但在好的旧笔记本中却不起作用。如果存在,它将在那里调用repr_json。
from IPython.display import JSON
JSON({'a': [1, 2, 3, 4,], 'b': {'inner1': 'helloworld', 'inner2': 'foobar'}})
from IPython.display import JSON
JSON(json_object, expanded=True)