Skip to content

Commit f121a9a

Browse files
committed
Use df.to_json(orient='table'
1 parent aa4573a commit f121a9a

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

jupyterlab_table/__init__.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,7 @@ def schema(self, schema):
8989
def _ipython_display_(self):
9090
bundle = {
9191
'application/vnd.dataresource+json': {
92-
'resources': [
93-
{
94-
'schema': self.schema,
95-
'data': prepare_data(self.data)
96-
}
97-
]
92+
'resources': [prepare_data(self.data, self.schema)]
9893
},
9994
'text/plain': '<jupyterlab_table.JSONTable object>'
10095
}

jupyterlab_table/utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import codecs
33
import collections
44
import os.path
5+
import json
56
import pandas as pd
67

78

@@ -72,10 +73,14 @@ def sanitize_dataframe(df):
7273
return df
7374

7475

75-
def prepare_data(data=None):
76-
"""Prepare Plotly data from Pandas DataFrame."""
76+
def prepare_data(data=None, schema=None):
77+
"""Prepare JSONTable data from Pandas DataFrame."""
7778

78-
if isinstance(data, list):
79-
return data
80-
data = sanitize_dataframe(data)
81-
return data.to_dict(orient='records')
79+
if isinstance(data, pd.DataFrame):
80+
data = sanitize_dataframe(data)
81+
data = data.to_json(orient='table')
82+
return json.loads(data)
83+
return {
84+
'data': data,
85+
'schema': schema
86+
}

0 commit comments

Comments
 (0)