| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"In this tutorial we'll learn how to build a compound widget of the [Gaussian Density](Gaussian%20Density.ipynb) example. \n", |
| 8 |
"\n", |
| 9 |
"Steps to build a compound widget:\n", |
| 10 |
"1. Create a class and extend it from DOMWidget or its subclasses. (If you are building a dashboard for example using HBox, VBox etc. then it makes sense to extend the class from Box, HBox or VBox as needed)\n", |
| 11 |
"2. Inside the class constructor call the super class constructor passing in the widgets as children\n", |
| 12 |
"3. Register any widget callbacks inside the constructor and define callbacks as class methods\n", |
| 13 |
"\n", |
| 14 |
"Once the compound widget is defined in a class, it can be put in a library and used along with other interactive widgets! Compound widgets are an excellent way of building reusable components (**using just python code**) which can be seamlessly integrated with `ipywidgets`, `bqplot` and other interactive widgets." |
| 15 |
] |
| 16 |
}, |
| 17 |
{ |
| 18 |
"cell_type": "code", |
| 19 |
"execution_count": null, |
| 20 |
"metadata": {}, |
| 21 |
"outputs": [], |
| 22 |
"source": [ |
| 23 |
"import numpy as np\n", |
| 24 |
"from scipy.stats import norm\n", |
| 25 |
"\n", |
| 26 |
"import ipywidgets as widgets\n", |
| 27 |
"import bqplot.pyplot as plt" |
| 28 |
] |
| 29 |
}, |
| 30 |
{ |
| 31 |
"cell_type": "code", |
| 32 |
"execution_count": null, |
| 33 |
"metadata": {}, |
| 34 |
"outputs": [], |
| 35 |
"source": [ |
| 36 |
"# extend the class from layout class VBox\n", |
| 37 |
"class GaussianDensity(widgets.VBox):\n", |
| 38 |
" def __init__(self, *args, **kwargs): \n", |
| 39 |
" x = np.linspace(-10, 10, 200)\n", |
| 40 |
" y = norm.pdf(x)\n", |
| 41 |
"\n", |
| 42 |
" # gaussian density figure\n", |
| 43 |
" self.title_tmpl = 'Gaussian Density (mu = {} and sigma = {})'\n", |
| 44 |
" self.pdf_fig = plt.figure(title=self.title_tmpl.format(0, 1), \n", |
| 45 |
" layout=widgets.Layout(width='800px', height='600px'))\n", |
| 46 |
" self.pdf_line = plt.plot(x, y, 'm', stroke_width=3)\n", |
| 47 |
" \n", |
| 48 |
" # sliders for mu and sigma \n", |
| 49 |
" self.mu_slider = widgets.FloatSlider(description='$\\mu$', value=0, min=-5, max=5, step=.1)\n", |
| 50 |
" self.sigma_slider = widgets.FloatSlider(description='$\\sigma$', value=1, min=0.1, max=5, step=.1)\n", |
| 51 |
" slider_layout = widgets.HBox([self.mu_slider, self.sigma_slider])\n", |
| 52 |
" \n", |
| 53 |
" # register callback with the 'value' trait of the sliders\n", |
| 54 |
" for slider in [self.mu_slider, self.sigma_slider]:\n", |
| 55 |
" slider.observe(self.update_density, 'value')\n", |
| 56 |
" \n", |
| 57 |
" # call super construction by passing in the widgets as children\n", |
| 58 |
" super(GaussianDensity, self).__init__(children=[self.pdf_fig, slider_layout])\n", |
| 59 |
" \n", |
| 60 |
" def update_density(self, change):\n", |
| 61 |
" new_mu = self.mu_slider.value\n", |
| 62 |
" new_sigma = self.sigma_slider.value\n", |
| 63 |
" # update the y attribute of the plot with the new pdf\n", |
| 64 |
" # computed using new mu and sigma values\n", |
| 65 |
" self.pdf_line.y = norm.pdf(self.pdf_line.x, new_mu, new_sigma)\n", |
| 66 |
"\n", |
| 67 |
" # also update the fig title\n", |
| 68 |
" self.pdf_fig.title = self.title_tmpl.format(new_mu, new_sigma)" |
| 69 |
] |
| 70 |
}, |
| 71 |
{ |
| 72 |
"cell_type": "code", |
| 73 |
"execution_count": null, |
| 74 |
"metadata": {}, |
| 75 |
"outputs": [], |
| 76 |
"source": [ |
| 77 |
"gaussian_density_widget = GaussianDensity()" |
| 78 |
] |
| 79 |
}, |
| 80 |
{ |
| 81 |
"cell_type": "code", |
| 82 |
"execution_count": null, |
| 83 |
"metadata": {}, |
| 84 |
"outputs": [], |
| 85 |
"source": [ |
| 86 |
"# render the compound widget like any other interactive widget\n", |
| 87 |
"gaussian_density_widget" |
| 88 |
] |
| 89 |
}, |
| 90 |
{ |
| 91 |
"cell_type": "code", |
| 92 |
"execution_count": null, |
| 93 |
"metadata": {}, |
| 94 |
"outputs": [], |
| 95 |
"source": [ |
| 96 |
"# we can stack this compound widget together with other interactive widgets\n", |
| 97 |
"widgets.HBox([gaussian_density_widget, widgets.IntSlider()])" |
| 98 |
] |
| 99 |
} |
| 100 |
], |
| 101 |
"metadata": { |
| 102 |
"kernelspec": { |
| 103 |
"display_name": "Python 3", |
| 104 |
"language": "python", |
| 105 |
"name": "python3" |
| 106 |
}, |
| 107 |
"language_info": { |
| 108 |
"codemirror_mode": { |
| 109 |
"name": "ipython", |
| 110 |
"version": 3 |
| 111 |
}, |
| 112 |
"file_extension": ".py", |
| 113 |
"mimetype": "text/x-python", |
| 114 |
"name": "python", |
| 115 |
"nbconvert_exporter": "python", |
| 116 |
"pygments_lexer": "ipython3", |
| 117 |
"version": "3.7.6" |
| 118 |
} |
| 119 |
}, |
| 120 |
"nbformat": 4, |
| 121 |
"nbformat_minor": 2 |
| 122 |
} |