Beispiel #1
0
def setup_info():
	server_cm = ConfigManager(config_dir=j_path.jupyter_config_dir())
	cfg = server_cm.get('jupyter_notebook_config')
	server_extensions = (cfg.setdefault('NotebookApp', {})
				.setdefault('server_extensions', [])
				)
	extension = 'hide_code.hide_code'
	if extension not in server_extensions:
		ext = 'Not loaded'
	else:
		ext = 'Loaded'

	files = []
	for (dirpath, dirnames, filenames) in os.walk(path.join(Utils.get_site_package_dir(), 'hide_code')):
		files.extend(filenames)
		break	

	custom_js = ''
	with open(path.join(Utils.get_site_package_dir(), 'notebook','static','custom','custom.js'), 'r') as f:
		for line in iter(f):
			if not line.startswith(' *') and not line.startswith('/'):
				custom_js = custom_js + line + ' '


	return ("Installation dir: {0}\nConfiguration dir: {1}\nExport handler extensions: {2}\nHide Code files: {3}\nCustom JS contents: {4}"
		.format(Utils.get_site_package_dir(), j_path.jupyter_config_dir(), ext, files, custom_js))
Beispiel #2
0
def test_config_dir_windows():
    with no_config_env:
        config = jupyter_config_dir()
    assert config == home_jupyter

    with config_env:
        config = jupyter_config_dir()
    assert config == jupyter_config_env
Beispiel #3
0
def test_config_dir_linux():
    with windows, no_config_env:
        config = jupyter_config_dir()
    assert config == home_jupyter

    with windows, config_env:
        config = jupyter_config_dir()
    assert config == jupyter_config_env
Beispiel #4
0
def test_config_dir_linux():
    with windows, no_config_env:
        config = jupyter_config_dir()
    assert config == home_jupyter
    
    with windows, config_env:
        config = jupyter_config_dir()
    assert config == jupyter_config_env
Beispiel #5
0
def test_config_dir_darwin():
    with darwin, no_config_env:
        config = jupyter_config_dir()
    assert config == home_jupyter

    with darwin, config_env:
        config = jupyter_config_dir()
    assert config == jupyter_config_env
Beispiel #6
0
def test_config_dir_darwin():
    with darwin, no_config_env:
        config = jupyter_config_dir()
    assert config == home_jupyter
    
    with darwin, config_env:
        config = jupyter_config_dir()
    assert config == jupyter_config_env
Beispiel #7
0
def install(enable=False, **kwargs):
    """Install the nbpresent nbextension assets and optionally enables the
       nbextension and server extension for every run.

    Parameters
    ----------
    enable: bool
        Enable the extension on every notebook launch
    **kwargs: keyword arguments
        Other keyword arguments passed to the install_nbextension command
    """
    from notebook.nbextensions import install_nbextension
    from notebook.services.config import ConfigManager

    directory = join(dirname(abspath(__file__)), "static", "nbpresent")

    kwargs = {k: v for k, v in kwargs.items() if not (v is None)}

    kwargs["destination"] = "nbpresent"
    install_nbextension(directory, **kwargs)

    if enable:
        path = jupyter_config_dir()
        if "prefix" in kwargs:
            path = join(kwargs["prefix"], "etc", "jupyter")
            if not exists(path):
                print("Making directory", path)
                os.makedirs(path)

        cm = ConfigManager(config_dir=path)
        print("Enabling nbpresent server component in", cm.config_dir)
        cfg = cm.get("jupyter_notebook_config")
        print("Existing config...")
        pprint(cfg)
        server_extensions = cfg.setdefault("NotebookApp", {}).setdefault("server_extensions", [])
        if "nbpresent" not in server_extensions:
            cfg["NotebookApp"]["server_extensions"] += ["nbpresent"]

        cm.update("jupyter_notebook_config", cfg)
        print("New config...")
        pprint(cm.get("jupyter_notebook_config"))

        _jupyter_config_dir = jupyter_config_dir()
        # try:
        #     subprocess.call(["conda", "info", "--root"])
        #     print("conda detected")
        #     _jupyter_config_dir = ENV_CONFIG_PATH[0]
        # except OSError:
        #     print("conda not detected")

        cm = ConfigManager(config_dir=join(_jupyter_config_dir, "nbconfig"))
        print("Enabling nbpresent nbextension at notebook launch in", cm.config_dir)

        if not exists(cm.config_dir):
            print("Making directory", cm.config_dir)
            os.makedirs(cm.config_dir)

        cm.update("notebook", {"load_extensions": {"nbpresent/nbpresent.min": True}})
