Skip to content

Commit b563e09

Browse files
committed
improve 'notebook_extension' and add 'plotly' as a dependency
1 parent 5e3edf4 commit b563e09

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

adaptive/notebook_integration.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,42 @@
77

88

99
_async_enabled = False
10-
_plotting_enabled = False
10+
_holoviews_enabled = False
11+
_ipywidgets_enabled = False
12+
_plotly_enabled = False
1113

1214

1315
def notebook_extension():
16+
"""Enable ipywidgets, holoviews, and asyncio notebook integration."""
1417
if not in_ipynb():
1518
raise RuntimeError('"adaptive.notebook_extension()" may only be run '
1619
'from a Jupyter notebook.')
1720

18-
global _plotting_enabled
19-
_plotting_enabled = False
21+
global _async_enabled, _holoviews_enabled, _ipywidgets_enabled
22+
23+
# Load holoviews
24+
try:
25+
if not _holoviews_enabled:
26+
import holoviews
27+
holoviews.notebook_extension('bokeh', logo=False)
28+
_holoviews_enabled = True
29+
except ModuleNotFoundError:
30+
warnings.warn("holoviews is not installed; plotting "
31+
"is disabled.", RuntimeWarning)
32+
33+
# Load ipywidgets
2034
try:
21-
import ipywidgets
22-
import holoviews
23-
holoviews.notebook_extension('bokeh', logo=False)
24-
_plotting_enabled = True
35+
if not _ipywidgets_enabled:
36+
import ipywidgets
37+
_ipywidgets_enabled = True
2538
except ModuleNotFoundError:
26-
warnings.warn("holoviews and (or) ipywidgets are not installed; plotting "
39+
warnings.warn("ipywidgets is not installed; live_info "
2740
"is disabled.", RuntimeWarning)
2841

29-
global _async_enabled
30-
get_ipython().magic('gui asyncio')
31-
_async_enabled = True
42+
# Enable asyncio integration
43+
if not _async_enabled:
44+
get_ipython().magic('gui asyncio')
45+
_async_enabled = True
3246

3347

3448
def ensure_holoviews():
@@ -38,6 +52,22 @@ def ensure_holoviews():
3852
raise RuntimeError('holoviews is not installed; plotting is disabled.')
3953

4054

55+
def ensure_plotly():
56+
global _plotly_enabled
57+
try:
58+
import plotly
59+
if not _plotly_enabled:
60+
import plotly.graph_objs
61+
import plotly.figure_factory
62+
import plotly.offline
63+
# This injects javascript and should happen only once
64+
plotly.offline.init_notebook_mode()
65+
_plotly_enabled = True
66+
return plotly
67+
except ModuleNotFoundError:
68+
raise RuntimeError('plotly is not installed; plotting is disabled.')
69+
70+
4171
def in_ipynb():
4272
try:
4373
# If we are running in IPython, then `get_ipython()` is always a global
@@ -72,7 +102,7 @@ def live_plot(runner, *, plotter=None, update_interval=2, name=None):
72102
dm : `holoviews.core.DynamicMap`
73103
The plot that automatically updates every `update_interval`.
74104
"""
75-
if not _plotting_enabled:
105+
if not _holoviews_enabled:
76106
raise RuntimeError("Live plotting is not enabled; did you run "
77107
"'adaptive.notebook_extension()'?")
78108

@@ -126,7 +156,7 @@ def live_info(runner, *, update_interval=0.5):
126156
Returns an interactive ipywidget that can be
127157
visualized in a Jupyter notebook.
128158
"""
129-
if not _plotting_enabled:
159+
if not _holoviews_enabled:
130160
raise RuntimeError("Live plotting is not enabled; did you run "
131161
"'adaptive.notebook_extension()'?")
132162

docs/environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies:
1010
- scipy
1111
- holoviews
1212
- bokeh
13+
- plotly
1314
- ipyparallel
1415
- distributed
1516
- ipykernel>=4.8*

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ dependencies:
1414
- jupyter_client>=5.2.2
1515
- ipywidgets
1616
- scikit-optimize
17+
- plotly

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def get_version_and_cmdclass(package_name):
3838
'ipywidgets',
3939
'bokeh',
4040
'matplotlib',
41+
'plotly',
4142
],
4243
}
4344

0 commit comments

Comments
 (0)