Last active
February 26, 2023 04:31
-
-
Save nerevar/a068ee373e22391ad3a1413b3e554fb5 to your computer and use it in GitHub Desktop.
Pretty print json in jupyter/ipython notebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import uuid | |
from IPython.display import display_javascript, display_html, display | |
class RenderJSON(object): | |
def __init__(self, json_data): | |
if isinstance(json_data, dict) or isinstance(json_data, list): | |
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%;font: 12px/18px monospace !important;"></div>'.format(self.uuid), raw=True) | |
display_javascript(""" | |
require(["https://rawgit.com/caldwell/renderjson/master/renderjson.js"], function() { | |
renderjson.set_show_to_level(2); | |
document.getElementById('%s').appendChild(renderjson(%s)) | |
}); | |
""" % (self.uuid, self.json_str), raw=True) | |
RenderJSON([ | |
{ | |
"a": 1 | |
}, | |
{ | |
"b": 2, | |
"in1": { | |
"key": "value" | |
} | |
} | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment