8000 Merge pull request #4892 from joferkington/fix_nbagg_timer · matplotlib/matplotlib@7eadf59 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7eadf59

Browse files
committed
Merge pull request #4892 from joferkington/fix_nbagg_timer
FIX: single-shot timers in nbagg backend
2 parents f852c0c + 0c79c02 commit 7eadf59

File tree

2 files changed

+167
-25
lines changed

2 files changed

+167
-25
lines changed

lib/matplotlib/backends/backend_nbagg.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# lib/matplotlib/backends/web_backend/nbagg_uat.ipynb to help verify
44
# that changes made maintain expected behaviour.
55

6+
import datetime
67
from base64 import b64encode
78
import json
89
import io
@@ -171,7 +172,6 @@ def clearup_closed(self):
171172

172173
class TimerTornado(TimerBase):
173174
def _timer_start(self):
174-
import datetime
175175
self._timer_stop()
176176
if self._single:
177177
ioloop = tornado.ioloop.IOLoop.instance()
@@ -182,12 +182,18 @@ def _timer_start(self):
182182
self._timer = tornado.ioloop.PeriodicCallback(
183183
self._on_timer,
184184
self.interval)
185-
self._timer.start()
185+
self._timer.start()
186186

187187
def _timer_stop(self):
188-
if self._timer is not None:
188+
if self._timer is None:
189+
return
190+
elif self._single:
191+
ioloop = tornado.ioloop.IOLoop.instance()
192+
ioloop.remove_timeout(self._timer)
193+
else:
189194
self._timer.stop()
190-
self._timer = None
195+
196+
self._timer = None
191197

192198
def _timer_set_interval(self):
193199
# Only stop and restart it if the timer has already been started

lib/matplotlib/backends/web_backend/nbagg_uat.ipynb

