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)
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.""" if not skip_running_check and notebook_is_running(): raise NotebookRunningError( 'Cannot configure while the Jupyter notebook server is running') _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)