Beispiel #8
0
def install(enable=False, disable=False, prefix=None):
    """Install the nb_config_manager config piece.

    Parameters
    ----------
    enable: bool
        Enable the nb_config_manager on every notebook launch
    disable: bool
        Disable nb_config_manager on every notebook launch
    """
    from notebook.services.config import ConfigManager

    if enable:
        if prefix is not None:
            path = join(prefix, "etc", "jupyter")
            if not exists(path):
                print("Making directory", path)
                os.makedirs(path)
        else:
            path = jupyter_config_dir()

        cm = ConfigManager(config_dir=path)
        print("Enabling nb_config_manager in", cm.config_dir)
        cfg = cm.get("jupyter_notebook_config")
        print("Existing config...")
        pprint(cfg)

        notebook_app = cfg.setdefault("NotebookApp", {})
        if "config_manager_class" not in notebook_app:
            cfg["NotebookApp"]["config_manager_class"] = "nb_config_manager.EnvironmentConfigManager"

        cm.update("jupyter_notebook_config", cfg)
        print("New config...")
        pprint(cm.get("jupyter_notebook_config"))

    if disable:
        if prefix is not None:
            path = join(prefix, "etc", "jupyter")
        else:
            path = jupyter_config_dir()

        cm = ConfigManager(config_dir=path)
        print("Disabling nb_config_manager in", cm.config_dir)
        cfg = cm.get("jupyter_notebook_config")
        print("Existing config...")
        pprint(cfg)

        config_manager = cfg["NotebookApp"]["config_manager_class"]

        if "nb_config_manager.EnvironmentConfigManager" == config_manager:
            cfg["NotebookApp"].pop("config_manager_class")

        cm.set("jupyter_notebook_config", cfg)
        print("New config...")
        pprint(cm.get("jupyter_notebook_config"))
Beispiel #9
0
    def enable_server_extension(self, extension):
        loader = JSONFileConfigLoader('jupyter_notebook_config.json', jupyter_config_dir())
        try:
            config = loader.load_config()
        except ConfigFileNotFound:
            config = Config()

        if 'server_extensions' not in config.NotebookApp:
            config.NotebookApp.server_extensions = []
        if extension not in config.NotebookApp.server_extensions:
            config.NotebookApp.server_extensions.append(extension)

        # save the updated config
        with io.open(os.path.join(jupyter_config_dir(), 'jupyter_notebook_config.json'), 'w+') as f:
            f.write(six.u(json.dumps(config, indent=2)))
Beispiel #10
0
    def run(self):
        # Install Python package
        install.run(self)

        # Install JavaScript extensions to ~/.local/jupyter/
        install_nbextension(EXT_DIR, overwrite=True, user=True)

        # Activate the JS extensions on the notebook, tree, and edit screens
        js_cm = ConfigManager()
        js_cm.update('notebook',
                     {"load_extensions": {
                         'nbdocker/notebook': True
                     }})
        js_cm.update('tree', {"load_extensions": {'nbdocker/dashboard': True}})
        js_cm.update('edit', {"load_extensions": {'nbdocker/editor': True}})

        # Activate the Python server extension
        server_cm = ConfigManager(config_dir=jupyter_config_dir())
        cfg = server_cm.get('jupyter_notebook_config')
        server_extensions = (cfg.setdefault('NotebookApp', {}).setdefault(
            'server_extensions', []))
        if extension not in server_extensions:
            cfg['NotebookApp']['server_extensions'] += [
                'nbdocker.DockerHanlder'
            ]
            server_cm.update('jupyter_notebook_config', cfg)
Beispiel #11
0
def get_notebook_path():
    """Returns the absolute path of the Notebook or None if it cannot be determined
    NOTE: works only when the security is token-based or there is also no password
    """
    connection_file = os.path.basename(ipykernel.get_connection_file())
    kernel_id = connection_file.split('-', 1)[1].split('.')[0]

    # Assumes you've already run `jupyter notebook --generate-config` to generate
    # `jupyter_notebook_config.py` and have edited and/or uncommented the line
    # containing `c.FileContentsManager.root_dir =`:
    c = Config()
    file_path = os.path.join(jupyter_config_dir(),
                             'jupyter_notebook_config.py')
    exec(open(file_path).read())
    root_dir = c['FileContentsManager']['root_dir']

    for srv in notebookapp.list_running_servers():
        try:
            if srv['token'] == '' and not srv[
                    'password']:  # No token and no password, ahem...
                req = urllib.request.urlopen(srv['url'] + 'api/sessions')
            else:
                req = urllib.request.urlopen(srv['url'] +
                                             'api/sessions?token=' +
                                             srv['token'])
            sessions = json.load(req)
            for sess in sessions:
                if sess['kernel']['id'] == kernel_id:

                    return os.path.abspath(
                        os.path.join(root_dir, sess['notebook']['path']))
        except:
            pass  # There may be stale entries in the runtime directory

    return None
