Created
April 13, 2022 08:06
-
-
Save shaybensasson/be2b1872c341dca62b0f23b7190bcd78 to your computer and use it in GitHub Desktop.
Render JSON (great for hierarchical dicts) for jupyter notebooks
This file contains 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
# https://stackoverflow.com/a/37124230/1640414 | |
# https://caldwell.github.io/renderjson/ | |
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() { | |
renderjson.set_show_to_level("all") | |
document.getElementById('%s').appendChild(renderjson(%s)) | |
}); | |
""" % (self.uuid, self.json_str), raw=True) | |
RenderJSON({'a': [1, 2, 3, 4,], 'b': {'inner1': 'helloworld', 'inner2': 'foobar'}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment