本文整理汇总了Python中traitlets.config.manager.BaseJSONConfigManager.update方法的典型用法代码示例。如果您正苦于以下问题:Python BaseJSONConfigManager.update方法的具体用法?Python BaseJSONConfigManager.update怎么用?Python BaseJSONConfigManager.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类traitlets.config.manager.BaseJSONConfigManager
的用法示例。
在下文中一共展示了BaseJSONConfigManager.update方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _install_extension_nb41
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
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,
}
})
示例2: _set_nbextension_state
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
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_server_extension
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
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,
}
})
示例4: _set_bundler_state
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
def _set_bundler_state(name, label, module_name, group, state,
user=True, sys_prefix=False, logger=None):
"""Set whether a bundler is enabled or disabled.
Returns True if the final state is the one requested.
Parameters
----------
name : string
Unique name of the bundler
label : string
Human-readable label for the bundler menu item in the notebook UI
module_name : string
Dotted module/package name containing the bundler
group : string
'download' or 'deploy' indicating the parent menu containing the label
state : bool
The state in which to leave the extension
user : bool [default: True]
Whether to update the user's .jupyter/nbconfig 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("{} {} bundler {}...".format(
"Enabling" if state else "Disabling",
name,
module_name
))
if state:
cm.update(BUNDLER_SECTION, {
BUNDLER_SUBSECTION: {
name: {
"label": label,
"module_name": module_name,
"group" : group
}
}
})
else:
cm.update(BUNDLER_SECTION, {
BUNDLER_SUBSECTION: {
name: None
}
})
return (cm.get(BUNDLER_SECTION)
.get(BUNDLER_SUBSECTION, {})
.get(name) is not None) == state
示例5: toggle_serverextension_python
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
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)
示例6: _set_labextension_state
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
def _set_labextension_state(name, state,
user=True, sys_prefix=False, logger=None):
"""Set whether the JupyterLab frontend should use the named labextension
Returns True if the final state is the one requested.
Parameters
name : string
The name of the extension.
state : bool
The state in which to leave the extension
user : bool [default: True]
Whether to update the user's .jupyter/labextensions 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), 'labconfig')
cm = BaseJSONConfigManager(config_dir=config_dir)
if logger:
logger.info("{} extension {}...".format(
"Enabling" if state else "Disabling",
name
))
cfg = cm.get("jupyterlab_config")
labextensions = (
cfg.setdefault("LabApp", {})
.setdefault("labextensions", {})
)
old_enabled = labextensions.get(name, None)
new_enabled = state if state is not None else not old_enabled
if logger:
if new_enabled:
logger.info(u"Enabling: %s" % (name))
else:
logger.info(u"Disabling: %s" % (name))
labextensions[name] = new_enabled
if logger:
logger.info(u"- Writing config: {}".format(config_dir))
cm.update("jupyterlab_config", cfg)
if new_enabled:
full_dest = find_labextension(name)
validate_labextension_folder(name, full_dest, logger=logger)
return old_enabled == state
示例7: _write_config_data
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
def _write_config_data(data, user=False, sys_prefix=False):
"""Update the config for the current context
Parameters
----------
data : object
An object which can be accepted by ConfigManager.update
user : bool [default: False]
Get the user's .jupyter config directory
sys_prefix : bool [default: False]
Get sys.prefix, i.e. ~/.envs/my-env/etc/jupyter
"""
config_dir = _get_config_dir(user=user, sys_prefix=sys_prefix)
config_man = BaseJSONConfigManager(config_dir=config_dir)
config_man.update('jupyter_notebook_config', data)
示例8: uninstall_nbextension
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
def uninstall_nbextension(
dest, require=None, user=False, sys_prefix=False, prefix=None, nbextensions_dir=None, logger=None
):
"""Uninstall a Javascript extension of the notebook
Removes staged files and/or directories in the nbextensions directory and
removes the extension from the frontend config.
Parameters
----------
dest : str
path to file, directory, zip or tarball archive, or URL to install
name the nbextension is installed to. For example, if destination is 'foo', then
the source file will be installed to 'nbextensions/foo', regardless of the source name.
This cannot be specified if an archive is given as the source.
require : str [optional]
require.js path used to load the extension.
If specified, frontend config loading extension will be removed.
user : bool [default: False]
Whether to install to the user's nbextensions directory.
Otherwise do a system-wide install (e.g. /usr/local/share/jupyter/nbextensions).
prefix : str [optional]
Specify install prefix, if it should differ from default (e.g. /usr/local).
Will install to ``<prefix>/share/jupyter/nbextensions``
nbextensions_dir : str [optional]
Specify absolute path of nbextensions directory explicitly.
logger : Jupyter logger [optional]
Logger instance to use
"""
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}})
示例9: _find_disable_nbextension
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
def _find_disable_nbextension(section, require, logger=None):
"""Disable an nbextension from the first config location where it is enabled.
Returns True if it changed any config, False otherwise.
"""
for config_dir in jupyter_config_path():
cm = BaseJSONConfigManager(
config_dir=os.path.join(config_dir, 'nbconfig'))
d = cm.get(section)
if d.get('load_extensions', {}).get(require, None):
if logger:
logger.info("Disabling %s extension in %s", require, config_dir)
cm.update(section, {'load_extensions': {require: None}})
return True
return False
示例10: _set_nbextension_state
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
def _set_nbextension_state(section, require, state,
user=True, sys_prefix=False, logger=None):
"""Set whether the section's frontend should require the nbextension."""
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
示例11: _write_config_data
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
def _write_config_data(data, user=False, sys_prefix=False, logger=None):
"""Update the config for the current context
Parameters
----------
data : object
An object which can be accepted by ConfigManager.update
user : bool [default: False]
Get the user's .jupyter config directory
sys_prefix : bool [default: False]
Get sys.prefix, i.e. ~/.envs/my-env/etc/jupyter
logger: logger instance
The logger instance.
"""
config_dir = _get_config_dir(user=user, sys_prefix=sys_prefix)
if logger:
logger.info(u"- Writing config: {}".format(config_dir))
config_man = BaseJSONConfigManager(config_dir=config_dir)
config_man.update(CONFIG_NAME, data)
示例12: set_password
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
def set_password(args):
password = args.password
while not password :
password1 = getpass("" if args.quiet else "Provide password: ")
password_repeat = getpass("" if args.quiet else "Repeat password: ")
if password1 != password_repeat:
print("Passwords do not match, try again")
elif len(password1) < 4:
print("Please provide at least 4 characters")
else:
password = password1
password_hash = passwd(password)
cfg = BaseJSONConfigManager(config_dir=jupyter_config_dir())
cfg.update('jupyter_notebook_config', {
'NotebookApp': {
'password': password_hash,
}
})
if not args.quiet:
print("password stored in config dir: %s" % jupyter_config_dir())
示例13: uninstall_nbextension
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
def uninstall_nbextension(dest, require=None, user=False, sys_prefix=False,
prefix=None, nbextensions_dir=None, logger=None):
"""Uninstall a Javascript extension of the notebook."""
nbext = _get_nbextension_dir(user=user, sys_prefix=sys_prefix,
prefix=prefix,
nbextensions_dir=nbextensions_dir)
dest = cast_unicode_py2(dest)
full_dest = os.path.join(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}})
示例14: toggle_install_config
# 需要导入模块: from traitlets.config.manager import BaseJSONConfigManager [as 别名]
# 或者: from traitlets.config.manager.BaseJSONConfigManager import update [as 别名]
def toggle_install_config(install, user=False, sys_prefix=False,
skip_running_check=False, logger=None):
"""Install/remove contrib nbextensions to/from jupyter_nbconvert_config."""
_err_on_running(skip_running_check=skip_running_check)
_check_conflicting_kwargs(user=user, sys_prefix=sys_prefix)
config_dir = nbextensions._get_config_dir(user=user, sys_prefix=sys_prefix)
if logger:
logger.info(
'{} jupyter_contrib_nbextensions items {} config in {}'.format(
'Installing' if install else 'Uninstalling',
'to' if install else 'from',
config_dir))
# Configure the jupyter_nbextensions_configurator serverextension to load
if install:
configurator_app = EnableJupyterNbextensionsConfiguratorApp(
user=user, sys_prefix=sys_prefix, logger=logger)
configurator_app.start()
nbextensions.enable_nbextension(
'notebook', 'contrib_nbextensions_help_item/main',
user=user, sys_prefix=sys_prefix, logger=logger)
else:
nbconf_cm = BaseJSONConfigManager(
config_dir=os.path.join(config_dir, 'nbconfig'))
for require, section in {
'contrib_nbextensions_help_item/main': 'notebook'}.items():
if logger:
logger.info('- Disabling {}'.format(require))
logger.info(
'-- Editing config: {}'.format(
nbconf_cm.file_name(section)))
nbconf_cm.update(section, {'load_extensions': {require: None}})
# Set extra template path, pre- and post-processors for nbconvert
cm = BaseJSONConfigManager(config_dir=config_dir)
config_basename = 'jupyter_nbconvert_config'
config = cm.get(config_basename)
# avoid warnings about unset version
config.setdefault('version', 1)
if logger:
logger.info(
u'- Editing config: {}'.format(cm.file_name(config_basename)))
# Set extra template path, pre- and post-processors for nbconvert
if logger:
logger.info('-- Configuring nbconvert template path')
# our templates directory
_update_config_list(config, 'Exporter.template_path', [
'.',
jupyter_contrib_nbextensions.nbconvert_support.templates_directory(),
], install)
# our preprocessors
if logger:
logger.info('-- Configuring nbconvert preprocessors')
proc_mod = 'jupyter_contrib_nbextensions.nbconvert_support'
_update_config_list(config, 'Exporter.preprocessors', [
proc_mod + '.CodeFoldingPreprocessor',
proc_mod + '.PyMarkdownPreprocessor',
], install)
if logger:
logger.info(
u'- Writing config: {}'.format(cm.file_name(config_basename)))
_set_managed_config(cm, config_basename, config, logger=logger)