Beispiel #12
0
def _get_config_manager(level):
    """Get the location of config files for the current context
    Returns the string to the environment
    """
    allowed = ["all", "user", "sys_prefix", "system", "app", "extension"]
    if level not in allowed:
        raise ValueError(f"Page config level must be one of: {allowed}")

    config_name = "labconfig"

    if level == "all":
        return ConfigManager(config_dir_name=config_name)

    if level == "user":
        config_dir = jupyter_config_dir()
    elif level == "sys_prefix":
        # Delayed import since this gets monkey-patched in tests
        from jupyter_core.paths import ENV_CONFIG_PATH

        config_dir = ENV_CONFIG_PATH[0]
    else:
        config_dir = SYSTEM_CONFIG_PATH[0]

    full_config_path = osp.join(config_dir, config_name)

    return ConfigManager(read_config_path=[full_config_path],
                         write_config_dir=full_config_path)
Beispiel #13
0
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,
        }},
    )
Beispiel #14
0
 def save(self, content):
     makedirs(paths.jupyter_config_dir(), exist_ok=True)
     with fdopen(os_open(self.__config_path, O_RDWR | O_CREAT, 0o600), 'w+') as json_file:
         json_file.seek(0)
         json_file.truncate()
         json_file.write(
             json.dumps(self.__dict_merge(self.__default_config.copy(), content), indent=2, sort_keys=True))
Beispiel #15
0
def get_config_dir(args):
    if args.system:
        return jp.SYSTEM_CONFIG_PATH[0]
    elif args.sys_prefix:
        return jp.ENV_CONFIG_PATH[0]
    else:
        return jp.jupyter_config_dir()
def jupyter_config_dirs():
    import jupyter_core.paths as jupypaths
    cdirs = {'user': jupypaths.jupyter_config_dir(),
             'environment': jupypaths.ENV_CONFIG_PATH[0],
             'system': jupypaths.SYSTEM_CONFIG_PATH[0]}

    return {k: os.path.join(v, 'nbconfig') for k,v in cdirs.items()}
Beispiel #17
0
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,
        }})
Beispiel #18
0
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,
        }
    })
Beispiel #19
0
def jupyter():
    '''Configuration for Jupyter
    '''
    # Get the path to Jupyter config
    jupyter_dir = jupyter_config_dir()

    # Set a target path for the CSS file
    custom_dir = os.path.join(jupyter_dir, 'custom')
    custom_css_path = os.path.join(custom_dir, 'custom.css')

    if os.path.isfile(custom_css_path):
        # Incase the user already uses a custom CSS file, back it up
        backup_css_path = os.path.join(custom_dir, 'custom.backup')
        os.rename(custom_css_path, backup_css_path)
    else:
        # Otherwise check if the custom folder is there
        if not os.path.isdir(custom_dir):
            os.makedirs(custom_dir)

    resource_package = 'nbfancy'
    config_path = '/tools/css'  # Do not use os.path.join()
    css_dir = pkg_resources.resource_filename(resource_package, config_path)

    # Copy our custom CSS file to the path
    copy(os.path.join(css_dir, 'custom.css'), custom_css_path)
    try:
        copytree(os.path.join(css_dir, 'css'), os.path.join(custom_dir, 'css'))
    except FileExistsError as e:
        print('ERROR: You already have a directory named')
        print(os.path.join(custom_dir, 'css'))
        print('Remove or rename and try again.')
        print(
            'Install will continue, on the assumption that these files are left over from a previous build.'
        )
Beispiel #20
0
def persist_config(config_file=None, mode=0o600):
    """Context manager that can be used to modify a config object

    On exit of the context manager, the config will be written back to disk, 
    by default with user-only (600) permissions.
    """

    if config_file is None:
        config_file = os.path.join(jupyter_config_dir(), 'jupyter_notebook_config.json')

    loader = JSONFileConfigLoader(os.path.basename(config_file), os.path.dirname(config_file))
    try:
        config = loader.load_config()
    except ConfigFileNotFound:
        config = Config()

    yield config

    with io.open(config_file, 'w', encoding='utf8') as f:
        f.write(cast_unicode(json.dumps(config, indent=2)))

    try:
        os.chmod(config_file, mode)
    except Exception as e:
        tb = traceback.format_exc()
        warnings.warn("Failed to set permissions on %s:\n%s" % (config_file, tb),
            RuntimeWarning)
Beispiel #21
0
 def save_setting_to_file(content):
     makedirs(paths.jupyter_config_dir(), exist_ok=True)
     with fdopen(
             osopen(EnvironmentSettings.config_path,
                    O_RDWR | O_CREAT | O_TRUNC, 0o600), 'w+') as file:
         file.write(
             json.dumps(json.loads(content), indent=4, sort_keys=True))
Beispiel #22
0
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,
        }
    })
def persist_config(config_file=None, mode=0o600):
    """Context manager that can be used to modify a config object

    On exit of the context manager, the config will be written back to disk,
    by default with user-only (600) permissions.
    """

    if config_file is None:
        config_file = os.path.join(jupyter_config_dir(),
                                   "jupyter_server_config.json")

    os.makedirs(os.path.dirname(config_file), exist_ok=True)

    loader = JSONFileConfigLoader(os.path.basename(config_file),
                                  os.path.dirname(config_file))
    try:
        config = loader.load_config()
    except ConfigFileNotFound:
        config = Config()

    yield config

    with open(config_file, "w", encoding="utf8") as f:
        f.write(json.dumps(config, indent=2))

    try:
        os.chmod(config_file, mode)
    except Exception:
        tb = traceback.format_exc()
        warnings.warn(f"Failed to set permissions on {config_file}:\n{tb}",
                      RuntimeWarning)
