Breddels EWASS2017ipywidgets Demo
Breddels EWASS2017ipywidgets Demo
In [1]: 1+3
Out[1]: 4
In [3]: #IPython.display.Image(url="https://apod.nasa.gov/apod/image/1703/OrionTrapezium_Hubble_
IPython.display.Image(url="demo.png")
In [4]: %%html
<button>click</button>
<IPython.core.display.HTML object>
In [5]: %%html
<button onclick='alert("hi")'>click</button>
<IPython.core.display.HTML object>
1
In [7]: button = widgets.Button(description="hi")
In [8]: button
hi
hi
hi
In [12]: slider.value
Out[12]: 7.5
In [13]: slider.value = 2
In [16]: text.value
Out[16]: 1.0
2
3 Making your own Widget
• http://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Custom.html
In [22]: %%javascript
require.undef('hello'); // if we execute this cell twice, we want to lose the old defin
// define the module, which has the same name as _view_module above
define('hello', ["jupyter-js-widgets"], function(widgets) {
var HelloView = widgets.DOMWidgetView.extend({
render: function() {
this.update_value()
this.model.on('change:value', this.update_value, this);
},
update_value: function() {
this.el.textContent = this.model.get('value');
},
});
return {
HelloView : HelloView // the key has the same name as _view_name above
};
});
<IPython.core.display.Javascript object>
In [23]: hi = HelloWidget()
In [24]: hi
# Other widgets * gmaps (google maps) * ipyleaflet (open streetview and more) * pythreejs (3d
visualization / WebGL wrapper) * ipyvolume (3d visualization / scientific) * bqplot (2d plotting)
* Soon? aladin lite (Thomas Boch/CDS, sky atlas)
3
4 Bqplot
• Interactive plotting in the Jupyter notebook
• Documentation: https://bqplot.readthedocs.io
• Github: https://github.com/bloomberg/bqplot
x = np.linspace(0, 2, 50)
y = x**2
fig = plt.figure()
scatter = plt.scatter(x, y)
plt.show()
In [30]: scatter.selected
4
import scipy.ndimage
cube = scipy.ndimage.gaussian_filter(cube, 2.5)
cube.shape
In [34]: p3.figure()
p3.volshow(cube, level=[0.3, 0.11, 0.19],
opacity=[0.2, 0.1, 0.06], level_width=0.05, data_min=0.03, data_max=0
p3.show()
In [36]: p3.figure()
scatter = p3.scatter(x, y, z, marker='sphere')
p3.show()
In [37]: scatter.size = 1
In [42]: p3.save("example.html")
!open example.html
5
In [49]: p3.figure()
p3.style.use('dark')
quiver = p3.quiver(*ipyvolume.datasets.animated_stream.fetch().data[:,::,::4], size=5)
p3.animate_glyphs(quiver, interval=200)
p3.show()
In [50]: p3.style.use('light')
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
6 Conclusions
• Jupyter notebook is an interactive environment, to mix code, text, equations, plots
• With ipywidget you can build small GUI’s, e.g. to play with variables (model parameter for
instance)
• ipyvolume brings 3d visualization, volume rendering, scatter, quiver, and animations
• Widgets can be rendered outside the notebook, live documentation, share with collegues,
bring to a meeting, press material
In [ ]: