10
10
11
11
import jupyter_highlight_selected_word
12
12
import latex_envs
13
- import psutil
14
13
from jupyter_contrib_core .notebook_compat import nbextensions
15
14
from jupyter_nbextensions_configurator .application import \
16
15
EnableJupyterNbextensionsConfiguratorApp
16
+ from notebook .notebookapp import list_running_servers
17
17
from traitlets .config import Config
18
18
from traitlets .config .manager import BaseJSONConfigManager
19
19
@@ -24,53 +24,37 @@ class NotebookRunningError(Exception):
24
24
pass
25
25
26
26
27
- def notebook_is_running ():
27
+ def notebook_is_running (runtime_dir = None ):
28
28
"""Return true if a notebook process appears to be running."""
29
- for p in psutil .process_iter ():
30
- # p.name() can throw exceptions due to zombie processes on Mac OS X, so
31
- # ignore psutil.ZombieProcess
32
- # (See https://code.google.com/p/psutil/issues/detail?id=428)
33
-
34
- # It isn't enough to search just the process name, we have to
35
- # search the process command to see if jupyter-notebook is running.
36
-
37
- # Checking the process command can cause an AccessDenied exception to
38
- # be thrown for system owned processes, ignore those as well
39
- try :
40
- # use lower, since python may be Python, e.g. on OSX
41
- if ('python' or 'jupyter' ) in p .name ().lower ():
42
- for arg in p .cmdline ():
43
- # the missing k is deliberate!
44
- # The usual string 'jupyter-notebook' can get truncated.
45
- if 'jupyter-noteboo' in arg :
46
- return True
47
- except (psutil .ZombieProcess , psutil .AccessDenied ):
48
- pass
29
+ try :
30
+ return bool (next (list_running_servers (runtime_dir = runtime_dir )))
31
+ except StopIteration :
49
32
return False
50
33
51
34
52
35
def toggle_install (install , user = False , sys_prefix = False , overwrite = False ,
53
36
symlink = False , prefix = None , nbextensions_dir = None ,
54
- logger = None ):
37
+ logger = None , skip_running_check = False ):
55
38
"""Install or remove all jupyter_contrib_nbextensions files & config."""
56
- if notebook_is_running ():
39
+ if not skip_running_check and notebook_is_running ():
57
40
raise NotebookRunningError (
58
41
'Cannot configure while the Jupyter notebook server is running' )
59
42
_check_conflicting_kwargs (user = user , sys_prefix = sys_prefix , prefix = prefix ,
60
43
nbextensions_dir = nbextensions_dir )
61
44
toggle_install_files (
62
45
install , user = user , sys_prefix = sys_prefix , overwrite = overwrite ,
63
46
symlink = symlink , prefix = prefix , nbextensions_dir = nbextensions_dir ,
64
- logger = logger )
47
+ logger = logger , skip_running_check = skip_running_check )
65
48
toggle_install_config (
66
- install , user = user , sys_prefix = sys_prefix , logger = logger )
49
+ install , user = user , sys_prefix = sys_prefix , logger = logger ,
50
+ skip_running_check = skip_running_check )
67
51
68
52
69
53
def toggle_install_files (install , user = False , sys_prefix = False , logger = None ,
70
54
overwrite = False , symlink = False , prefix = None ,
71
- nbextensions_dir = None ):
55
+ nbextensions_dir = None , skip_running_check = False ):
72
56
"""Install/remove jupyter_contrib_nbextensions files."""
73
- if notebook_is_running ():
57
+ if not skip_running_check and notebook_is_running ():
74
58
raise NotebookRunningError (
75
59
'Cannot configure while the Jupyter notebook server is running' )
76
60
kwargs = dict (user = user , sys_prefix = sys_prefix , prefix = prefix ,
@@ -96,9 +80,10 @@ def toggle_install_files(install, user=False, sys_prefix=False, logger=None,
96
80
nbextensions .uninstall_nbextension_python (mod .__name__ , ** kwargs )
97
81
98
82
99
- def toggle_install_config (install , user = False , sys_prefix = False , logger = None ):
83
+ def toggle_install_config (install , user = False , sys_prefix = False ,
84
+ skip_running_check = False , logger = None ):
100
85
"""Install/remove contrib nbextensions to/from jupyter_nbconvert_config."""
101
- if notebook_is_running ():
86
+ if not skip_running_check and notebook_is_running ():
102
87
raise NotebookRunningError (
103
88
'Cannot configure while the Jupyter notebook server is running' )
104
89
_check_conflicting_kwargs (user = user , sys_prefix = sys_prefix )
@@ -163,20 +148,23 @@ def toggle_install_config(install, user=False, sys_prefix=False, logger=None):
163
148
164
149
165
150
def install (user = False , sys_prefix = False , prefix = None , nbextensions_dir = None ,
166
- logger = None , overwrite = False , symlink = False ):
151
+ logger = None , overwrite = False , symlink = False ,
152
+ skip_running_check = False ):
167
153
"""Install all jupyter_contrib_nbextensions files & config."""
168
154
return toggle_install (
169
155
True , user = user , sys_prefix = sys_prefix , prefix = prefix ,
170
156
nbextensions_dir = nbextensions_dir , logger = logger ,
171
- overwrite = overwrite , symlink = symlink )
157
+ overwrite = overwrite , symlink = symlink ,
158
+ skip_running_check = skip_running_check )
172
159
173
160
174
161
def uninstall (user = False , sys_prefix = False , prefix = None , nbextensions_dir = None ,
175
- logger = None ):
162
+ logger = None , skip_running_check = False ):
176
163
"""Uninstall all jupyter_contrib_nbextensions files & config."""
177
164
return toggle_install (
178
165
False , user = user , sys_prefix = sys_prefix , prefix = prefix ,
179
- nbextensions_dir = nbextensions_dir , logger = logger )
166
+ nbextensions_dir = nbextensions_dir , logger = logger ,
167
+ skip_running_check = skip_running_check )
180
168
181
169
# -----------------------------------------------------------------------------
182
170
# Private API
0 commit comments