Skip to content

Instantly share code, notes, and snippets.

@aseifert
Last active February 22, 2019 13:54
Show Gist options
  • Save aseifert/d55d92504396e61147a83058fc7c64f5 to your computer and use it in GitHub Desktop.
Save aseifert/d55d92504396e61147a83058fc7c64f5 to your computer and use it in GitHub Desktop.
collapsible JSON objects for Jupyter notebooks
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)
@aseifert
Copy link
Author

this currently doesn't work with jupyter-lab, cf Calysto/calysto_processing#11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment