Skip to content

Commit

Permalink
Merge pull request #807 from finos/schema-widget
Browse files Browse the repository at this point in the history
Allow client mode widget to be constructed with schema
  • Loading branch information
texodus authored Nov 12, 2019
2 parents 3eae4af + 7ee422b commit 8c812f8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
35 changes: 32 additions & 3 deletions python/perspective/perspective/core/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
# This file is part of the Perspective library, distributed under the terms of
# the Apache License 2.0. The full license can be found in the LICENSE file.
#
import six
import numpy
import pandas
import json
from datetime import datetime
from datetime import date, datetime
from functools import partial, wraps
from ipywidgets import Widget
from traitlets import observe, Unicode
Expand All @@ -18,11 +19,39 @@
from ..table import Table, is_libpsp


def _type_to_string(t):
'''Convert a type object to a string representing a Perspective-supported type.
Redefine here as we can't have any dependencies on libbinding in client mode.
'''
if t in six.integer_types:
return "integer"
elif t is float:
return "float"
elif t is bool:
return "boolean"
elif t is date:
return "date"
elif t is datetime:
return "datetime"
elif t is six.binary_type or t is six.text_type:
return "string"
else:
raise PerspectiveError(
"Unsupported type `{0}` in schema - Perspective supports `int`, `float`, `bool`, `date`, `datetime`, and `str` (or `unicode`).".format(str(t)))


def _serialize(data):
# Attempt to serialize data and pass it to the front-end as JSON
if isinstance(data, list) or isinstance(data, dict):
# grab reference to data if trivially serializable
if isinstance(data, list):
return data
elif isinstance(data, dict):
for v in data.values():
# serialize schema values to string
if isinstance(v, type):
return {k: _type_to_string(data[k]) for k in data}
else:
return data
elif isinstance(data, numpy.recarray):
# flatten numpy record arrays
columns = [data[col] for col in data.dtype.names]
Expand Down
41 changes: 41 additions & 0 deletions python/perspective/perspective/tests/core/test_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
# This file is part of the Perspective library, distributed under the terms of
# the Apache License 2.0. The full license can be found in the LICENSE file.
#
import six
import numpy as np
from datetime import date, datetime
from pytest import raises
from perspective import PerspectiveError, PerspectiveWidget, Table

Expand Down Expand Up @@ -68,6 +70,45 @@ def test_widget_client(self):
assert widget.table is None
assert widget._data == data

def test_widget_client_schema(self):
widget = PerspectiveWidget({
"a": int,
"b": float,
"c": bool,
"d": date,
"e": datetime,
"f": str
}, client=True)
assert widget.table is None
assert widget._data == {
"a": "integer",
"b": "float",
"c": "boolean",
"d": "date",
"e": "datetime",
"f": "string"
}

def test_widget_client_schema_py2_types(self):
if six.PY2:
widget = PerspectiveWidget({
"a": long, # noqa: F821
"b": float,
"c": bool,
"d": date,
"e": datetime,
"f": unicode # noqa: F821
}, client=True)
assert widget.table is None
assert widget._data == {
"a": "integer",
"b": "float",
"c": "boolean",
"d": "date",
"e": "datetime",
"f": "string"
}

def test_widget_client_update(self):
data = {"a": np.arange(0, 50)}
widget = PerspectiveWidget(data, client=True)
Expand Down

0 comments on commit 8c812f8

Please sign in to comment.