|
162 | 162 | "cell_type": "markdown",
|
163 | 163 | "metadata": {},
|
164 | 164 | "source": [
|
165 |
| - "## Add your own command" |
| 165 | + "## Add your own command\n", |
| 166 | + "\n", |
| 167 | + "Let's create a nice plot with `bqlot` and generate some random data.\n", |
| 168 | + "\n", |
| 169 | + "See https://github.com/bqplot/bqplot/blob/master/examples/Advanced%20Plotting/Animations.ipynb for more details." |
166 | 170 | ]
|
167 | 171 | },
|
168 | 172 | {
|
|
171 | 175 | "metadata": {},
|
172 | 176 | "outputs": [],
|
173 | 177 | "source": [
|
174 |
| - "from random import randint\n", |
175 |
| - "from ipywidgets import IntSlider\n", |
| 178 | + "import numpy as np\n", |
176 | 179 | "\n",
|
177 |
| - "slider = IntSlider(min=0, max=100)\n", |
178 |
| - "slider" |
| 180 | + "from bqplot import LinearScale, Lines, Bars, Axis, Figure\n", |
| 181 | + "from ipywidgets import IntSlider" |
179 | 182 | ]
|
180 | 183 | },
|
181 | 184 | {
|
|
184 | 187 | "metadata": {},
|
185 | 188 | "outputs": [],
|
186 | 189 | "source": [
|
187 |
| - "def my_command():\n", |
188 |
| - " slider.value = randint(0, 100)\n", |
| 190 | + "xs = LinearScale()\n", |
| 191 | + "ys1 = LinearScale()\n", |
| 192 | + "ys2 = LinearScale()\n", |
| 193 | + "\n", |
| 194 | + "x = np.arange(20)\n", |
| 195 | + "y = np.cumsum(np.random.randn(20))\n", |
| 196 | + "y1 = np.random.rand(20)\n", |
189 | 197 | "\n",
|
190 |
| - "app.commands.add_command('random', execute=my_command, label=\"My Random Command\")" |
| 198 | + "line = Lines(x=x, y=y, scales={'x': xs, 'y': ys1}, colors=['magenta'], marker='square')\n", |
| 199 | + "bar = Bars(x=x, y=y1, scales={'x': xs, 'y': ys2}, colorpadding=0.2, colors=['steelblue'])\n", |
| 200 | + "\n", |
| 201 | + "xax = Axis(scale=xs, label='x', grid_lines='solid')\n", |
| 202 | + "yax1 = Axis(scale=ys1, orientation='vertical', tick_format='0.1f', label='y', grid_lines='solid')\n", |
| 203 | + "yax2 = Axis(scale=ys2, orientation='vertical', side='right', tick_format='0.0%', label='y1', grid_lines='none')\n", |
| 204 | + "\n", |
| 205 | + "Figure(marks=[bar, line], axes=[xax, yax1, yax2], animation_duration=1000)" |
| 206 | + ] |
| 207 | + }, |
| 208 | + { |
| 209 | + "cell_type": "markdown", |
| 210 | + "metadata": {}, |
| 211 | + "source": [ |
| 212 | + "We now define a function to update the data." |
| 213 | + ] |
| 214 | + }, |
| 215 | + { |
| 216 | + "cell_type": "code", |
| 217 | + "execution_count": null, |
| 218 | + "metadata": {}, |
| 219 | + "outputs": [], |
| 220 | + "source": [ |
| 221 | + "def update_data():\n", |
| 222 | + " line.y = np.cumsum(np.random.randn(20))\n", |
| 223 | + " bar.y = np.random.rand(20)" |
| 224 | + ] |
| 225 | + }, |
| 226 | + { |
| 227 | + "cell_type": "code", |
| 228 | + "execution_count": null, |
| 229 | + "metadata": {}, |
| 230 | + "outputs": [], |
| 231 | + "source": [ |
| 232 | + "update_data()" |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "markdown", |
| 237 | + "metadata": {}, |
| 238 | + "source": [ |
| 239 | + "This function will now be called when the JupyterLab command is executed." |
| 240 | + ] |
| 241 | + }, |
| 242 | + { |
| 243 | + "cell_type": "code", |
| 244 | + "execution_count": null, |
| 245 | + "metadata": {}, |
| 246 | + "outputs": [], |
| 247 | + "source": [ |
| 248 | + "app.commands.add_command('update_data', execute=update_data, label=\"Update Data\")" |
191 | 249 | ]
|
192 | 250 | },
|
193 | 251 | {
|
|
203 | 261 | "metadata": {},
|
204 | 262 | "outputs": [],
|
205 | 263 | "source": [
|
206 |
| - "app.commands.execute('random')" |
| 264 | + "app.commands.execute('update_data')" |
207 | 265 | ]
|
208 | 266 | },
|
209 | 267 | {
|
|
262 | 320 | "metadata": {},
|
263 | 321 | "outputs": [],
|
264 | 322 | "source": [
|
265 |
| - "palette.add_item('random', 'Python Commands')" |
| 323 | + "palette.add_item('update_data', 'Python Commands')" |
266 | 324 | ]
|
267 | 325 | },
|
268 | 326 | {
|
|
287 | 345 | "metadata": {},
|
288 | 346 | "outputs": [],
|
289 | 347 | "source": [
|
290 |
| - "app.commands.remove_command('random')" |
| 348 | + "app.commands.remove_command('update_data')" |
291 | 349 | ]
|
292 | 350 | }
|
293 | 351 | ],
|
|
307 | 365 | "name": "python",
|
308 | 366 | "nbconvert_exporter": "python",
|
309 | 367 | "pygments_lexer": "ipython3",
|
310 |
| - "version": "3.8.1" |
| 368 | + "version": "3.8.2" |
311 | 369 | },
|
312 | 370 | "widgets": {
|
313 | 371 | "application/vnd.jupyter.widget-state+json": {
|
|
0 commit comments