Skip to content

Commit

Permalink
Merge pull request #450 from Kitware/dynamic-pycandela
Browse files Browse the repository at this point in the history
Fixup pycandela initialization, validate pycandela docs
  • Loading branch information
jeffbaumes committed Oct 18, 2016
2 parents 1da25f4 + bbd5c49 commit 1063920
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 46 deletions.
4 changes: 2 additions & 2 deletions docs/components/barchart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ Example

.. code-block:: python
import candela
import pycandela
data = [{'a': d, 'b': d} for d in range(10)]
candela.components.BarChart(data=data, x='a', y='b')
pycandela.components.BarChart(data=data, x='a', y='b')
**R**

Expand Down
4 changes: 2 additions & 2 deletions docs/components/boxplot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ Example

.. code-block:: python
import candela
import pycandela
data = [{'a': d, 'b': d/2 + 7} for d in range(10)]
candela.components.BoxPlot(data=data, fields=['a', 'b'])
pycandela.components.BoxPlot(data=data, fields=['a', 'b'])
**R**

Expand Down
4 changes: 2 additions & 2 deletions docs/components/bulletchart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ Example

.. code-block:: python
import candela
import pycandela
candela.components.BulletChart(
pycandela.components.BulletChart(
value=0.8,
title='My measurement',
subtitle='... it is really important',
Expand Down
4 changes: 2 additions & 2 deletions docs/components/ganttchart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Example

.. code-block:: python
import candela
import pycandela
data = [
dict(name='Do this', level=1, start=0, end=5),
Expand All @@ -71,7 +71,7 @@ Example
dict(name='That part 1', level=2, start=5, end=10),
dict(name='That part 2', level=2, start=10, end=15)
];
candela.components.GanttChart(
pycandela.components.GanttChart(
data=data, label='name',
start='start', end='end', level='level',
width=700, height=200
Expand Down
4 changes: 2 additions & 2 deletions docs/components/geo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ Example

.. code-block:: python
import candela
import pycandela
data = [
dict(lat=41.702, lng=-87.644),
dict(lat=41.617, lng=-87.693),
dict(lat=41.715, lng=-87.712)
]
candela.components.Geo(
pycandela.components.Geo(
map=dict(
zoom=10,
center=dict(x=-87.6194, y=41.867516)
Expand Down
4 changes: 2 additions & 2 deletions docs/components/geodots.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ Example

.. code-block:: python
import candela
import pycandela
data = [
dict(lat=41.702, lng=-87.644, a=5),
dict(lat=41.617, lng=-87.693, a=15),
dict(lat=41.715, lng=-87.712, a=25)
]
candela.components.GeoDots(
pycandela.components.GeoDots(
zoom=10,
center=dict(longitude=-87.6194, latitude=41.867516),
data=data,
Expand Down
4 changes: 2 additions & 2 deletions docs/components/heatmap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ Example

.. code-block:: python
import candela
import pycandela
data = [{'a': d, 'b': 10 - d, 'name': d} for d in range(10)]
candela.components.Heatmap(
pycandela.components.Heatmap(
data=data, fields=['a', 'b'], id='name', width=700, height=400)
**R**
Expand Down
4 changes: 2 additions & 2 deletions docs/components/histogram.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ Example

.. code-block:: python
import candela
import pycandela
from random import normalvariate as nv
data = [{'a': nv(0, 1)} for d in range(1000)]
candela.Histogram(data=data, bin='a', width=700, height=400)
pycandela.components.Histogram(data=data, bin='a', width=700, height=400)
**R**

Expand Down
4 changes: 2 additions & 2 deletions docs/components/linechart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ Example

.. code-block:: python
import candela
import pycandela
data = [{'a': d, 'b': d} for d in range(10)]
candela.components.LineChart(
pycandela.components.LineChart(
data=data, x='a', y=['b'], width=700, height=400)
**R**
Expand Down
4 changes: 2 additions & 2 deletions docs/components/lineup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ Example

.. code-block:: python
import candela
import pycandela
data = [{'a': d, 'b': 10 - d, 'name': d} for d in range(10)]
candela.components.LineUp(data=data, fields=['a', 'b'])
pycandela.components.LineUp(data=data, fields=['a', 'b'])
**R**

Expand Down
4 changes: 2 additions & 2 deletions docs/components/scatterplot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ Example

.. code-block:: python
import candela
import pycandela
data = [{'a': d, 'b': d} for d in range(10)]
candela.components.ScatterPlot(
pycandela.components.ScatterPlot(
data=data, x='a', y='b', width=700, height=400)
**R**
Expand Down
4 changes: 2 additions & 2 deletions docs/components/scatterplotmatrix.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ Example

.. code-block:: python
import candela
import pycandela
data = [{'a': d, 'b': 10 - d, 'name': d} for d in range(10)]
candela.components.ScatterPlotMatrix(data=data, fields=['a', 'b'])
pycandela.components.ScatterPlotMatrix(data=data, fields=['a', 'b'])
**R**

Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Quick start - Python

import pycandela
pycandela.components.ScatterPlot(
data=df, color='species', x='sepalLength', y='sepalWidth')
data=data, color='species', x='sepalLength', y='sepalWidth')

Quick start - R
---------------
Expand Down
49 changes: 28 additions & 21 deletions python/pycandela/pycandela/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@

_component_list = []

js = """
require.config({
paths: {
candela: 'https://unpkg.com/candela/dist/candela'
},
urlArgs: null
});
(function (el) {
_require_config = """
if (!window.__pycandela_config) {
require.config({
paths: {
candela: 'https://unpkg.com/candela/dist/candela'
},
urlArgs: null
});
window.__pycandela_config = true;
}
"""


def init():
init_js = _require_config + """
require(['candela'], function (candela) {
var components = [];
for (var comp in candela.components) {
Expand All @@ -31,12 +37,11 @@
kernel.execute('__pycandela._component_list = ' +
JSON.stringify(components));
}, function (error) {
el.append('<pre>' + error + '</pre>');
element.append('<pre>' + error + '</pre>');
});
})(element);
"""
"""

publish_display_data({'application/javascript': js})
publish_display_data({'application/javascript': init_js})


class DataFrameEncoder(json.JSONEncoder):
Expand All @@ -53,14 +58,18 @@ def __init__(self, name, **kwargs):

def _ipython_display_(self):
js = ("""
(function (el) {
var render = function () {
%s
require(['candela'], function (candela) {
var comp = candela.components['%s'];
var vis = new comp(el.get(0), %s);
var vis = new comp(element.get(0), %s);
vis.render();
});
})(element);
""" % (self.name, json.dumps(self.options, cls=DataFrameEncoder)))
};
render();
""" % (_require_config, self.name,
json.dumps(self.options, cls=DataFrameEncoder)))

publish_display_data({'application/javascript': js})

def display(self):
Expand All @@ -72,10 +81,8 @@ def __dir__(self):
return _component_list

def __getattr__(self, name):
if name not in _component_list:
raise AttributeError(
"'" + type(self).__name__ +
"' class has no attribute '" + name + "'")
return partial(Component, name)

components = ComponentAccessor()

init()

0 comments on commit 1063920

Please sign in to comment.