Skip to content

Commit a15cb8d

Browse files
authoredJul 13, 2016
Merge pull request bqplot#164 from SylvainCorlay/Update-WOF
Use Play Widget
2 parents 5c240d9 + 6290991 commit a15cb8d

File tree

2 files changed

+31
-40
lines changed

2 files changed

+31
-40
lines changed
 

‎examples/Applications/Wealth of Nations.ipynb

+29-39
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
},
1616
"outputs": [],
1717
"source": [
18-
"# Required imports\n",
1918
"import pandas as pd\n",
20-
"from bqplot import (LogScale, LinearScale, OrdinalColorScale, ColorAxis,\n",
21-
" Axis, Scatter, Lines, CATEGORY10, Label, Figure)\n",
22-
"from bqplot.default_tooltip import Tooltip\n",
23-
"from ipywidgets import VBox, IntSlider, Button\n",
24-
"from IPython.display import display\n",
25-
"import os\n",
2619
"import numpy as np\n",
27-
"from time import sleep"
20+
"import os\n",
21+
"\n",
22+
"from bqplot import (\n",
23+
" LogScale, LinearScale, OrdinalColorScale, ColorAxis,\n",
24+
" Axis, Scatter, Lines, CATEGORY10, Label, Figure, Tooltip\n",
25+
")\n",
26+
"\n",
27+
"from ipywidgets import VBox, IntSlider, Play, jsdlink"
2828
]
2929
},
3030
{
@@ -35,7 +35,6 @@
3535
},
3636
"outputs": [],
3737
"source": [
38-
"# The GUI starts with this year\n",
3938
"initial_year = 1800"
4039
]
4140
},
@@ -197,8 +196,8 @@
197196
},
198197
"outputs": [],
199198
"source": [
200-
"ax_y = Axis(label='Life Expectancy', scale=y_sc, orientation='vertical', side='left')\n",
201-
"ax_x = Axis(label='Income per Capita', scale=x_sc)"
199+
"ax_y = Axis(label='Life Expectancy', scale=y_sc, orientation='vertical', side='left', grid_lines='solid')\n",
200+
"ax_x = Axis(label='Income per Capita', scale=x_sc, grid_lines='solid')"
202201
]
203202
},
204203
{
@@ -256,6 +255,17 @@
256255
"#### Creating the Figure"
257256
]
258257
},
258+
{
259+
"cell_type": "code",
260+
"execution_count": null,
261+
"metadata": {
262+
"collapsed": true
263+
},
264+
"outputs": [],
265+
"source": [
266+
"time_interval = 50"
267+
]
268+
},
259269
{
260270
"cell_type": "code",
261271
"execution_count": null,
@@ -265,8 +275,7 @@
265275
"outputs": [],
266276
"source": [
267277
"fig = Figure(marks=[wealth_scat, year_label, nation_line], axes=[ax_x, ax_y],\n",
268-
" title='Health and Wealth of Nations', background_style={'fill': 'White'},\n",
269-
" animation_duration=100)"
278+
" title='Health and Wealth of Nations', animation_duration=time_interval)"
270279
]
271280
},
272281
{
@@ -289,17 +298,6 @@
289298
"year_slider = IntSlider(min=1800, max=2008, step=1, description='Year', value=initial_year)"
290299
]
291300
},
292-
{
293-
"cell_type": "code",
294-
"execution_count": null,
295-
"metadata": {
296-
"collapsed": true
297-
},
298-
"outputs": [],
299-
"source": [
300-
"animate_button = Button(description='Play', background_color='MediumSeaGreen', color='Black', icon='fa-play')"
301-
]
302-
},
303301
{
304302
"cell_type": "markdown",
305303
"metadata": {},
@@ -341,38 +339,30 @@
341339
},
342340
"outputs": [],
343341
"source": [
344-
"def year_changed(values):\n",
342+
"def year_changed(new):\n",
345343
" wealth_scat.x, wealth_scat.y, wealth_scat.size = get_data(year_slider.value)\n",
346344
" year_label.text = str(year_slider.value)\n",
347-
" \n",
345+
"\n",
348346
"year_slider.observe(year_changed, 'value')"
349347
]
350348
},
351349
{
352350
"cell_type": "markdown",
353351
"metadata": {},
354352
"source": [
355-
"#### Defining the callback for the button\n",
356-
"\n",
357-
"When the `Button` is clicked, we trigger the animation by changing the slider value through all the years for which data is available. The change in the `Slider` value triggers a change in the plot."
353+
"#### Add an animation button"
358354
]
359355
},
360356
{
361357
"cell_type": "code",
362358
"execution_count": null,
363359
"metadata": {
364-
"collapsed": true
360+
"collapsed": false
365361
},
366362
"outputs": [],
367363
"source": [
368-
"def button_clicked(value):\n",
369-
" animate_button.visible = False\n",
370-
" for i in range(1800, 2009, 1):\n",
371-
" year_slider.value = i\n",
372-
" sleep(0.05)\n",
373-
" animate_button.visible = True\n",
374-
"\n",
375-
"animate_button.on_click(button_clicked)"
364+
"play_button = Play(min=1800, max=2008, interval=time_interval)\n",
365+
"jsdlink((play_button, 'value'), (year_slider, 'value'))"
376366
]
377367
},
378368
{
@@ -391,7 +381,7 @@
391381
},
392382
"outputs": [],
393383
"source": [
394-
"display(VBox([animate_button, fig, year_slider]))"
384+
"VBox([play_button, fig, year_slider])"
395385
]
396386
}
397387
],

‎setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def run(self):
173173
]),
174174
],
175175
'install_requires': [
176-
'ipywidgets>=5.0.0',
176+
'ipywidgets>=5.2.0',
177177
'numpy',
178178
'pandas'],
179179
'packages': find_packages(),
@@ -205,6 +205,7 @@ def run(self):
205205
'Programming Language :: Python :: 3',
206206
'Programming Language :: Python :: 3.3',
207207
'Programming Language :: Python :: 3.4',
208+
'Programming Language :: Python :: 3.5',
208209
],
209210
}
210211

0 commit comments

Comments
 (0)