Skip to content

Commit

Permalink
Merge pull request #49 from open-contracting/40-render-json
Browse files Browse the repository at this point in the history
add render_json method
  • Loading branch information
jpmckinney committed Aug 9, 2021
2 parents c8d47d5 + 1acb385 commit 083269a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

0.3.5 (2021-07-16)
------------------

Added
~~~~~

- Add :meth:`~ocdskingfishercolab.render_json` to render JSON into collapsible HTML.

0.3.4 (2021-04-16)
------------------

Expand Down
21 changes: 21 additions & 0 deletions ocdskingfishercolab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
files.download.return_value = None
from gspread_dataframe import set_with_dataframe
from IPython import get_ipython
from IPython.display import HTML
from libcoveocds.config import LibCoveOCDSConfig
from notebook import notebookapp
from oauth2client.client import GoogleCredentials
Expand Down Expand Up @@ -342,6 +343,26 @@ def _save_file_to_drive(metadata, filename):
return drive_file


def render_json(json_string):
"""
Renders JSON into collapsible HTML.
:param json_string: JSON-deserializable string
"""
if not isinstance(json_string, str):
json_string = json.dumps(json_string)
return HTML(f"""
<script
src="https://cdn.jsdelivr.net/gh/caldwell/renderjson@master/renderjson.js">
</script>
<script>
renderjson.set_show_to_level(1)
document.body.appendChild(renderjson({json_string}))
new ResizeObserver(google.colab.output.resizeIframeToContent).observe(document.body)
</script>
""")


class OCDSKingfisherColabError(Exception):
"""Base class for exceptions from within this package"""

Expand Down

0 comments on commit 083269a

Please sign in to comment.