Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_nbextensionpy_enable(self):
self._inject_mock_extension('notebook')
install_nbextension_python('mockextension', user=True)
enable_nbextension_python('mockextension')
config_dir = os.path.join(_get_config_dir(user=True), 'nbconfig')
cm = BaseJSONConfigManager(config_dir=config_dir)
enabled = cm.get('notebook').get('load_extensions', {}).get('_mockdestination/index', False)
assert enabled
def _get_config(self, user=True):
cm = BaseJSONConfigManager(config_dir=_get_config_dir(user))
data = cm.get("jupyter_notebook_config")
return data.get("NotebookApp", {}).get("nbserver_extensions", {})
def test_nbextension_disable(self):
self.test_nbextension_enable()
disable_nbextension(section='notebook', require=u'ƒ')
config_dir = os.path.join(_get_config_dir(user=True), 'nbconfig')
cm = BaseJSONConfigManager(config_dir=config_dir)
enabled = cm.get('notebook').get('load_extensions', {}).get(u'ƒ', False)
assert not enabled
"""
nbext = _get_nbextension_dir(user=user, sys_prefix=sys_prefix, prefix=prefix, nbextensions_dir=nbextensions_dir)
dest = cast_unicode_py2(dest)
full_dest = pjoin(nbext, dest)
if os.path.lexists(full_dest):
if logger:
logger.info("Removing: %s" % full_dest)
if os.path.isdir(full_dest) and not os.path.islink(full_dest):
shutil.rmtree(full_dest)
else:
os.remove(full_dest)
# Look through all of the config sections making sure that the nbextension
# doesn't exist.
config_dir = os.path.join(_get_config_dir(user=user, sys_prefix=sys_prefix), 'nbconfig')
cm = BaseJSONConfigManager(config_dir=config_dir)
if require:
for section in NBCONFIG_SECTIONS:
cm.update(section, {"load_extensions": {require: None}})
require : string
An importable AMD module inside the nbextensions static path
state : bool
The state in which to leave the extension
user : bool [default: True]
Whether to update the user's .jupyter/nbextensions directory
sys_prefix : bool [default: False]
Whether to update the sys.prefix, i.e. environment. Will override
`user`.
logger : Jupyter logger [optional]
Logger instance to use
"""
user = False if sys_prefix else user
config_dir = os.path.join(
_get_config_dir(user=user, sys_prefix=sys_prefix), 'nbconfig')
cm = BaseJSONConfigManager(config_dir=config_dir)
if logger:
logger.info("{} {} extension {}...".format(
"Enabling" if state else "Disabling",
section,
require
))
cm.update(section, {"load_extensions": {require: state}})
validate_nbextension(require, logger=logger)
return cm.get(section).get(require) == state
`load_jupyter_server_extension` function
enabled : bool [default: None]
Toggle state for the extension. Set to None to toggle, True to enable,
and False to disable the extension.
parent : Configurable [default: None]
user : bool [default: True]
Toggle in the user's configuration location (e.g. ~/.jupyter).
sys_prefix : bool [default: False]
Toggle in the current Python environment's configuration location
(e.g. ~/.envs/my-env/etc/jupyter). Will override `user`.
logger : Jupyter logger [optional]
Logger instance to use
"""
user = False if sys_prefix else user
config_dir = _get_config_dir(user=user, sys_prefix=sys_prefix)
cm = BaseJSONConfigManager(parent=parent, config_dir=config_dir)
cfg = cm.get("jupyter_notebook_config")
server_extensions = (
cfg.setdefault("NotebookApp", {})
.setdefault("nbserver_extensions", {})
)
old_enabled = server_extensions.get(import_name, None)
new_enabled = enabled if enabled is not None else not old_enabled
if logger:
if new_enabled:
logger.info(u"Enabling: %s" % (import_name))
else:
logger.info(u"Disabling: %s" % (import_name))
server_extensions[import_name] = new_enabled
def list_nbextensions(self):
"""List all the nbextensions"""
config_dirs = [os.path.join(p, 'nbconfig') for p in jupyter_config_path()]
print("Known nbextensions:")
for config_dir in config_dirs:
head = u' config dir: {}'.format(config_dir)
head_shown = False
cm = BaseJSONConfigManager(parent=self, config_dir=config_dir)
for section in NBCONFIG_SECTIONS:
data = cm.get(section)
if 'load_extensions' in data:
if not head_shown:
# only show heading if there is an nbextension here
print(head)
head_shown = True
print(u' {} section'.format(section))
for require, enabled in data['load_extensions'].items():
print(u' {} {}'.format(
require,
GREEN_ENABLED if enabled else RED_DISABLED))
if enabled:
validate_nbextension(require, logger=self.log)