本文整理汇总了Python中traitlets.config.manager.BaseJSONConfigManager类的典型用法代码示例。如果您正苦于以下问题:Python BaseJSONConfigManager类的具体用法?Python BaseJSONConfigManager怎么用?Python BaseJSONConfigManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BaseJSONConfigManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: list_labextensions
def list_labextensions(self):
"""List all the labextensions"""
print("Known labextensions:")
for config_dir in jupyter_config_path():
cm = BaseJSONConfigManager(parent=self, config_dir=config_dir)
data = cm.get(CONFIG_NAME)
labextensions = (
data.setdefault("LabApp", {})
.setdefault("labextensions", {})
)
if labextensions:
print(u'config dir: {}'.format(config_dir))
for name, config in sorted(labextensions.items()):
if isinstance(config, bool):
config = dict(enabled=config)
enabled = config['enabled']
full_dest = find_labextension(name)
print(u' {} {}: {}'.format(
name,
GREEN_ENABLED if enabled else RED_DISABLED,
full_dest if not None else RED_X+" Files not found"
))
if full_dest is not None:
validate_labextension_folder(name, full_dest, self.log)
示例2: _set_nbextension_state
def _set_nbextension_state(section, require, state, user=True, sys_prefix=False, logger=None):
"""Set whether the section's frontend should require the named nbextension
Returns True if the final state is the one requested.
Parameters
----------
section : string
The section of the server to change, one of NBCONFIG_SECTIONS
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
示例3: _install_extension_nb41
def _install_extension_nb41(enable=True):
"""deprecated, pre-4.2 implementation of installing notebook extension"""
# server-side
server = BaseJSONConfigManager(config_dir=jupyter_config_dir())
server_cfg = server.get('jupyter_notebook_config')
app_cfg = server_cfg.get('NotebookApp', {})
server_extensions = app_cfg.get('server_extensions', [])
server_ext = 'ipyparallel.nbextension'
server_changed = False
if enable and server_ext not in server_extensions:
server_extensions.append(server_ext)
server_changed = True
elif (not enable) and server_ext in server_extensions:
server_extensions.remove(server_ext)
server_changed = True
if server_changed:
server.update('jupyter_notebook_config', {
'NotebookApp': {
'server_extensions': server_extensions,
}
})
# frontend config (*way* easier because it's a dict)
frontend = FrontendConfigManager()
frontend.update('tree', {
'load_extensions': {
'ipyparallel/main': enable or None,
}
})
示例4: get
def get(self, section_name):
"""Get the config from all config sections."""
config = {}
for p in self.read_config_path:
cm = BaseJSONConfigManager(config_dir=p)
recursive_update(config, cm.get(section_name))
return config
示例5: _install_kernelspec_manager
def _install_kernelspec_manager(prefix, disable=False):
CKSM = "beakerx.kernel_spec.BeakerXKernelSpec"
KSMC = "kernel_spec_class"
action_prefix = "Dis" if disable else "En"
log.info("{}abling BeakerX server config...".format(action_prefix))
path = os.path.join(prefix, "etc", "jupyter")
if not os.path.exists(path):
log.debug("Making directory {}...".format(path))
os.makedirs(path)
cm = BaseJSONConfigManager(config_dir=path)
cfg = cm.get("jupyter_notebook_config")
log.debug("Existing config in {}...\n{}".format(path, _pretty(cfg)))
nb_app = cfg.setdefault("KernelSpecManager", {})
if disable and nb_app.get(KSMC, None) == CKSM:
nb_app.pop(KSMC)
elif not disable:
nb_app.update({KSMC: CKSM})
log.debug("Writing config in {}...".format(path))
cm.set("jupyter_notebook_config", cfg)
cfg = cm.get("jupyter_notebook_config")
log.debug("Verifying config in {}...\n{}".format(path, _pretty(cfg)))
if disable:
assert KSMC not in cfg["KernelSpecManager"]
else:
assert cfg["KernelSpecManager"][KSMC] == CKSM
log.info("{}abled BeakerX server config".format(action_prefix))
示例6: list_nbextensions
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)
示例7: list_nbextensions
def list_nbextensions(self):
"""List all the nbextensions"""
config_dirs = [os.path.join(p, 'nbconfig') for p in jupyter_config_path()]
print("Known bundlerextensions:")
for config_dir in config_dirs:
head = u' config dir: {}'.format(config_dir)
head_shown = False
cm = BaseJSONConfigManager(parent=self, config_dir=config_dir)
data = cm.get('notebook')
if 'bundlerextensions' in data:
if not head_shown:
# only show heading if there is an nbextension here
print(head)
head_shown = True
for bundler_id, info in data['bundlerextensions'].items():
label = info.get('label')
module = info.get('module_name')
if label is None or module is None:
msg = u' {} {}'.format(bundler_id, RED_DISABLED)
else:
msg = u' "{}" from {} {}'.format(
label, module, GREEN_ENABLED
)
print(msg)
示例8: install_server_extension
def install_server_extension(enable=True):
"""Register ipyparallel clusters tab as a notebook server extension
Toggle with enable=True/False.
"""
# server-side
server = BaseJSONConfigManager(config_dir=jupyter_config_dir())
server_cfg = server.get('jupyter_notebook_config')
app_cfg = server_cfg.get('NotebookApp', {})
server_extensions = app_cfg.get('server_extensions', [])
server_ext = 'ipyparallel.nbextension'
server_changed = False
if enable and server_ext not in server_extensions:
server_extensions.append(server_ext)
server_changed = True
elif (not enable) and server_ext in server_extensions:
server_extensions.remove(server_ext)
server_changed = True
if server_changed:
server.update('jupyter_notebook_config', {
'NotebookApp': {
'server_extensions': server_extensions,
}
})
# frontend config (*way* easier because it's a dict)
frontend = FrontendConfigManager()
frontend.update('tree', {
'load_extensions': {
'ipyparallel/main': enable or None,
}
})
示例9: list_labextensions
def list_labextensions(self):
"""List all the labextensions"""
print("Known labextensions:")
seen = False
for config_dir in jupyter_config_path():
config_dir = os.path.join(config_dir, CONFIG_DIR)
cm = BaseJSONConfigManager(parent=self, config_dir=config_dir)
labextensions = cm.get('labextensions')
if labextensions:
print(u'config dir: {}'.format(config_dir))
seen = True
for name, config in sorted(labextensions.items()):
if isinstance(config, bool):
config = dict(enabled=config)
enabled = config['enabled']
full_dest = find_labextension(name)
print(u' {} {}: {}'.format(
name,
GREEN_ENABLED if enabled else RED_DISABLED,
full_dest if not None else RED_X+" Files not found"
))
if full_dest is not None:
validate_labextension_folder(name, full_dest, self.log)
if not seen:
print('....None found!')
示例10: test_labextension_disable
def test_labextension_disable(self):
self.test_labextension_enable()
disable_labextension(self.name)
config_dir = os.path.join(_get_config_dir(user=True), 'labconfig')
cm = BaseJSONConfigManager(config_dir=config_dir)
enabled = cm.get('jupyterlab_config').get('LabApp', {}).get('labextensions', {}).get(self.name, False)
assert not enabled
示例11: test_nbextension_disable
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
示例12: get
def get(self, section_name):
"""Get the config from all config sections."""
config = {}
# step through back to front, to ensure front of the list is top priority
for p in self.read_config_path[::-1]:
cm = BaseJSONConfigManager(config_dir=p)
recursive_update(config, cm.get(section_name))
return config
示例13: toggle_serverextension_python
def toggle_serverextension_python(import_name, enabled=None, parent=None,
user=True, sys_prefix=False, logger=None):
"""Toggle a server extension.
By default, toggles the extension in the system-wide Jupyter configuration
location (e.g. /usr/local/etc/jupyter).
Parameters
----------
import_name : str
Importable Python module (dotted-notation) exposing the magic-named
`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("server_extensions", [])
)
old_enabled = import_name in server_extensions
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))
if new_enabled:
if not old_enabled:
server_extensions.append(import_name)
elif old_enabled:
while import_name in server_extensions:
server_extensions.pop(server_extensions.index(import_name))
if logger:
logger.info(u"- Writing config: {}".format(config_dir))
cm.update("jupyter_notebook_config", cfg)
if new_enabled:
validate_serverextension(import_name, logger)
示例14: test_enable
def test_enable(self):
"""Should add the bundler to the notebook configuration."""
enable_bundler_python('notebook.bundler.zip_bundler')
config_dir = os.path.join(_get_config_dir(user=True), 'nbconfig')
cm = BaseJSONConfigManager(config_dir=config_dir)
bundlers = cm.get('notebook').get('bundlerextensions', {})
self.assertEqual(len(bundlers), 1)
self.assertIn('notebook_zip_download', bundlers)
示例15: test_labextensionpy_enable
def test_labextensionpy_enable(self):
self._inject_mock_extension()
install_labextension_python('mockextension', user=True)
enable_labextension_python('mockextension')
config_dir = os.path.join(_get_config_dir(user=True), 'labconfig')
cm = BaseJSONConfigManager(config_dir=config_dir)
enabled = cm.get('jupyterlab_config').get('LabApp', {}).get('labextensions', {}).get('mockextension', False)
assert enabled