From 4c06cc5df2b6cdea99dbac1da827608904a88eb9 Mon Sep 17 00:00:00 2001 From: nemanja Date: Fri, 1 Sep 2017 15:44:19 +0200 Subject: [PATCH 1/2] adding webagg.address parameter to rcParams --- lib/matplotlib/backends/backend_webagg.py | 9 +++++++-- lib/matplotlib/rcsetup.py | 10 ++++++++++ lib/matplotlib/style/core.py | 2 +- matplotlibrc.template | 3 +++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/backends/backend_webagg.py b/lib/matplotlib/backends/backend_webagg.py index e39bf2cb2bab..8b92e16311ba 100644 --- a/lib/matplotlib/backends/backend_webagg.py +++ b/lib/matplotlib/backends/backend_webagg.py @@ -229,7 +229,7 @@ def __init__(self, url_prefix=''): template_path=core.FigureManagerWebAgg.get_static_file_path()) @classmethod - def initialize(cls, url_prefix='', port=None): + def initialize(cls, url_prefix='', port=None, address=None): if cls.initialized: return @@ -253,10 +253,15 @@ def random_ports(port, n): yield port + random.randint(-2 * n, 2 * n) success = None + + if address is None: + cls.address = rcParams['webagg.address'] + else: + cls.address = address cls.port = rcParams['webagg.port'] for port in random_ports(cls.port, rcParams['webagg.port_retries']): try: - app.listen(port) + app.listen(port, cls.address) except socket.error as e: if e.errno != errno.EADDRINUSE: raise diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index ae60b108f541..429c778a21be 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -885,6 +885,15 @@ def validate_animation_writer_path(p): modules["matplotlib.animation"].writers.set_dirty() return p +def validate_webagg_address(s): + if s is not None: + import socket + try: + socket.inet_aton(s) + except socket.error as e: + raise ValueError("'webagg.address' is not a valid IP address") + return s + # A validator dedicated to the named line styles, based on the items in # ls_mapper, and a list of possible strings read from Line2D.set_linestyle _validate_named_linestyle = ValidateInStrings('linestyle', @@ -943,6 +952,7 @@ def _validate_linestyle(ls): 'backend.qt4': ['PyQt4', validate_qt4], 'backend.qt5': ['PyQt5', validate_qt5], 'webagg.port': [8988, validate_int], + 'webagg.address': ['127.0.0.1', validate_webagg_address], 'webagg.open_in_browser': [True, validate_bool], 'webagg.port_retries': [50, validate_int], 'nbagg.transparent': [True, validate_bool], diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index 45ac249bc07b..0d5fff2b7010 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -37,7 +37,7 @@ # A list of rcParams that should not be applied from styles STYLE_BLACKLIST = { - 'interactive', 'backend', 'backend.qt4', 'webagg.port', + 'interactive', 'backend', 'backend.qt4', 'webagg.port', 'webagg.address', 'webagg.port_retries', 'webagg.open_in_browser', 'backend_fallback', 'toolbar', 'timezone', 'datapath', 'figure.max_open_warning', 'savefig.directory', 'tk.window_focus', 'docstring.hardcopy'} diff --git a/matplotlibrc.template b/matplotlibrc.template index 2a6e8b273fb5..fd592af89558 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -53,6 +53,9 @@ backend : $TEMPLATE_BACKEND # The port to use for the web server in the WebAgg backend. # webagg.port : 8888 +# The address on which the WebAgg web server should be reachable +# webagg.address : 127.0.0.1 + # If webagg.port is unavailable, a number of other random ports will # be tried until one that is available is found. # webagg.port_retries : 50 From 5b8c4c13c7c3e395bd38140086e9049f0975115f Mon Sep 17 00:00:00 2001 From: nemanja Date: Wed, 6 Sep 2017 10:29:15 +0200 Subject: [PATCH 2/2] adding webagg.address parameter to rcParams --- lib/matplotlib/rcsetup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 429c778a21be..5ef97c079235 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -892,7 +892,8 @@ def validate_webagg_address(s): socket.inet_aton(s) except socket.error as e: raise ValueError("'webagg.address' is not a valid IP address") - return s + return s + raise ValueError("'webagg.address' is not a valid IP address") # A validator dedicated to the named line styles, based on the items in # ls_mapper, and a list of possible strings read from Line2D.set_linestyle