From 86228dd5c03e15ab26e63c3cd052b554e329248c Mon Sep 17 00:00:00 2001 From: Kelvin Loh <kel85uk@gmail.com> Date: Thu, 27 Dec 2018 09:32:20 +0100 Subject: [PATCH] Check if ipython module is available before setting the init_notebook_mode. Called once during setting of plotly backend. --- kwant/_plotter.py | 12 ++++++++++-- kwant/plotter.py | 9 +++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/kwant/_plotter.py b/kwant/_plotter.py index 25ce0c63..e82cb7ee 100644 --- a/kwant/_plotter.py +++ b/kwant/_plotter.py @@ -20,6 +20,14 @@ from math import sqrt, pi import numpy as np from enum import Enum + +try: + __IPYTHON__ + is_ipython_kernel = True +except NameError: + is_ipython_kernel = False + + try: import matplotlib import matplotlib.colors @@ -42,9 +50,9 @@ except ImportError: try: - import plotly.offline as plotly + import plotly.offline as plotly_module import plotly.graph_objs as plotly_graph_objs - plotly.init_notebook_mode(connected=True) + init_notebook_mode_set = False plotly_available = True except ImportError: warnings.warn("plotly is not available, if other backends are unavailable," diff --git a/kwant/plotter.py b/kwant/plotter.py index 4b3657e7..0b94a833 100644 --- a/kwant/plotter.py +++ b/kwant/plotter.py @@ -165,9 +165,14 @@ def _maybe_output_fig(fig, file=None, show=True): pyplot.show() elif get_backend() == _p.Backends.plotly: if file is not None: - _p.plotly.plot(fig, show_link=False, filename=file, auto_open=False) + _p.plotly_module.plot(fig, show_link=False, filename=file, auto_open=False) if show: - _p.plotly.iplot(fig) + if (_p.is_ipython_kernel): + _p.plotly_module.iplot(fig) + else: + raise RuntimeError('show flag using the plotly backend can ' + 'only be True if and only if called from a ' + 'jupyter/ipython environment.') def set_colors(color, collection, cmap, norm=None): -- GitLab