77
88
99_async_enabled = False
10- _plotting_enabled = False
10+ _holoviews_enabled = False
11+ _ipywidgets_enabled = False
12+ _plotly_enabled = False
1113
1214
1315def 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
3448def 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+
4171def 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
0 commit comments