Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b182e2

Browse files
committed
Use Matplotlib's tool icons in WebAgg backend.
This is one step to removing dependence on jQuery-UI.
1 parent 002b27e commit 4b182e2

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

examples/user_interfaces/embedding_webagg_sgskip.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"""
1313

1414
import io
15+
import json
1516
import mimetypes
17+
from pathlib import Path
1618

1719
try:
1820
import tornado
@@ -24,14 +26,13 @@
2426
import tornado.websocket
2527

2628

29+
import matplotlib as mpl
2730
from matplotlib.backends.backend_webagg_core import (
2831
FigureManagerWebAgg, new_figure_manager_given_figure)
2932
from matplotlib.figure import Figure
3033

3134
import numpy as np
3235

33-
import json
34-
3536

3637
def create_figure():
3738
"""
@@ -219,6 +220,11 @@ def __init__(self, figure):
219220
tornado.web.StaticFileHandler,
220221
{'path': FigureManagerWebAgg.get_static_file_path()}),
221222

223+
# Static images for the toolbar
224+
(r'/_images/(.*)',
225+
tornado.web.StaticFileHandler,
226+
{'path': Path(mpl.get_data_path(), 'images')}),
227+
222228
# The page that contains all of the pieces
223229
('/', self.MainPage),
224230

lib/matplotlib/backends/backend_webagg.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import tornado.websocket
3434

3535
import matplotlib as mpl
36-
from matplotlib import cbook
3736
from matplotlib.backend_bases import _Backend
3837
from matplotlib._pylab_helpers import Gcf
3938
from . import backend_webagg_core as core
@@ -64,8 +63,8 @@ class WebAggApplication(tornado.web.Application):
6463
class FavIcon(tornado.web.RequestHandler):
6564
def get(self):
6665
self.set_header('Content-Type', 'image/png')
67-
self.write(
68-
cbook._get_data_path('images/matplotlib.png').read_bytes())
66+
self.write(Path(mpl.get_data_path(),
67+
'images/matplotlib.png').read_bytes())
6968

7069
class SingleFigurePage(tornado.web.RequestHandler):
7170
def __init__(self, application, request, *, url_prefix='', **kwargs):
@@ -170,6 +169,11 @@ def __init__(self, url_prefix=''):
170169
tornado.web.StaticFileHandler,
171170
{'path': core.FigureManagerWebAgg.get_static_file_path()}),
172171

172+
# Static images for the toolbar
173+
(url_prefix + r'/_images/(.*)',
174+
tornado.web.StaticFileHandler,
175+
{'path': Path(mpl.get_data_path(), 'images')}),
176+
173177
# A Matplotlib favicon
174178
(url_prefix + r'/favicon.ico', self.FavIcon),
175179

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -345,26 +345,27 @@ def send_event(self, event_type, **kwargs):
345345
self.manager._send_event(event_type, **kwargs)
346346

347347

348-
_JQUERY_ICON_CLASSES = {
349-
'home': 'ui-icon ui-icon-home',
350-
'back': 'ui-icon ui-icon-circle-arrow-w',
351-
'forward': 'ui-icon ui-icon-circle-arrow-e',
352-
'zoom_to_rect': 'ui-icon ui-icon-search',
353-
'move': 'ui-icon ui-icon-arrow-4',
354-
'download': 'ui-icon ui-icon-disk',
355-
None: None,
348+
_ALLOWED_TOOL_ITEMS = {
349+
'home',
350+
'back',
351+
'forward',
352+
'pan',
353+
'zoom',
354+
'download',
355+
None,
356356
}
357357

358358

359359
class NavigationToolbar2WebAgg(backend_bases.NavigationToolbar2):
360360

361361
# Use the standard toolbar items + download button
362-
toolitems = [(text, tooltip_text, _JQUERY_ICON_CLASSES[image_file],
363-
name_of_method)
364-
for text, tooltip_text, image_file, name_of_method
365-
in (backend_bases.NavigationToolbar2.toolitems +
366-
(('Download', 'Download plot', 'download', 'download'),))
367-
if image_file in _JQUERY_ICON_CLASSES]
362+
toolitems = [
363+
(text, tooltip_text, image_file, name_of_method)
364+
for text, tooltip_text, image_file, name_of_method
365+
in (*backend_bases.NavigationToolbar2.toolitems,
366+
('Download', 'Download plot', 'filesave', 'download'))
367+
if name_of_method in _ALLOWED_TOOL_ITEMS
368+
]
368369

369370
def _init_toolbar(self):
370371
self.message = ''

lib/matplotlib/backends/web_backend/js/mpl.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,23 +283,18 @@ mpl.figure.prototype._init_toolbar = function () {
283283
continue;
284284
}
285285
var button = document.createElement('button');
286-
button.classList =
287-
'ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only';
286+
button.classList = 'ui-button ui-widget ui-state-default ui-corner-all';
288287
button.setAttribute('role', 'button');
289288
button.setAttribute('aria-disabled', 'false');
290289
button.addEventListener('click', on_click_closure(method_name));
291290
button.addEventListener('mouseover', on_mouseover_closure(tooltip));
292291

293-
var icon_img = document.createElement('span');
294-
icon_img.classList =
295-
'ui-button-icon-primary ui-icon ' + image + ' ui-corner-all';
296-
297-
var tooltip_span = document.createElement('span');
298-
tooltip_span.classList = 'ui-button-text';
299-
tooltip_span.innerHTML = tooltip;
292+
var icon_img = document.createElement('img');
293+
icon_img.src = '_images/' + image + '.png';
294+
icon_img.srcset = '_images/' + image + '_large.png 2x';
295+
icon_img.alt = tooltip;
300296

301297
button.appendChild(icon_img);
302-
button.appendChild(tooltip_span);
303298

304299
toolbar.appendChild(button);
305300
}

0 commit comments

Comments
 (0)