Skip to content

Commit 38e9e52

Browse files
committed
Add Timer class to support animations
1 parent 55117ee commit 38e9e52

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import numpy as np
2121
from PIL import Image
2222

23+
from pyodide.ffi.wrappers import (
24+
clear_interval, clear_timeout, set_interval, set_timeout)
25+
2326
from matplotlib import _api, backend_bases, backend_tools
2427
from matplotlib.backends import backend_agg
2528
from matplotlib.backend_bases import (
@@ -79,9 +82,38 @@ def _handle_key(key):
7982
return key
8083

8184

85+
class TimerJs(backend_bases.TimerBase):
86+
def __init__(self, *args, **kwargs):
87+
self._timer: int | None = None
88+
super().__init__(*args, **kwargs)
89+
90+
def _timer_start(self):
91+
self._timer_stop()
92+
if self._single:
93+
self._timer = set_timeout(self._on_timer, self.interval)
94+
else:
95+
self._timer = set_interval(self._on_timer, self.interval)
96+
97+
def _timer_stop(self):
98+
if self._timer is None:
99+
return
100+
elif self._single:
101+
clear_timeout(self._timer)
102+
self._timer = None
103+
else:
104+
clear_interval(self._timer)
105+
self._timer = None
106+
107+
def _timer_set_interval(self):
108+
# Only stop and restart it if the timer has already been started
109+
if self._timer is not None:
110+
self._timer_stop()
111+
self._timer_start()
112+
82113

83114
class FigureCanvasWebAggCore(backend_agg.FigureCanvasAgg):
84115
manager_class = _api.classproperty(lambda cls: FigureManagerWebAgg)
116+
_timer_cls = TimerJs
85117
# Webagg and friends having the right methods, but still
86118
# having bugs in practice. Do not advertise that it works until
87119
# we can debug this.

0 commit comments

Comments
 (0)