Skip to content

Commit

Permalink
Various Vega pane fixes (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Nov 14, 2019
1 parent 1d3583c commit 0279e6c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
12 changes: 6 additions & 6 deletions panel/models/vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ class VegaPlot(LayoutDOM):
a Bokeh plot.
"""

__javascript__ = ["https://cdn.jsdelivr.net/npm/vega@5.3.1",
'https://cdn.jsdelivr.net/npm/vega-lite@3.2.1',
'https://cdn.jsdelivr.net/npm/vega-embed@4.0.0-rc1']
__javascript__ = ["https://cdn.jsdelivr.net/npm/vega@5",
'https://cdn.jsdelivr.net/npm/vega-lite@3',
'https://cdn.jsdelivr.net/npm/vega-embed@6']

__js_require__ = {
'baseUrl': 'https://cdn.jsdelivr.net/npm/',
'paths': {
"vega-embed": "vega-embed@4.0.0/build/vega-embed.min",
"vega-lite": "vega-lite@3.2.1/build/vega-lite.min",
"vega": "vega@5.3.1/build/vega.min"
"vega-embed": "vega-embed@6/build/vega-embed.min",
"vega-lite": "vega-lite@3/build/vega-lite.min",
"vega": "vega@5/build/vega.min"
},
'exports': {'vega-embed': 'vegaEmbed', 'vega': 'vega', 'vega-lite': 'vl'}
}
Expand Down
24 changes: 0 additions & 24 deletions panel/models/vega.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import * as p from "@bokehjs/core/properties"
import {HTMLBox, HTMLBoxView} from "@bokehjs/models/layouts/html_box"

function get_file(file: string, callback: any): void {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', file, true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == 200) {
callback(xobj.responseText);
}
};
xobj.send(null);
}

export class VegaPlotView extends HTMLBoxView {
model: VegaPlot
_connected: string[]
Expand Down Expand Up @@ -58,12 +46,6 @@ export class VegaPlotView extends HTMLBoxView {
this._plot()
}

_receive_file(data: string, format: string): void {
const values = (format === 'json') ? JSON.parse(data): data;
this.model.data.data = {values: values, format: {type: format}};
this._plot();
}

_plot(): void {
if (!this.model.data || !(window as any).vegaEmbed)
return
Expand All @@ -75,12 +57,6 @@ export class VegaPlotView extends HTMLBoxView {
}
this.model.data['datasets'] = datasets
}
if (this.model.data.data && this.model.data.data.url) {
const url = this.model.data.data.url;
const url_components = url.split('.');
const format = url_components[url_components.length-1];
get_file(this.model.data.data.url, (result: string) => this._receive_file(result, format))
}
(window as any).vegaEmbed(this.el, this.model.data, {actions: false})
}
}
Expand Down
30 changes: 28 additions & 2 deletions panel/pane/vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from bokeh.models import ColumnDataSource
from pyviz_comms import JupyterComm

from ..viewable import Layoutable
from .base import PaneBase


Expand Down Expand Up @@ -74,7 +75,7 @@ def _get_sources(self, json, sources):
columns = set(data[0]) if data else []
if self.is_altair(self.object):
import altair as alt
if (not isinstance(self.object.data, alt.Data) and
if (not isinstance(self.object.data, (alt.Data, alt.UrlData)) and
columns == set(self.object.data)):
data = ColumnDataSource.from_df(self.object.data)
else:
Expand All @@ -86,6 +87,26 @@ def _get_sources(self, json, sources):
if data:
sources['data'] = ColumnDataSource(data=ds_as_cds(data))


@classmethod
def _get_dimensions(cls, json, props):
if json is None:
return

view = {}
if 'width' in json:
view['width'] = json['width']
if 'height' in json:
view['height'] = json['height']
if 'config' in json and 'view' in json['config']:
view = json['config']['view']
for p in ('width', 'height'):
if p not in view:
continue
if props.get(p) is None or p in view and props.get(p) < view[p]:
v = view[p]
props[p] = v+22 if isinstance(v, int) else v

def _get_model(self, doc, root=None, parent=None, comm=None):
if 'panel.models.vega' not in sys.modules:
if isinstance(comm, JupyterComm):
Expand All @@ -105,6 +126,7 @@ def _get_model(self, doc, root=None, parent=None, comm=None):
json = self._to_json(self.object)
self._get_sources(json, sources)
props = self._process_param_change(self._init_properties())
self._get_dimensions(json, props)
model = VegaPlot(data=json, data_sources=sources, **props)
if root is None:
root = model
Expand All @@ -117,4 +139,8 @@ def _update(self, model):
else:
json = self._to_json(self.object)
self._get_sources(json, model.data_sources)
model.data = json
props = {p : getattr(self, p) for p in list(Layoutable.param)
if getattr(self, p) is not None}
self._get_dimensions(json, props)
props['data'] = json
model.update(**props)

0 comments on commit 0279e6c

Please sign in to comment.