1- from IPython .display import display , JSON
1+ from IPython .display import display , DisplayObject
22import json
3+ import pandas as pd
4+ from .utils import prepare_data
35
46
57# Running `npm run build` will create static resources in the static
@@ -23,22 +25,77 @@ def _jupyter_nbextension_paths():
2325
2426# A display class that can be used within a notebook. E.g.:
2527# from jupyterlab_table import JSONTable
26- # JSONTable(data)
28+ # JSONTable(data, schema )
2729
28- class JSONTable (JSON ):
30+ class JSONTable (DisplayObject ):
2931 """A display class for displaying JSONTable visualizations in the Jupyter Notebook and IPython kernel.
3032
31- JSONTable expects a JSON-able dict , not serialized JSON strings.
33+ JSONTable expects a JSON-able list , not serialized JSON strings.
3234
3335 Scalar types (None, number, string) are not allowed, only dict containers.
3436 """
37+ # wrap data in a property, which warns about passing already-serialized JSON
38+ _data = None
39+ _schema = None
40+ def __init__ (self , data = None , schema = None , url = None , filename = None , metadata = None ):
41+ """Create a JSON Table display object given raw data.
3542
36- def _data_and_metadata (self ):
37- return self .data , self .metadata
38-
43+ Parameters
44+ ----------
45+ data : list
46+ Not an already-serialized JSON string.
47+ Scalar types (None, number, string) are not allowed, only list containers.
48+ schema : dict
49+ JSON Table Schema. See http://frictionlessdata.io/guides/json-table-schema/.
50+ url : unicode
51+ A URL to download the data from.
52+ filename : unicode
53+ Path to a local file to load the data from.
54+ metadata: dict
55+ Specify extra metadata to attach to the json display object.
56+ """
57+ self .schema = schema
58+ self .metadata = metadata
59+ super (JSONTable , self ).__init__ (data = data , url = url , filename = filename )
60+
61+ def _check_data (self ):
62+ if self .data is not None and not isinstance (self .data , (list , pd .DataFrame )):
63+ raise TypeError ("%s expects a JSONable list or pandas DataFrame, not %r" % (self .__class__ .__name__ , self .data ))
64+ if self .schema is not None and not isinstance (self .schema , dict ):
65+ raise TypeError ("%s expects a JSONable dict, not %r" % (self .__class__ .__name__ , self .schema ))
66+
67+ @property
68+ def data (self ):
69+ return self ._data
70+
71+ @property
72+ def schema (self ):
73+ return self ._schema
74+
75+ @data .setter
76+ def data (self , data ):
77+ if isinstance (data , str ):
78+ # warnings.warn("JSONTable expects JSON-able dict or list, not JSON strings")
79+ data = json .loads (data )
80+ self ._data = data
81+
82+ @schema .setter
83+ def schema (self , schema ):
84+ if isinstance (schema , str ):
85+ # warnings.warn("JSONTable expects a JSON-able list, not JSON strings")
86+ schema = json .loads (schema )
87+ self ._schema = schema
88+
3989 def _ipython_display_ (self ):
4090 bundle = {
41- 'application/vnd.dataresource+json' : self .data ,
91+ 'application/vnd.dataresource+json' : {
92+ 'resources' : [
93+ {
94+ 'schema' : self .schema ,
95+ 'data' : prepare_data (self .data )
96+ }
97+ ]
98+ },
4299 'text/plain' : '<jupyterlab_table.JSONTable object>'
43100 }
44101 metadata = {
0 commit comments