Skip to content

Backport PR #20591 on branch v3.5.x (Webagg backend: get rid of tornado) #21410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/devel/dependencies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ and the capabilities they provide.
* wxPython_ (>= 4) [#]_: for the wx-based backends.
* pycairo_ (>= 1.11.0) or cairocffi_ (>= 0.8): for the GTK and/or cairo-based
backends.
* Tornado_: for the WebAgg backend.
* Tornado_ (>=5): for the WebAgg backend.

.. _Tk: https://docs.python.org/3/library/tk.html
.. _PyQt5: https://pypi.org/project/PyQt5/
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/backends/backend_nbagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from matplotlib.backend_bases import _Backend, NavigationToolbar2
from matplotlib.backends.backend_webagg_core import (
FigureCanvasWebAggCore, FigureManagerWebAgg, NavigationToolbar2WebAgg,
TimerTornado)
TimerTornado, TimerAsyncio
)


def connection_info():
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_webagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from matplotlib.backend_bases import _Backend
from matplotlib._pylab_helpers import Gcf
from . import backend_webagg_core as core
from .backend_webagg_core import TimerTornado
from .backend_webagg_core import TimerAsyncio, TimerTornado


class ServerThread(threading.Thread):
Expand Down
45 changes: 42 additions & 3 deletions lib/matplotlib/backends/backend_webagg_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
# way over a web socket.
#
# - `backend_webagg.py` contains a concrete implementation of a basic
# application, implemented with tornado.
# application, implemented with asyncio.

import asyncio
import datetime
from io import BytesIO, StringIO
import json
Expand All @@ -19,7 +20,6 @@

import numpy as np
from PIL import Image
import tornado

from matplotlib import _api, backend_bases, backend_tools
from matplotlib.backends import backend_agg
Expand Down Expand Up @@ -85,6 +85,8 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def _timer_start(self):
import tornado

self._timer_stop()
if self._single:
ioloop = tornado.ioloop.IOLoop.instance()
Expand All @@ -98,6 +100,8 @@ def _timer_start(self):
self._timer.start()

def _timer_stop(self):
import tornado

if self._timer is None:
return
elif self._single:
Expand All @@ -114,8 +118,43 @@ def _timer_set_interval(self):
self._timer_start()


class TimerAsyncio(backend_bases.TimerBase):
def __init__(self, *args, **kwargs):
self._task = None
super().__init__(*args, **kwargs)

async def _timer_task(self, interval):
while True:
try:
await asyncio.sleep(interval)
self._on_timer()

if self._single:
break
except asyncio.CancelledError:
break

def _timer_start(self):
self._timer_stop()

self._task = asyncio.ensure_future(
self._timer_task(max(self.interval / 1_000., 1e-6))
)

def _timer_stop(self):
if self._task is not None:
self._task.cancel()
self._task = None

def _timer_set_interval(self):
# Only stop and restart it if the timer has already been started
if self._task is not None:
self._timer_stop()
self._timer_start()


class FigureCanvasWebAggCore(backend_agg.FigureCanvasAgg):
_timer_cls = TimerTornado
_timer_cls = TimerAsyncio
# Webagg and friends having the right methods, but still
# having bugs in practice. Do not advertise that it works until
# we can debug this.
Expand Down