Skip to content

Commit

Permalink
allow for plugin_config to be set from jupyterlab
Browse files Browse the repository at this point in the history
  • Loading branch information
sc1f committed Jul 1, 2021
1 parent 21144ef commit 48195bc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
18 changes: 12 additions & 6 deletions packages/perspective-jupyterlab/src/ts/psp_widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export interface PerspectiveWidgetOptions extends PerspectiveViewerOptions {
server?: boolean;
title?: string;
bindto?: HTMLElement;
plugin_config?: PerspectiveViewerOptions;

// these shouldn't exist, PerspectiveViewerOptions should be sufficient e.g.
// ["row-pivots"]
Expand Down Expand Up @@ -66,7 +65,7 @@ export class PerspectiveWidget extends Widget {
const sort: Sort = options.sort || [];
const filters: Filters = options.filters || [];
const expressions: Expressions = options.expressions || options["expressions"] || [];
const plugin_config: PerspectiveViewerOptions = options.plugin_config || {};
const plugin_config: object = options.plugin_config || {};
const dark: boolean = options.dark || false;
const editable: boolean = options.editable || false;
const server: boolean = options.server || false;
Expand Down Expand Up @@ -300,13 +299,20 @@ export class PerspectiveWidget extends Widget {
}
}

get plugin_config(): PerspectiveViewerOptions {
// `plugin_config` cannot be synchronously read from the viewer, as it is
// not part of the attribute API and only emitted from save(). Users can
// pass in a plugin config and have it applied to the viewer, but they
// cannot read the current `plugin_config` of the viewer if it has not
// already been set from Python.
get plugin_config(): object {
return this._plugin_config;
}
set plugin_config(plugin_config: PerspectiveViewerOptions) {
set plugin_config(plugin_config: object) {
this._plugin_config = plugin_config;

// Allow plugin configs passed from Python to take effect on the viewer
if (this._plugin_config) {
this.viewer.restore(this._plugin_config);
this.viewer.restore({plugin_config: this._plugin_config});
}
}

Expand Down Expand Up @@ -414,7 +420,7 @@ export class PerspectiveWidget extends Widget {
}

private _viewer: HTMLPerspectiveViewerElement;
private _plugin_config: PerspectiveViewerOptions;
private _plugin_config: object;
private _client: boolean;
private _server: boolean;
private _dark: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/perspective-jupyterlab/src/ts/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class PerspectiveDocumentWidget extends DocumentWidget<PerspectiveWidget>
this.context.model.fromString(resultAsB64);
this.context.save();
} else if (this._type === "json") {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result: any = await view.to_json();
this.context.model.fromJSON(result);
this.context.save();
Expand Down
19 changes: 18 additions & 1 deletion packages/perspective-jupyterlab/test/jupyter/widget.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ utils.with_jupyterlab(process.env.__JUPYTERLAB_PORT__, () => {
return tbl.querySelector("thead tr").childElementCount;
});

expect(num_columns).toEqual(8);
expect(num_columns).toBeGreaterThanOrEqual(8);

const num_rows = await viewer.evaluate(async viewer => {
const tbl = viewer.querySelector("regular-table");
Expand Down Expand Up @@ -195,6 +195,23 @@ utils.with_jupyterlab(process.env.__JUPYTERLAB_PORT__, () => {
expect(dimensions[1]).toEqual("100px");
}
);

test.jupyterlab(
"Sets plugin config",
[["table = perspective.Table(arrow_data)\n", "w = perspective.PerspectiveWidget(table)"], ["w"], ["w.columns = ['f64']\n", "w.plugin_config = {'f64': {'fixed': 10}}"]],
async page => {
const viewer = await default_body(page);
const plugin_config = await viewer.evaluate(async viewer => {
const config = await viewer.save();
return config.plugin_config;
});
expect(plugin_config).toEqual({
f64: {
fixed: 10
}
});
}
);
},
{name: "Simple", root: path.join(__dirname, "..", "..")}
);
Expand Down
9 changes: 5 additions & 4 deletions packages/perspective-viewer/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface HTMLPerspectiveViewerElement extends PerspectiveViewerOptions,
download(flat: boolean): Promise<any>;
copy(flat: boolean): Promise<void>;
save(): Promise<PerspectiveViewerOptions>;
restore(x: any): Promise<void>;
restore(x: PerspectiveViewerOptions): Promise<void>;
reset(): void;
notifyResize(): void;
restyleElement(): void;
Expand All @@ -36,15 +36,16 @@ export type Pivots = string[];
export type Columns = string[];

export interface PerspectiveViewerOptions {
aggregates?: Aggregates;
editable?: boolean;
plugin?: string;
columns?: Columns;
expressions?: Expressions;
"row-pivots"?: Pivots;
"column-pivots"?: Pivots;
aggregates?: Aggregates;
filters?: Filters;
sort?: Sort;
expressions?: Expressions;
plugin_config?: object;
editable?: boolean;
selectable?: boolean;
}

Expand Down
16 changes: 16 additions & 0 deletions python/perspective/perspective/tests/viewer/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,19 @@ def test_save_restore(self):
assert viewer.plugin == "X Bar"
assert viewer.editable is True
assert viewer.expressions == ['"a" * 2']

def test_save_restore_plugin_config(self):
viewer = PerspectiveViewer(plugin="datagrid", plugin_config={"a": {"fixed": 4}})
config = viewer.save()

assert config["plugin_config"] == {
"a": {
"fixed": 4
}
}

viewer.reset()
assert viewer.plugin_config == {}

viewer.restore(**config)
assert viewer.plugin_config == config["plugin_config"]
5 changes: 4 additions & 1 deletion python/perspective/perspective/viewer/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class PerspectiveViewer(PerspectiveTraitlets, object):
"expressions",
"plugin",
"editable",
"plugin_config",
)

def __init__(
Expand Down Expand Up @@ -93,7 +94,8 @@ def __init__(
expressions which are applied to the view.
plugin (:obj:`str`/:obj:`perspective.Plugin`): Which plugin to
select by default.
plugin_config (:obj:`dict`): Custom config for all plugins by name.
plugin_config (:obj:`dict`): A configuration for the plugin, i.e.
the datagrid plugin or a chart plugin.
dark (:obj:`bool`): Whether to invert the color theme.
editable (:obj:`bool`): Whether to allow editability using the grid.
Expand Down Expand Up @@ -265,6 +267,7 @@ def reset(self):
self.aggregates = {}
self.columns = []
self.plugin = "datagrid"
self.plugin_config = {}
self.editable = False

def delete(self, delete_table=True):
Expand Down

0 comments on commit 48195bc

Please sign in to comment.