Browse code

improve 'notebook_extension' and add 'plotly' as a dependency

Bas Nijholt authored on 20/10/2018 13:04:45
Showing 4 changed files
... ...
@@ -7,28 +7,42 @@ import warnings
7 7
 
8 8
 
9 9
 _async_enabled = False
10
-_plotting_enabled = False
10
+_holoviews_enabled = False
11
+_ipywidgets_enabled = False
12
+_plotly_enabled = False
11 13
 
12 14
 
13 15
 def notebook_extension():
16
+    """Enable ipywidgets, holoviews, and asyncio notebook integration."""
14 17
     if not in_ipynb():
15 18
         raise RuntimeError('"adaptive.notebook_extension()" may only be run '
16 19
                            'from a Jupyter notebook.')
17 20
 
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
20 34
     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
25 38
     except ModuleNotFoundError:
26
-        warnings.warn("holoviews and (or) ipywidgets are not installed; plotting "
39
+        warnings.warn("ipywidgets is not installed; live_info "
27 40
                       "is disabled.", RuntimeWarning)
28 41
 
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
32 46
 
33 47
 
34 48
 def ensure_holoviews():
... ...
@@ -38,6 +52,22 @@ def ensure_holoviews():
38 52
         raise RuntimeError('holoviews is not installed; plotting is disabled.')
39 53
 
40 54
 
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
+
41 71
 def in_ipynb():
42 72
     try:
43 73
         # 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):
72 102
     dm : `holoviews.core.DynamicMap`
73 103
         The plot that automatically updates every `update_interval`.
74 104
     """
75
-    if not _plotting_enabled:
105
+    if not _holoviews_enabled:
76 106
         raise RuntimeError("Live plotting is not enabled; did you run "
77 107
                            "'adaptive.notebook_extension()'?")
78 108
 
... ...
@@ -126,7 +156,7 @@ def live_info(runner, *, update_interval=0.5):
126 156
     Returns an interactive ipywidget that can be
127 157
     visualized in a Jupyter notebook.
128 158
     """
129
-    if not _plotting_enabled:
159
+    if not _holoviews_enabled:
130 160
         raise RuntimeError("Live plotting is not enabled; did you run "
131 161
                            "'adaptive.notebook_extension()'?")
132 162
 
... ...
@@ -10,6 +10,7 @@ dependencies:
10 10
     - scipy
11 11
     - holoviews
12 12
     - bokeh
13
+    - plotly
13 14
     - ipyparallel
14 15
     - distributed
15 16
     - ipykernel>=4.8*
... ...
@@ -14,3 +14,4 @@ dependencies:
14 14
   - jupyter_client>=5.2.2
15 15
   - ipywidgets
16 16
   - scikit-optimize
17
+  - plotly
... ...
@@ -38,6 +38,7 @@ extras_require = {
38 38
         'ipywidgets',
39 39
         'bokeh',
40 40
         'matplotlib',
41
+        'plotly',
41 42
     ],
42 43
 }
43 44