Skip to content

Commit 7aa51b4

Browse files
authored
Merge pull request #42 from jtpio/update-examples
Update the example notebooks
2 parents d146e2b + f00d012 commit 7aa51b4

File tree

5 files changed

+252
-29
lines changed

5 files changed

+252
-29
lines changed

README.md

+25-5
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ Try it in your browser with Binder:
2424

2525
## Examples
2626

27-
### Widgets and Panels
27+
### Add Jupyter Widgets to the JupyterLab interface
2828

29-
![widgets-panels](https://user-images.githubusercontent.com/591645/69000410-8f151f00-08cf-11ea-8491-7b8848497b62.gif)
29+
![widgets-panels](https://user-images.githubusercontent.com/591645/80025074-59104280-84e0-11ea-9766-0cb49cba285a.gif)
3030

31-
### Command Registry
31+
### Execute Commands
3232

33-
![command-registry](./docs/screencasts/commands.gif)
33+
![command-registry](https://user-images.githubusercontent.com/591645/80026017-beb0fe80-84e1-11ea-842d-fa3bf5bc4a9b.gif)
3434

3535
### Custom Python Commands and Command Palette
3636

37-
![custom-commands](https://user-images.githubusercontent.com/591645/73125753-adbc2400-3faa-11ea-95f8-f7060e883ccd.gif)
37+
![custom-commands](https://user-images.githubusercontent.com/591645/80026023-c1135880-84e1-11ea-9e83-fdb739659357.gif)
38+
39+
### Building small UI applications
40+
41+
![ipytree-example](https://user-images.githubusercontent.com/591645/80026006-b8bb1d80-84e1-11ea-87cc-86495186b938.gif)
3842

3943
## Installation
4044

@@ -56,6 +60,22 @@ To install the JupyterLab extension:
5660
jupyter labextension install @jupyter-widgets/jupyterlab-manager ipylab
5761
```
5862

63+
## Running the examples locally
64+
65+
To try out the examples locally, the recommended way is to create a new environment with the dependencies:
66+
67+
```bash
68+
# create a new conda environment
69+
conda create -n ipylab-examples -c conda-forge jupyterlab ipylab ipytree bqplot ipywidgets numpy
70+
conda activate ipylab-examples
71+
72+
# install the JupyterLab extensions
73+
jupyter labextension install @jupyter-widgets/jupyterlab-manager ipylab bqplot ipytree
74+
75+
# start JupyterLab
76+
jupyter lab
77+
```
78+
5979
## Under the hood
6080

6181
`ipylab` can be seen as a proxy from Python to JupyterLab over Jupyter Widgets:

environment.yml

+2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ name: ipylab
22
channels:
33
- conda-forge
44
dependencies:
5+
- bqplot=0.12
56
- ipylab=0.2.1
67
- ipytree=0.1
78
- jupyterlab=2
89
- nodejs
10+
- numpy

examples/commands.ipynb

+70-12
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@
162162
"cell_type": "markdown",
163163
"metadata": {},
164164
"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."
166170
]
167171
},
168172
{
@@ -171,11 +175,10 @@
171175
"metadata": {},
172176
"outputs": [],
173177
"source": [
174-
"from random import randint\n",
175-
"from ipywidgets import IntSlider\n",
178+
"import numpy as np\n",
176179
"\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"
179182
]
180183
},
181184
{
@@ -184,10 +187,65 @@
184187
"metadata": {},
185188
"outputs": [],
186189
"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",
189197
"\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\")"
191249
]
192250
},
193251
{
@@ -203,7 +261,7 @@
203261
"metadata": {},
204262
"outputs": [],
205263
"source": [
206-
"app.commands.execute('random')"
264+
"app.commands.execute('update_data')"
207265
]
208266
},
209267
{
@@ -262,7 +320,7 @@
262320
"metadata": {},
263321
"outputs": [],
264322
"source": [
265-
"palette.add_item('random', 'Python Commands')"
323+
"palette.add_item('update_data', 'Python Commands')"
266324
]
267325
},
268326
{
@@ -287,7 +345,7 @@
287345
"metadata": {},
288346
"outputs": [],
289347
"source": [
290-
"app.commands.remove_command('random')"
348+
"app.commands.remove_command('update_data')"
291349
]
292350
}
293351
],
@@ -307,7 +365,7 @@
307365
"name": "python",
308366
"nbconvert_exporter": "python",
309367
"pygments_lexer": "ipython3",
310-
"version": "3.8.1"
368+
"version": "3.8.2"
311369
},
312370
"widgets": {
313371
"application/vnd.jupyter.widget-state+json": {

0 commit comments

Comments
 (0)