Last active
February 22, 2019 13:54
-
-
Save aseifert/d55d92504396e61147a83058fc7c64f5 to your computer and use it in GitHub Desktop.
collapsible JSON objects 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
class rjson(object): | |
""" | |
(with modifications) from: | |
https://www.reddit.com/r/IPython/comments/34t4m7/lpt_print_json_in_collapsible_format_in_ipython/ | |
which in turn is leveraging http://caldwell.github.io/renderjson/ | |
""" | |
def __init__(self, json_data, open='🔽', close='🔺', level=1): | |
if isinstance(json_data, dict): | |
self.json_str = json.dumps(json_data) | |
else: | |
self.json_str = json | |
self.open = open | |
self.close = close | |
self.level = level | |
self.uuid = str(uuid.uuid4()) | |
def _ipython_display_(self): | |
display_html('<div id="{}" style="width:100%;"></div>'.format(self.uuid), | |
raw=True | |
) | |
display_javascript(""" | |
require(["https://rawgit.com/caldwell/renderjson/master/renderjson.js"], function() { | |
/* https://getemoji.com/ */ | |
renderjson.set_icons('%s', '%s') | |
.set_show_to_level(%d); | |
document.getElementById('%s').appendChild(renderjson(%s)) | |
}); | |
""" % (self.open, self.close, self.level, self.uuid, self.json_str), raw=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this currently doesn't work with jupyter-lab, cf Calysto/calysto_processing#11