def persist_config(config_file=None, mode=0o600):
    """Context manager that can be used to modify a config object

    On exit of the context manager, the config will be written back to disk,
    by default with user-only (600) permissions.
    """

    if config_file is None:
        config_file = os.path.join(jupyter_config_dir(), 'jupyter_server_config.json')

    loader = JSONFileConfigLoader(os.path.basename(config_file), os.path.dirname(config_file))
    try:
        config = loader.load_config()
    except ConfigFileNotFound:
        config = Config()

    yield config

    with io.open(config_file, 'w', encoding='utf8') as f:
        f.write(cast_unicode(json.dumps(config, indent=2)))

    try:
        os.chmod(config_file, mode)
    except Exception as e:
        tb = traceback.format_exc()
        warnings.warn("Failed to set permissions on %s:\n%s" % (config_file, tb),
            RuntimeWarning)
Beispiel #25
0
def _install_notebook_extension():
    print('Installing notebook extension')
    install_nbextension(EXT_DIR, overwrite=True, user=True)
    cm = ConfigManager()
    print('Enabling extension for notebook')
    cm.update('notebook', {"load_extensions": {'urth_cms_js/notebook/main': True}})
    print('Enabling extension for dashboard')
    cm.update('tree', {"load_extensions": {'urth_cms_js/dashboard/main': True}})
    print('Enabling extension for text editor')
    cm.update('edit', {"load_extensions": {'urth_cms_js/editor/main': True}})
    print('Enabling notebook and associated files bundler')
    cm.update('notebook', { 
      'jupyter_cms_bundlers': {
        'notebook_associations_download': {
          'label': 'IPython Notebook bundle (.zip)',
          'module_name': 'urth.cms.nb_bundler',
          'group': 'download'
        }
      }
    })

    print('Installing notebook server extension')
    fn = os.path.join(jupyter_config_dir(), 'jupyter_notebook_config.py')
    with open(fn, 'r+') as fh:
        lines = fh.read()
        if SERVER_EXT_CONFIG not in lines:
            fh.seek(0, 2)
            fh.write('\n')
            fh.write(SERVER_EXT_CONFIG)
Beispiel #26
0
def _install_server_extension():
    # Install Notebook server extension
    fn = os.path.join(jupyter_config_dir(), 'jupyter_notebook_config.py')
    with open(fn, 'r+') as fh:
        lines = fh.read()
        if SERVER_EXT_CONFIG not in lines:
            fh.seek(0, 2)
            fh.write('\n')
            fh.write(SERVER_EXT_CONFIG)
Beispiel #27
0
def _install_server_extension():
    # Install Notebook server extension
    fn = os.path.join(jupyter_config_dir(), 'jupyter_notebook_config.py')
    with open(fn, 'r+') as fh:
        lines = fh.read()
        if SERVER_EXT_CONFIG not in lines:
            fh.seek(0, 2)
            fh.write('\n')
            fh.write(SERVER_EXT_CONFIG)
def jupyter_config_dirs():
    import jupyter_core.paths as jupypaths
    cdirs = {
        'user': jupypaths.jupyter_config_dir(),
        'environment': jupypaths.ENV_CONFIG_PATH[0],
        'system': jupypaths.SYSTEM_CONFIG_PATH[0]
    }

    return {k: os.path.join(v, 'nbconfig') for k, v in cdirs.items()}
Beispiel #29
0
    def clear_download(cls):
        localdir = os.path.join(jupyter_config_dir(), "custom", "labext",
                                cls.id())
        localdir = Path(localdir)
        if Path(localdir / "js").exists():
            shutil.rmtree(os.path.join(localdir, "js"))

        if Path(localdir / "css").exists():
            shutil.rmtree(os.path.join(localdir, "css"))
    def enable_server_extension(self, extension):
        loader = JSONFileConfigLoader('jupyter_notebook_config.json',
                                      jupyter_config_dir())
        try:
            config = loader.load_config()
        except ConfigFileNotFound:
            config = Config()

        if 'server_extensions' not in config.NotebookApp:
            config.NotebookApp.server_extensions = []
        if extension not in config.NotebookApp.server_extensions:
            config.NotebookApp.server_extensions.append(extension)

        # save the updated config
        with io.open(
                os.path.join(jupyter_config_dir(),
                             'jupyter_notebook_config.json'), 'w+') as f:
            f.write(six.u(json.dumps(config, indent=2)))