Lines changed: 157 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
{
22
"metadata": {
3-
"name": "",
4-
"signature": "sha256:a1ac68aba163c75eab3d1fc91aa4d9a8ca66b09159619563827a19967d96814b"
3+
"kernelspec": {
4+
"display_name": "Python 2",
5+
"language": "python",
6+
"name": "python2"
7+
},
8+
"language_info": {
9+
"codemirror_mode": {
10+
"name": "ipython",
11+
"version": 2
12+
},
13+
"file_extension": ".py",
14+
"mimetype": "text/x-python",
15+
"name": "python",
16+
"nbconvert_exporter": "python",
17+
"pygments_lexer": "ipython2",
18+
"version": "2.7.10"
19+
},
20+
"name": ""
521
},
622
"nbformat": 3,
723
"nbformat_minor": 0,
@@ -17,7 +33,8 @@
1733
],
1834
"language": "python",
1935
"metadata": {},
20-
"outputs": []
36+
"outputs": [],
37+
"prompt_number": null
2138
},
2239
{
2340
"cell_type": "markdown",
@@ -42,7 +59,8 @@
4259
],
4360
"language": "python",
4461
"metadata": {},
45-
"outputs": []
62+
"outputs": [],
63+
"prompt_number": null
4664
},
4765
{
4866
"cell_type": "markdown",
@@ -70,7 +88,8 @@
7088
],
7189
"language": "python",
7290
"metadata": {},
73-
"outputs": []
91+
"outputs": [],
92+
"prompt_number": null
7493
},
7594
{
7695
"cell_type": "markdown",
@@ -90,7 +109,8 @@
90109
],
91110
"language": "python",
92111
"metadata": {},
93-
"outputs": []
112+
"outputs": [],
113+
"prompt_number": null
94114
},
95115
{
96116
"cell_type": "markdown",
@@ -109,7 +129,8 @@
109129
],
110130
"language": "python",
111131
"metadata": {},
112-
"outputs": []
132+
"outputs": [],
133+
"prompt_number": null
113134
},
114135
{
115136
"cell_type": "markdown",
@@ -128,7 +149,8 @@
128149
],
129150
"language": "python",
130151
"metadata": {},
131-
"outputs": []
152+
"outputs": [],
153+
"prompt_number": null
132154
},
133155
{
134156
"cell_type": "markdown",
@@ -148,7 +170,8 @@
148170
],
149171
"language": "python",
150172
"metadata": {},
151-
"outputs": []
173+
"outputs": [],
174+
"prompt_number": null
152175
},
153176
{
154177
"cell_type": "markdown",
@@ -167,7 +190,8 @@
167190
],
168191
"language": "python",
169192
"metadata": {},
170-
"outputs": []
193+
"outputs": [],
194+
"prompt_number": null
171195
},
172196
{
173197
"cell_type": "markdown",
@@ -189,7 +213,8 @@
189213
],
190214
"language": "python",
191215
"metadata": {},
192-
"outputs": []
216+
"outputs": [],
217+
"prompt_number": null
193218
},
194219
{
195220
"cell_type": "markdown",
@@ -210,7 +235,8 @@
210235
],
211236
"language": "python",
212237
"metadata": {},
213-
"outputs": []
238+
"outputs": [],
239+
"prompt_number": null
214240
},
215241
{
216242
"cell_type": "markdown",
@@ -227,7 +253,8 @@
227253
],
228254
"language": "python",
229255
"metadata": {},
230-
"outputs": []
256+
"outputs": [],
257+
"prompt_number": null
231258
},
232259
{
233260
"cell_type": "markdown",
@@ -244,7 +271,8 @@
244271
],
245272
"language": "python",
246273
"metadata": {},
247-
"outputs": []
274+
"outputs": [],
275+
"prompt_number": null
248276
},
249277
{
250278
"cell_type": "markdown",
@@ -263,7 +291,8 @@
263291
],
264292
"language": "python",
265293
"metadata": {},
266-
"outputs": []
294+
"outputs": [],
295+
"prompt_number": null
267296
},
268297
{
269298
"cell_type": "markdown",
@@ -298,7 +327,8 @@
298327
],
299328
"language": "python",
300329
"metadata": {},
301-
"outputs": []
330+
"outputs": [],
331+
"prompt_number": null
302332
},
303333
{
304334
"cell_type": "markdown",
@@ -323,7 +353,8 @@
323353
],
324354
"language": "python",
325355
"metadata": {},
326-
"outputs": []
356+
"outputs": [],
357+
"prompt_number": null
327358
},
328359
{
329360
"cell_type": "markdown",
@@ -361,7 +392,8 @@
361392
],
362393
"language": "python",
363394
"metadata": {},
364-
"outputs": []
395+
"outputs": [],
396+
"prompt_number": null
365397
},
366398
{
367399
"cell_type": "markdown",
@@ -394,7 +426,8 @@
394426
],
395427
"language": "python",
396428
"metadata": {},
397-
"outputs": []
429+
"outputs": [],
430+
"prompt_number": null
398431
},
399432
{
400433
"cell_type": "markdown",
@@ -438,10 +471,113 @@
438471
],
439472
"language": "python",
440473
"metadata": {},
441-
"outputs": []
474+
"outputs": [],
475+
"prompt_number": null
476+
},
477+
{
478+
"cell_type": "markdown",
479+
"metadata": {},
480+
"source": [
481+
"### UAT 17 - Timers\n",
482+
"\n",
483+
"Single-shot timers follow a completely different code path in the nbagg backend than regular timers (such as those used in the animation example above.) The next set of tests ensures that both \"regular\" and \"single-shot\" timers work properly.\n",
484+
"\n",
485+
"The following should show a simple clock that updates twice a second:"
486+
]
487+
},
488+
{
489+
"cell_type": "code",
490+
"collapsed": true,
491+
"input": [
492+
"import time\n",
493+
"\n",
494+
"fig, ax = plt.subplots()\n",
495+
"text = ax.text(0.5, 0.5, '', ha='center')\n",
496+
"\n",
497+
"def update(text):\n",
498+
" text.set(text=time.ctime())\n",
499+
" text.axes.figure.canvas.draw()\n",
500+
" \n",
501+
"timer = fig.canvas.new_timer(500, [(update, [text], {})])\n",
502+
"timer.start()\n",
503+
"plt.show()"
504+
],
505+
"language": "python",
506+
"metadata": {},
507+
"outputs": [],
508+
"prompt_number": null
509+
},
510+
{
511+
"cell_type": "markdown",
512+
"metadata": {},
513+
"source": [
514+
"However, the following should only update once and then stop:"
515+
]
516+
},
517+
{
518+
"cell_type": "code",
519+
"collapsed": true,
520+
"input": [
521+
"fig, ax = plt.subplots()\n",
522+
"text = ax.text(0.5, 0.5, '', ha='center') \n",
523+
"timer = fig.canvas.new_timer(500, [(update, [text], {})])\n",
524+
"\n",
525+
"timer.single_shot = True\n",
526+
"timer.start()\n",
527+
"\n",
528+
"plt.show()"
529+
],
530+
"language": "python",
531+
"metadata": {},
532+
"outputs": [],
533+
"prompt_number": null
534+
},
535+
{
536+
"cell_type": "markdown",
537+
"metadata": {},
538+
"source": [
539+
"And the next two examples should never show any visible text at all:"
540+
]
541+
},
542+
{
543+
"cell_type": "code",
544+
"collapsed": true,
545+
"input": [
546+
"fig, ax = plt.subplots()\n",
547+
"text = ax.text(0.5, 0.5, '', ha='center')\n",
548+
"timer = fig.canvas.new_timer(500, [(update, [text], {})])\n",
549+
"\n",
550+
"timer.start()\n",
551+
"timer.stop()\n",
552+
"\n",
553+
"plt.show()"
554+
],
555+
"language": "python",
556+
"metadata": {},
557+
"outputs": [],
558+
"prompt_number": null
559+
},
560+
{
561+
"cell_type": "code",
562+
"collapsed": true,
563+
"input": [
564+
"fig, ax = plt.subplots()\n",
565+
"text = ax.text(0.5, 0.5, '', ha='center')\n",
566+
"timer = fig.canvas.new_timer(500, [(update, [text], {})])\n",
567+
"\n",
568+
"timer.single_shot = True\n",
569+
"timer.start()\n",
570+
"timer.stop()\n",
571+
"\n",
572+
"plt.show()"
573+
],
574+
"language": "python",
575+
"metadata": {},
576+
"outputs": [],
577+
"prompt_number": null
442578
}
443579
],
444580
"metadata": {}
445581
}
446582
]
447-
}
583+
}

0 commit comments

Comments
 (0)
0