Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow client mode widget to be constructed with schema #807

Merged
merged 2 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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