Beispiel #31
0
class TableSettings:
    def __init__(self):
        pass

    def save(self, content):
        makedirs(paths.jupyter_config_dir(), exist_ok=True)
        with fdopen(os_open(self.__config_path, O_RDWR | O_CREAT, 0o600),
                    'w+') as json_file:
            json_file.seek(0)
            json_file.truncate()
            json_file.write(
                json.dumps(self.__dict_merge(self.__default_config.copy(),
                                             content),
                           indent=2,
                           sort_keys=True))

    def load(self):
        try:
            json_file = open(self.__config_path)
            data = self.__dict_merge(self.__default_config,
                                     json.load(json_file))
        except:
            data = self.__default_config
            self.save(data)

        return data

    def load_options(self):
        return self.load().get('beakerx_tabledisplay').get('options')

    __default_config = {
        "beakerx_tabledisplay": {
            "version": 1,
            "options": {
                "auto_link_table_links": False,
                "show_publication": True
            }
        }
    }

    __config_path = path.join(paths.jupyter_config_dir(),
                              'beakerx_tabledisplay.json')

    def __dict_merge(self, target, *args):
        if len(args) > 1:
            for obj in args:
                self.__dict_merge(target, obj)
            return target
        obj = args[0]
        if not isinstance(obj, dict):
            return obj
        for k, v in obj.items():
            if k in target and isinstance(target[k], dict):
                self.__dict_merge(target[k], v)
            else:
                target[k] = copy.deepcopy(v)
        return target
 def __init__(self, name, **kwargs):
     self.name = name
     self.prefix = kwargs.get('prefix', None)
     self.kwargs = kwargs
     if self.prefix is None:
         self.path = jupyter_config_dir()
     else:
         self.path = join(self.prefix, "etc", "jupyter")
     self.cm = ConfigManager(config_dir=join(self.path, 'nbconfig'))
     self.cm_server = ConfigManager(config_dir=self.path)
Beispiel #33
0
 def setup_server_config(self, EXT_CONFIG):
     # Set the server extension to launch on startup by modifying the
     # jupyter_notebook_config.py
     CONFIG = os.path.join(jupyter_config_dir(), 'jupyter_config.py')
     ensure_dir_exists(jupyter_config_dir())
     if os.path.isfile(CONFIG):
         pass
     else:
         c = JupyterApp()
         c.write_default_config()
     with open(CONFIG, 'r+') as fh:
         lines = fh.read()
         for i in EXT_CONFIG:
             if i not in lines:
                 fh.seek(0, 2)
                 fh.write('\n')
                 fh.write(i)
     print("OUTCOME: Added the Stepsize server extension "
           "configuration to the jupyter_config.py")
Beispiel #34
0
def save_json_config(json_file, newconfig):
    """ Save config as JSON file
    :param json_file: Filename of JSON file
    :param newconfig: New traitlets based configuration
    """
    s = json.dumps(newconfig, indent=2, separators=(',', ': '), sort_keys=True)
    json_config = os.path.join(jupyter_config_dir(), json_file)
    make_backup(json_config)
    with open(json_config, 'w') as f:
        f.write(s)
Beispiel #35
0
 def setup_server_config(self, EXT_CONFIG):
     # Set the server extension to launch on startup by modifying the
     # jupyter_notebook_config.py
     CONFIG = os.path.join(jupyter_config_dir(), 'jupyter_config.py')
     ensure_dir_exists(jupyter_config_dir())
     if os.path.isfile(CONFIG):
         pass
     else:
         c = JupyterApp()
         c.write_default_config()
     with open(CONFIG, 'r+') as fh:
         lines = fh.read()
         for i in EXT_CONFIG:
             if i not in lines:
                 fh.seek(0, 2)
                 fh.write('\n')
                 fh.write(i)
     print("OUTCOME: Added the Stepsize server extension "
           "configuration to the jupyter_config.py")
Beispiel #36
0
def test_paths():
    output = get_jupyter_output('--paths')
    for d in (jupyter_config_dir(), jupyter_data_dir(), jupyter_runtime_dir()):
        assert d in output
    for key in ('config', 'data', 'runtime'):
        assert ('%s:' % key) in output

    for path in (jupyter_config_path(), jupyter_path()):
        for d in path:
            assert d in output
 def __init__(self, name, **kwargs):
     self.name = name
     self.prefix = kwargs.get('prefix', None)
     self.kwargs = kwargs
     if self.prefix is None:
         self.path = jupyter_config_dir()
     else:
         self.path = join(self.prefix, "etc", "jupyter")
     self.cm = ConfigManager(config_dir=join(self.path, 'nbconfig'))
     self.cm_server = ConfigManager(config_dir=self.path)
Beispiel #38
0
def test_paths():
    output = get_jupyter_output('--paths')
    for d in (jupyter_config_dir(), jupyter_data_dir(), jupyter_runtime_dir()):
        assert d in output
    for key in ('config', 'data', 'runtime'):
        assert ('%s:' % key) in output
    
    for path in (jupyter_config_path(), jupyter_path()):
        for d in path:
            assert d in output
