diff --git a/kwant/_plotter.py b/kwant/_plotter.py index 25ce0c63c879656b89cdd0d4b2234fb5451adeb3..e82cb7ee8d714e8d51bcd3b8deec588eefae9f1c 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 4b3657e7f4d3a272d76b170a5183cc5aca44a159..0b94a8331571319b5c0359478d35b22ee1e42e8c 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):