diff --git a/.gitignore b/.gitignore index e2f217b..5339585 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,5 @@ __pycache__ .cache .coverage *.swp +.vscode +.pytest_cache diff --git a/IPython/core/display.py b/IPython/core/display.py index fa64d9c..41d9378 100644 --- a/IPython/core/display.py +++ b/IPython/core/display.py @@ -800,7 +800,7 @@ class JSON(DisplayObject): """ # wrap data in a property, which warns about passing already-serialized JSON _data = None - def __init__(self, data=None, url=None, filename=None, expanded=False, metadata=None, **kwargs): + def __init__(self, data=None, url=None, filename=None, expanded=False, metadata=None, root='root', **kwargs): """Create a JSON display object given raw data. Parameters @@ -817,8 +817,13 @@ class JSON(DisplayObject): Metadata to control whether a JSON display component is expanded. metadata: dict Specify extra metadata to attach to the json display object. + root : str + The name of the root element of the JSON tree """ - self.metadata = {'expanded': expanded} + self.metadata = { + 'expanded': expanded, + 'root': root, + } if metadata: self.metadata.update(metadata) if kwargs: diff --git a/IPython/core/tests/test_display.py b/IPython/core/tests/test_display.py index 5a1b5be..3851d67 100644 --- a/IPython/core/tests/test_display.py +++ b/IPython/core/tests/test_display.py @@ -213,31 +213,41 @@ def test_progress_iter(): def test_json(): d = {'a': 5} lis = [d] - md = {'expanded': False} - md2 = {'expanded': True} - j = display.JSON(d) - j2 = display.JSON(d, expanded=True) - nt.assert_equal(j._repr_json_(), (d, md)) - nt.assert_equal(j2._repr_json_(), (d, md2)) + metadata = [ + {'expanded': False, 'root': 'root'}, + {'expanded': True, 'root': 'root'}, + {'expanded': False, 'root': 'custom'}, + {'expanded': True, 'root': 'custom'}, + ] + json_objs = [ + display.JSON(d), + display.JSON(d, expanded=True), + display.JSON(d, root='custom'), + display.JSON(d, expanded=True, root='custom'), + ] + for j, md in zip(json_objs, metadata): + nt.assert_equal(j._repr_json_(), (d, md)) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") j = display.JSON(json.dumps(d)) nt.assert_equal(len(w), 1) - nt.assert_equal(j._repr_json_(), (d, md)) - nt.assert_equal(j2._repr_json_(), (d, md2)) - - j = display.JSON(lis) - j2 = display.JSON(lis, expanded=True) - nt.assert_equal(j._repr_json_(), (lis, md)) - nt.assert_equal(j2._repr_json_(), (lis, md2)) + nt.assert_equal(j._repr_json_(), (d, metadata[0])) + + json_objs = [ + display.JSON(lis), + display.JSON(lis, expanded=True), + display.JSON(lis, root='custom'), + display.JSON(lis, expanded=True, root='custom'), + ] + for j, md in zip(json_objs, metadata): + nt.assert_equal(j._repr_json_(), (lis, md)) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") j = display.JSON(json.dumps(lis)) nt.assert_equal(len(w), 1) - nt.assert_equal(j._repr_json_(), (lis, md)) - nt.assert_equal(j2._repr_json_(), (lis, md2)) + nt.assert_equal(j._repr_json_(), (lis, metadata[0])) def test_video_embedding(): """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash"""