Beispiel #39
0
def test_paths():
    output = get_jupyter_output("--paths")
    for d in (jupyter_config_dir(), jupyter_data_dir(), jupyter_runtime_dir()):
        assert d in output
    for key in ("config", "data", "runtime"):
        assert ("%s:" % key) in output

    for path in (jupyter_config_path(), jupyter_path()):
        for d in path:
            assert d in output
Beispiel #40
0
def install_post_save_hook(config_path: Optional[Path] = None):
    """Splices the post save hook into the global Jupyter configuration file
    """
    if config_path is None:
        config_dir = jupyter_config_dir()
        config_path = Path(config_dir) / "jupyter_notebook_config.py"

    config_path = config_path.expanduser().resolve()

    if not config_path.exists():
        logger.debug(
            f"No existing Jupyter configuration detected at {config_path}. Creating..."
        )
        config_path.parent.mkdir(exist_ok=True, parents=True)
        with config_path.open("w") as fp:
            fp.write(post_save_hook_initialize_block)
        logger.info("nbautoexport post-save hook installed.")
        return

    # If config exists, check for existing nbautoexport initialize block and install as appropriate
    logger.debug(f"Detected existing Jupyter configuration at {config_path}")

    with config_path.open("r") as fp:
        config = fp.read()

    if block_regex.search(config):
        logger.debug("Detected existing nbautoexport post-save hook.")

        version_match = version_regex.search(config)
        if version_match:
            existing_version = version_match.group()
            logger.debug(
                f"Existing post-save hook is version {existing_version}")
        else:
            existing_version = ""
            logger.debug("Existing post-save hook predates versioning.")

        if parse_version(existing_version) < parse_version(__version__):
            logger.info(
                f"Updating nbautoexport post-save hook with version {__version__}..."
            )
            with config_path.open("w") as fp:
                # Open as w replaces existing file. We're replacing entire config.
                fp.write(
                    block_regex.sub(post_save_hook_initialize_block, config))
        else:
            logger.debug("No changes made.")
            return
    else:
        logger.info("Installing post-save hook.")
        with config_path.open("a") as fp:
            # Open as a just appends. We append block at the end of existing file.
            fp.write("\n" + post_save_hook_initialize_block)

    logger.info("nbautoexport post-save hook installed.")
Beispiel #41
0
def set_password(args):
    password = args.password
    while not password:
        password1 = getpass("" if args.quiet else "Provide password: "******"" if args.quiet else "Repeat password:  "******"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_server_config",
               {"ServerApp": {
                   "password": password_hash,
               }})
    if not args.quiet:
        print("password stored in config dir: %s" % jupyter_config_dir())
Beispiel #42
0
def _get_config_dir(level):
    """Get the location of config files for the current context
    Returns the string to the environment
    """
    if level == 'user':
        extdir = jupyter_config_dir()
    elif level == 'sys_prefix':
        extdir = ENV_CONFIG_PATH[0]
    else:
        extdir = SYSTEM_CONFIG_PATH[0]
    return extdir
Beispiel #43
0
def install(enable=False, disable=False, prefix=None, verbose=False):
    """Install the nb_conda_kernels config piece.

    Parameters
    ----------
    enable: bool
        Enable the BeakerX server config on every notebook launch
    disable: bool
        Disable BeakerX server config
    """
    if verbose:
        log.setLevel(logging.DEBUG)

    if enable == disable:
        log.error("Please provide (one of) --enable or --disable")
        raise ValueError(enable, disable)

    log.info(
        "{}abling BeakerX server config...".format("En" if enable else "Dis"))

    path = jupyter_config_dir()

    if prefix is not None:
        path = join(prefix, "etc", "jupyter")
        if not 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 enable:
        nb_app.update({KSMC: CKSM})
    elif disable and nb_app.get(KSMC, None) == CKSM:
        nb_app.pop(KSMC)

    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 enable:
        assert cfg["KernelSpecManager"][KSMC] == CKSM
    else:
        assert KSMC not in cfg["KernelSpecManager"]

    log.info("{}abled BeakerX server config".format("En" if enable else "Dis"))
Beispiel #44
0
def install(enable=False, disable=False, prefix=None, verbose=False):
    """Install the nb_conda_kernels config piece.

    Parameters
    ----------
    enable: bool
        Enable the BeakerX server config on every notebook launch
    disable: bool
        Disable BeakerX server config
    """
    if verbose:
        log.setLevel(logging.DEBUG)

    if enable == disable:
        log.error("Please provide (one of) --enable or --disable")
        raise ValueError(enable, disable)

    log.info("{}abling BeakerX server config...".format("En" if enable else "Dis"))

    path = jupyter_config_dir()

    if prefix is not None:
        path = join(prefix, "etc", "jupyter")
        if not 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 enable:
        nb_app.update({KSMC: CKSM})
    elif disable and nb_app.get(KSMC, None) == CKSM:
        nb_app.pop(KSMC)

    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 enable:
        assert cfg["KernelSpecManager"][KSMC] == CKSM
    else:
        assert KSMC not in cfg["KernelSpecManager"]

    log.info("{}abled BeakerX server config".format("En" if enable else "Dis"))
