Skip to content

Commit 129d19e

Browse files
committed
[install] add flag to skip check for running notebook server
allows simple installation in multiple-environment or multi-user configurations
1 parent a5166fd commit 129d19e

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

src/jupyter_contrib_nbextensions/application.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
import jupyter_contrib_nbextensions
1515
from jupyter_contrib_nbextensions.install import (
16-
install, toggle_install_config, toggle_install_files, uninstall,
16+
install, NotebookRunningError, toggle_install_config, toggle_install_files,
17+
uninstall,
1718
)
1819
from jupyter_contrib_nbextensions.migrate import migrate
1920

@@ -71,6 +72,11 @@ class BaseContribNbextensionsInstallApp(BaseContribNbextensionsApp):
7172
{'BaseContribNbextensionsInstallApp': {'symlink': True}},
7273
'Create symlinks for nbextensions instead of copying files'
7374
),
75+
'skip-running-check': (
76+
{'BaseContribNbextensionsInstallApp':
77+
{'skip_running_check': True}},
78+
'Perform actions even if notebook server(s) are already running'
79+
),
7480
}
7581

7682
_conflicting_flagsets = [['--user', '--system', '--sys-prefix'], ]
@@ -91,6 +97,9 @@ class BaseContribNbextensionsInstallApp(BaseContribNbextensionsApp):
9197
'', config=True,
9298
help='Full path to nbextensions dir '
9399
'(consider instead using system, sys_prefix, prefix or user option)')
100+
skip_running_check = Bool(
101+
False, config=True,
102+
help='Perform actions even if notebook server(s) are already running')
94103

95104
def parse_command_line(self, argv=None):
96105
"""
@@ -153,15 +162,23 @@ def start(self):
153162
sys.exit('{} takes no extra arguments'.format(self.name))
154163
self.log.info('{} {}'.format(self.name, ' '.join(self.argv)))
155164
kwargs = dict(
156-
user=self.user, sys_prefix=self.sys_prefix, logger=self.log)
165+
user=self.user, sys_prefix=self.sys_prefix, logger=self.log,
166+
skip_running_check=self.skip_running_check)
157167
kwargs_files = dict(**kwargs)
158168
kwargs_files.update(dict(
159169
prefix=self.prefix, nbextensions_dir=self.nbextensions_dir,
160170
overwrite=self.overwrite, symlink=self.symlink))
161-
if not self.only_config:
162-
toggle_install_files(self._toggle_value, **kwargs_files)
163-
if not self.only_files:
164-
toggle_install_config(self._toggle_value, **kwargs)
171+
try:
172+
if not self.only_config:
173+
toggle_install_files(self._toggle_value, **kwargs_files)
174+
if not self.only_files:
175+
toggle_install_config(self._toggle_value, **kwargs)
176+
except NotebookRunningError as err:
177+
self.log.warn('Error: %s', err)
178+
self.log.info(
179+
'To perform actions even while a notebook server is running,'
180+
'you may use the flag\n--skip-running-check')
181+
raise
165182

166183

167184
class UninstallContribNbextensionsApp(InstallContribNbextensionsApp):

src/jupyter_contrib_nbextensions/install.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,27 @@ def notebook_is_running(runtime_dir=None):
3434

3535
def toggle_install(install, user=False, sys_prefix=False, overwrite=False,
3636
symlink=False, prefix=None, nbextensions_dir=None,
37-
logger=None):
37+
logger=None, skip_running_check=False):
3838
"""Install or remove all jupyter_contrib_nbextensions files & config."""
39-
if notebook_is_running():
39+
if not skip_running_check and notebook_is_running():
4040
raise NotebookRunningError(
4141
'Cannot configure while the Jupyter notebook server is running')
4242
_check_conflicting_kwargs(user=user, sys_prefix=sys_prefix, prefix=prefix,
4343
nbextensions_dir=nbextensions_dir)
4444
toggle_install_files(
4545
install, user=user, sys_prefix=sys_prefix, overwrite=overwrite,
4646
symlink=symlink, prefix=prefix, nbextensions_dir=nbextensions_dir,
47-
logger=logger)
47+
logger=logger, skip_running_check=skip_running_check)
4848
toggle_install_config(
49-
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)
5051

5152

5253
def toggle_install_files(install, user=False, sys_prefix=False, logger=None,
5354
overwrite=False, symlink=False, prefix=None,
54-
nbextensions_dir=None):
55+
nbextensions_dir=None, skip_running_check=False):
5556
"""Install/remove jupyter_contrib_nbextensions files."""
56-
if notebook_is_running():
57+
if not skip_running_check and notebook_is_running():
5758
raise NotebookRunningError(
5859
'Cannot configure while the Jupyter notebook server is running')
5960
kwargs = dict(user=user, sys_prefix=sys_prefix, prefix=prefix,
@@ -79,9 +80,10 @@ def toggle_install_files(install, user=False, sys_prefix=False, logger=None,
7980
nbextensions.uninstall_nbextension_python(mod.__name__, **kwargs)
8081

8182

82-
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):
8385
"""Install/remove contrib nbextensions to/from jupyter_nbconvert_config."""
84-
if notebook_is_running():
86+
if not skip_running_check and notebook_is_running():
8587
raise NotebookRunningError(
8688
'Cannot configure while the Jupyter notebook server is running')
8789
_check_conflicting_kwargs(user=user, sys_prefix=sys_prefix)
@@ -146,20 +148,23 @@ def toggle_install_config(install, user=False, sys_prefix=False, logger=None):
146148

147149

148150
def install(user=False, sys_prefix=False, prefix=None, nbextensions_dir=None,
149-
logger=None, overwrite=False, symlink=False):
151+
logger=None, overwrite=False, symlink=False,
152+
skip_running_check=False):
150153
"""Install all jupyter_contrib_nbextensions files & config."""
151154
return toggle_install(
152155
True, user=user, sys_prefix=sys_prefix, prefix=prefix,
153156
nbextensions_dir=nbextensions_dir, logger=logger,
154-
overwrite=overwrite, symlink=symlink)
157+
overwrite=overwrite, symlink=symlink,
158+
skip_running_check=skip_running_check)
155159

156160

157161
def uninstall(user=False, sys_prefix=False, prefix=None, nbextensions_dir=None,
158-
logger=None):
162+
logger=None, skip_running_check=False):
159163
"""Uninstall all jupyter_contrib_nbextensions files & config."""
160164
return toggle_install(
161165
False, user=user, sys_prefix=sys_prefix, prefix=prefix,
162-
nbextensions_dir=nbextensions_dir, logger=logger)
166+
nbextensions_dir=nbextensions_dir, logger=logger,
167+
skip_running_check=skip_running_check)
163168

164169
# -----------------------------------------------------------------------------
165170
# Private API

0 commit comments

Comments
 (0)