Last active
February 5, 2024 18:40
-
-
Save t27/48b3ac73a1479914f9fe9383e5d45325 to your computer and use it in GitHub Desktop.
Render JSON as a collapsible field in jupyter notebooks - Updated - Also includes instructions for interleaving with print statements
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
## Add this to the first block in your note book | |
import uuid | |
from IPython.core.display import display, HTML | |
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()) | |
# This line is missed out in most of the versions of this script across the web, it is essential for this to work interleaved with print statements | |
self._ipython_display_() | |
def _ipython_display_(self): | |
display(HTML('<div id="{}" style="height: auto; width:100%;"></div>'.format(self.uuid))) | |
display(HTML("""<script> | |
require(["https://rawgit.com/caldwell/renderjson/master/renderjson.js"], function() { | |
renderjson.set_show_to_level(1) | |
document.getElementById('%s').appendChild(renderjson(%s)) | |
});</script> | |
""" % (self.uuid, self.json_str))) | |
# Since this is copy-pasted wrongly(mostly) at a lot of places across the web, i'm putting the fixed, updated version here, mainly for self-reference | |
## To use this function, call this, this now works even when you have a print statement before or after the RenderJSON call | |
RenderJSON(dict_to_render) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
having the same problem, i am solving