def test_jupyter_config_path_prefer_env():
    with prefer_env, patch.object(site, 'ENABLE_USER_SITE', True):
        path = jupyter_config_path()

    # deduplicated expected values
    values = list(dict.fromkeys([
        paths.ENV_CONFIG_PATH[0],
        jupyter_config_dir(),
        os.path.join(site.getuserbase(), 'etc', 'jupyter')
    ]))
    for p,v in zip(path, values):
        assert p == v
Beispiel #46
0
    def enable_server_extension(self, extension):
        '''Enables the server side extension in the user config.'''
        server_cm = ConfigManager(config_dir=jupyter_config_dir())

        makedirs(server_cm.config_dir)

        cfg = server_cm.get('jupyter_notebook_config')
        server_extensions = (cfg.setdefault('NotebookApp', {}).setdefault(
            'server_extensions', []))
        if extension not in server_extensions:
            cfg['NotebookApp']['server_extensions'] += [extension]
        server_cm.update('jupyter_notebook_config', cfg)
Beispiel #47
0
def set_password(args):
	password = args.password
	while not password  :
		password1 = getpass("" if args.quiet else "Provide password: "******"" if args.quiet else "Repeat password:  "******"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())
Beispiel #48
0
def load_json_config(json_filename):
    """ Load config as JSON file
    :param json_filename: Filename of JSON file
    :return: Traitlets based configuration
    """
    json_config = os.path.join(jupyter_config_dir(), json_filename)
    if debug is True: print("Configuring %s" % json_config)
    if os.path.isfile(json_config) is True:
        cl = JSONFileConfigLoader(json_config)
        config = cl.load_config()
    else:
        config = Config()
    return config
def _get_config_dir(user=False, sys_prefix=False):
    """Get the location of config files for the current context."""
    user = False if sys_prefix else user
    if user and sys_prefix:
        raise ArgumentConflict(
            "Cannot specify more than one of user or sys_prefix")
    if user:
        nbext = jupyter_config_dir()
    elif sys_prefix:
        nbext = ENV_CONFIG_PATH[0]
    else:
        nbext = SYSTEM_CONFIG_PATH[0]
    return nbext
Beispiel #50
0
 def save_setting_to_file(content):
     makedirs(paths.jupyter_config_dir(), exist_ok=True)
     with fdopen(osopen(EnvironmentSettings.config_path, O_RDWR | O_CREAT, 0o600), 'w+') as file:
         file_content = file.read()
         new_settings = json.loads(content)
         if file_content:
             saved_settings = json.loads(file_content)
             file.seek(0)
             file.truncate()
             for setting_name in new_settings['beakerx']:
                 saved_settings['beakerx'][setting_name] = new_settings['beakerx'][setting_name]
         else:
             saved_settings = new_settings
         file.write(json.dumps(saved_settings, indent=4, sort_keys=True))
    def enable_server_extension(self, extension):
        '''Enables the server side extension in the user config.'''
        server_cm = ConfigManager(config_dir=jupyter_config_dir())
        
        makedirs(server_cm.config_dir)

        cfg = server_cm.get('jupyter_notebook_config')
        server_extensions = (
            cfg.setdefault('NotebookApp', {})
            .setdefault('server_extensions', [])
        )
        if extension not in server_extensions:
            cfg['NotebookApp']['server_extensions'] += [extension]
        server_cm.update('jupyter_notebook_config', cfg)
Beispiel #52
0
    def __enter__(self):
        if JUPYTER:
            self.pdir = jupyter_config_dir()
            self.cff_name = 'jupyter_notebook_config.json'
        else:
            self.pdir = locate_profile(self.profile)
            self.cff_name = 'ipython_notebook_config.json'

        jc = JSONFileConfigLoader(self.cff_name, self.pdir)

        try:
            self.config = jc.load_config();
        except (ConfigFileNotFound,ValueError):
            self.config = Config()
        return self.config
Beispiel #53
0
def persist_config(mode=0o600):
    """Context manager that can be use to modify a config object

    On exit of the context manager, the config will be written back to disk, 
    by default with 600 permissions.
    """

    loader = JSONFileConfigLoader('jupyter_notebook_config.json', jupyter_config_dir())
    try:
        config = loader.load_config()
    except ConfigFileNotFound:
        config = Config()

    yield config

    filepath = os.path.join(jupyter_config_dir(), 'jupyter_notebook_config.json')
    with io.open(filepath, 'w') as f:
        f.write(six.u(json.dumps(config, indent=2)))
    try:
        os.chmod(filepath, mode)
    except Exception:
        traceback.print_exc()

        print("Something went wrong changing file permissions")
Beispiel #54
0
def remove_server_config(CONFIG, EXT_CONFIG):
    # Removes the config in the jupyter_config.py
    if os.path.isfile(CONFIG):
        # Creates a backup file
        timestamp = datetime.datetime.now().strftime("%d-%m-%Y_T%H-%M-%S")
        title = ('backup_config_' + timestamp + '.py')
        BACKUP = os.path.join(jupyter_config_dir(), title)
        print("ACTION: Creating a backup jupter_config.py, %s" % (BACKUP))
        shutil.copyfile(CONFIG, BACKUP)
        # Counter
        count = 0
        try:
            f = open(CONFIG, 'r')
            lines = f.readlines()
            f.close()
            f = open(CONFIG, 'w')
            for line in lines:
                if any(i in line for i in EXT_CONFIG):
                    pass
                    count += 1
                else:
                    f.write(line)
            f.truncate()
            f.close()
            if count > 0:
                print("OUTCOME: Removed the Stepsize extension " \
                      "configuration from the jupyter_config.py")
        except:
            print("WARNING: An error occured when trying to remove the " \
                  "Stepsize server configuration from the jupyter_config.py")
            print("ERROR: %s, at line %d" % (sys.exc_info()[0],
                                             sys.exc_traceback.tb_lineno))
    else:
        print("WARNING: Unable to remove the Stepsize extension " \
              "configuration since the jupyter_config.py does not exist in " \
              "%s" % (jupyter_config_dir()))
Beispiel #55
0
def install_extension():
    # Import inside run() so if the user doesn't have jupyter notebook yet, we grab that dependency,
    # then run this code which imports it.
    from notebook.nbextensions import install_nbextension
    from notebook.services.config import ConfigManager
    from jupyter_core.paths import jupyter_config_dir

    # Install JavaScript extension
    install_nbextension(os.path.join(EXT_DIR, "extensions", "spark.js"), overwrite=True, user=True)

    # Activate the JS extensions on the notebook
    js_cm = ConfigManager()
    js_cm.update('notebook', {"load_extensions": {'spark': True}})

    # Activate the Python server extension
    server_cm = ConfigManager(config_dir=jupyter_config_dir())
    cfg = server_cm.get('jupyter_notebook_config')
    server_extensions = cfg.setdefault('NotebookApp', {}).setdefault('server_extensions', [])
    if "jupyter-spark.spark" not in server_extensions:
        cfg['NotebookApp']['server_extensions'] += ['jupyter-spark.spark']
        server_cm.update('jupyter_notebook_config', cfg)
    def disable_server_extension(self, extension):
        '''Disables the server side extension in the user config.'''
        server_cm = ConfigManager(config_dir=jupyter_config_dir())
        
        makedirs(server_cm.config_dir)

        cfg = server_cm.get('jupyter_notebook_config')
        if ('NotebookApp' in cfg and
            'server_extensions' in cfg['NotebookApp'] and
            extension in cfg['NotebookApp']['server_extensions']):
            cfg['NotebookApp']['server_extensions'].remove(extension)

        server_cm.update('jupyter_notebook_config', cfg)

        server_extensions = (
            cfg.setdefault('NotebookApp', {})
            .setdefault('server_extensions', [])
        )
        if extension in server_extensions:
            cfg['NotebookApp']['server_extensions'].remove(extension)
        server_cm.update('jupyter_notebook_config', cfg)
    def run(self):
        print('Installing Python module')
        install.run(self)
        
        print('Installing notebook extension')
        install_nbextension(EXT_DIR, overwrite=True, user=True)
        cm = ConfigManager()
        print('Enabling extension for notebook')
        cm.update('notebook', {"load_extensions": {'urth_cms_js/notebook/main': True}})
        print('Enabling extension for dashboard')
        cm.update('tree', {"load_extensions": {'urth_cms_js/dashboard/main': True}})
        print('Enabling extension for text editor')
        cm.update('edit', {"load_extensions": {'urth_cms_js/editor/main': True}})

        print('Installing notebook server extension')
        fn = os.path.join(jupyter_config_dir(), 'jupyter_notebook_config.py')
        with open(fn, 'r+') as fh:
            lines = fh.read()
            if SERVER_EXT_CONFIG not in lines:
                fh.seek(0, 2)
                fh.write('\n')
                fh.write(SERVER_EXT_CONFIG)
Beispiel #58
0
def _get_config_dir(user=False, sys_prefix=False):
    """Get the location of config files for the current context

    Returns the string to the enviornment

    Parameters
    ----------

    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
    """
    user = False if sys_prefix else user
    if user and sys_prefix:
        raise ArgumentConflict("Cannot specify more than one of user or sys_prefix")
    if user:
        nbext = jupyter_config_dir()
    elif sys_prefix:
        nbext = ENV_CONFIG_PATH[0]
    else:
        nbext = SYSTEM_CONFIG_PATH[0]
    return nbext