Skip to content

Instantly share code, notes, and snippets.

@shaybensasson
Created April 13, 2022 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaybensasson/be2b1872c341dca62b0f23b7190bcd78 to your computer and use it in GitHub Desktop.
Save shaybensasson/be2b1872c341dca62b0f23b7190bcd78 to your computer and use it in GitHub Desktop.
Render JSON (great for hierarchical dicts) for jupyter notebooks
# 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