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}})
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"))
def __init__(self, *args, **kwargs): super(ExporterCollapsibleHeadings, self).__init__(*args, **kwargs) self.inliner_resources['css'].append(""" /* no local copies of fontawesome fonts in basic templates, so use cdn */ @import url(https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css) """) # noqa: E501 ch_dir = os.path.join( os.path.dirname(contrib_init), 'nbextensions', 'collapsible_headings') with open(os.path.join(ch_dir, 'main.css'), 'r') as f: self.inliner_resources['css'].append(f.read()) with open(os.path.join(ch_dir, 'main.js'), 'r') as f: self.inliner_resources['js'].append(f.read()) cm = ConfigManager() collapsible_headings_options = cm.get('notebook').get( 'collapsible_headings', {}) self.inliner_resources['js'].append(""" $(document).ready(function () { require(['nbextensions/collapsible_headings/main'], function (ch) { ch.set_collapsible_headings_options(%s); ch.refresh_all_headings(); }); }); """ % json.dumps(collapsible_headings_options))
def _default_config_dict(self): """ load merged config from more jupyter_notebook_config.d files re-uses notebook loading machinery to look through more locations """ manager = ConfigManager(read_config_path=jupyter_config_path()) return manager.get("jupyter_notebook_config").get("StarterManager", {})
def __init__(self, *args, **kwargs): super(ExporterCollapsibleHeadings, self).__init__(*args, **kwargs) self.inliner_resources['css'].append(""" /* no local copies of fontawesome fonts in basic templates, so use cdn */ @import url(https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css) """) # noqa: E501 ch_dir = os.path.join(os.path.dirname(contrib_init), 'nbextensions', 'collapsible_headings') with open(os.path.join(ch_dir, 'main.css'), 'r') as f: self.inliner_resources['css'].append(f.read()) with open(os.path.join(ch_dir, 'main.js'), 'r') as f: self.inliner_resources['js'].append(f.read()) cm = ConfigManager() collapsible_headings_options = cm.get('notebook').get( 'collapsible_headings', {}) self.inliner_resources['js'].append(""" $(document).ready(function () { require(['nbextensions/collapsible_headings/main'], function (ch) { ch.set_collapsible_headings_options(%s); ch.refresh_all_headings(); }); }); """ % json.dumps(collapsible_headings_options))
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))
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)
def __init__(self): # # Get config from the env variables or from jupyter_cs3_config.json file # if 'CS3_EXT_CONFIG' in os.environ.keys() and utils.strtobool( os.environ['CS3_EXT_CONFIG']): self.config = {} self.log.info("Reading CS3 config from environment") for name in self.__env_names: env_name = "CS3_" + name.upper() if env_name in os.environ: self.log.info(f"Reading config value {name} = " + str(os.environ[env_name])) self.config[name] = os.environ[env_name] else: self.log.error(f"Unable to read {name}") raise ValueError( f"Unable to read {name} from environment variables") else: self.log.info( f'Reading CS3 config from file {self.__config_dir}{self.__config_file_name}' ) config_path = jupyter_config_path() if self.__config_dir not in config_path: # add self.config_dir to the front, if set manually config_path.insert(0, os.getcwd() + self.__config_dir) cm = ConfigManager(read_config_path=config_path) config_file = cm.get(self.__config_file_name) self.config = config_file.get("cs3") if self.config is None: self.log.error( f'Error while reading cs3 config file {self.__config_dir}{self.__config_file_name}' ) raise IOError(u'Error while reading cs3 config file')
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_module_dir())): files.extend(filenames) break custom_js = '' with open(path.join(Utils.get_notebook_module_dir(), '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_module_dir(), j_path.jupyter_config_dir(), ext, files, custom_js))
def __init__(self): config_path = jupyter_config_path() if self.__config_dir not in config_path: # add self.config_dir to the front, if set manually config_path.insert(0, os.getcwd() + self.__config_dir) cm = ConfigManager(read_config_path=config_path) config_file = cm.get(self.__config_file_name) config = config_file.get("cs3") # overwriting default values with config file if config is not None: for key in self.config.keys(): if key in config.keys(): self.config[key] = config[key] # overwriting the values with env vars for key in self.config.keys(): env_name = "CS3_" + key.upper() if env_name in os.environ: self.config[key] = os.environ[env_name] if len(self.config["root_dir_list"]) > 0: root_dir_list = tuple(k.strip() for k in self.config["root_dir_list"].split(',')) self.config["root_dir_list"] = root_dir_list else: self.config["root_dir_list"] = tuple() if not self.config["reva_host"]: raise KeyError("Reva host not provided") if not self.config["client_id"]: raise KeyError("Client ID not provided")
def disable_nbextension(self, name): # Local import to avoid circular import issue on Py 2 from notebook.services.config import ConfigManager cm = ConfigManager(parent=self, config=self.config) if name not in cm.get(self.section).get('load_extensions', {}): sys.exit('{} is not enabled in section {}'.format(name, self.section)) # We're using a dict as a set - updating with None removes the key cm.update(self.section, {"load_extensions": {name: None}})
def disable_nbextension(self, name): # Local import to avoid circular import issue on Py 2 from notebook.services.config import ConfigManager cm = ConfigManager(parent=self, config=self.config) if name not in cm.get(self.section).get('load_extensions', {}): sys.exit('{} is not enabled in section {}'.format( name, self.section)) # We're using a dict as a set - updating with None removes the key cm.update(self.section, {"load_extensions": {name: None}})
def get_bundler(self, bundler_id): ''' :param bundler_id: Unique ID within the notebook/jupyter_cms_bundlers config section. :returns: Dict of bundler metadata with keys label, group, module_name :raises KeyError: If the bundler is not registered ''' cm = ConfigManager() return cm.get('notebook').get('jupyter_cms_bundlers', {})[bundler_id]
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)
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)
def __init__(self, *args, **kwargs): super(ExporterCollapsibleHeadings, self).__init__(*args, **kwargs) ch_dir = os.path.join( os.path.dirname(contrib_init), 'nbextensions', 'collapsible_headings') with open(os.path.join(ch_dir, 'main.css'), 'r') as f: main_css = f.read() self.inliner_resources['css'].append(main_css) self.inliner_resources['css'].append(""" /* no local copies of fontawesome fonts from basic templates, so get them from cdn */ @font-face { font-family: 'FontAwesome'; src: url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.eot'); src: url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.eot?#iefix') format('embedded-opentype'), url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.woff') format('woff'), url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.ttf') format('truetype'), url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.svg#fontawesomeregular') format('svg'); font-weight: normal; font-style: normal; } """) # noqa: E501 with open(os.path.join(ch_dir, 'main.js'), 'r') as f: self.inliner_resources['js'].append( f.read().replace( "define([", "define('nbextensions/collapsible_headings/main', [") ) cm = ConfigManager() collapsible_headings_options = cm.get('notebook').get( 'collapsible_headings', {}) self.inliner_resources['js'].append(""" require([ 'jquery', 'nbextensions/collapsible_headings/main' ], function ( $, nbext ) { nbext.set_collapsible_headings_options(%s); $(document).ready(function () { nbext.refresh_all_headings(); }); }); """ % json.dumps(collapsible_headings_options))
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 __init__(self, *args, **kwargs): super(ExporterCollapsibleHeadings, self).__init__(*args, **kwargs) ch_dir = os.path.join( os.path.dirname(contrib_init), 'nbextensions', 'collapsible_headings') with open(os.path.join(ch_dir, 'main.css'), 'r') as f: self.inliner_resources['css'].append(f.read()) self.inliner_resources['css'].append(""" /* no local copies of fontawesome fonts from basic templates, so get them from cdn */ @font-face { font-family: 'FontAwesome'; src: url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.eot'); src: url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.eot?#iefix') format('embedded-opentype'), url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.woff') format('woff'), url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.ttf') format('truetype'), url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.svg#fontawesomeregular') format('svg'); font-weight: normal; font-style: normal; } """) # noqa: E501 with open(os.path.join(ch_dir, 'main.js'), 'r') as f: self.inliner_resources['js'].append(f.read()) cm = ConfigManager() collapsible_headings_options = cm.get('notebook').get( 'collapsible_headings', {}) self.inliner_resources['js'].append(""" require([ 'jquery', 'nbextensions/collapsible_headings/main' ], function ( $, nbext ) { nbext.set_collapsible_headings_options(%s); $(document).ready(function () { nbext.refresh_all_headings(); }); }); """ % json.dumps(collapsible_headings_options))
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 get_bundler(self, bundler_id): """ Get bundler metadata from config given a bundler ID. Parameters ---------- bundler_id: str Unique bundler ID within the notebook/bundlerextensions config section Returns ------- dict Bundler metadata with label, group, and module_name attributes Raises ------ KeyError If the bundler ID is unknown """ cm = ConfigManager() return cm.get('notebook').get('bundlerextensions', {})[bundler_id]
def get_bundler(self, bundler_id): """ Get bundler metadata from config given a bundler ID. Parameters ---------- bundler_id: str Unique bundler ID within the notebook/bundlerextensions config section Returns ------- dict Bundler metadata with label, group, and module_name attributes Raises ------ KeyError If the bundler ID is unknown """ cm = ConfigManager() return cm.get('notebook').get('bundlerextensions', {})[bundler_id]
def check_extension_enabled(cls, section, require, expected_status=True, timeout=10, check_period=0.5): try: # single-user notebook server tests use cls.notebook for app cm = cls.notebook.config_manager except AttributeError: # jupyterhub-based tests don't (can't) have cls.notebook defined, # so we must construct a ConfigManager from scratch cm = ConfigManager( log=cls.log, config_dir=os.path.join(_get_config_dir(user=True), 'nbconfig') ) for ii in range(0, max(1, int(timeout / check_period))): load_exts = cm.get(section).get('load_extensions', {}) enabled = [req for req, en in load_exts.items() if en] if (require in enabled) == expected_status: break time.sleep(check_period) assert_func = ( nt.assert_in if expected_status else nt.assert_not_in) assert_func(require, enabled, 'nbxtension should {}be in enabled list'.format( '' if expected_status else 'not '))
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)
import random import json from server.db import ipy_mongodb_events as ev import logging from collections import defaultdict from tornado.options import options, parse_command_line from tornado.log import enable_pretty_logging from bson.objectid import ObjectId from IPython.html.base.handlers import IPythonHandler, json_errors from notebook.services.config import ConfigManager cm = ConfigManager() nb = cm.get('notebook') # JSON encoder for objects returned by mongodb class Encoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, ObjectId): return str(obj) else: return obj # Service endpoints: # # /events/{gid}: # POST:
class NBSetup(object): extensions_map = { 'notebook': 'main.js', 'tree': 'tree.js', 'edit': 'edit.js' } 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) def install(self): """ Install an extension (copy or symlinks) """ try: install_nbextension(self.kwargs['static'], **self._install_params()) self._echo("Installing {}".format(self.name), 'ok') except Exception as e: self._echo(e, None) self._echo("Installing {}".format(self.name), 'fail') def enable(self): mkdir_p(self.cm.config_dir) self._enable_client_extensions() try: __import__(self.name) self._enable_server_extensions() except ImportError: pass self._echo('Enabling {}'.format(self.name), 'ok') def disable(self): # Client side self._disable_client_extension() self._disable_server_extension() def _disable_client_extension(self): for _type, filename in list(self.extensions_map.items()): cfg = self.cm.get(_type) try: nb_key = "{}/{}".format(self.name, filename[:-3]) nb_extensions = list(cfg['load_extensions'].keys()) if nb_key in nb_extensions: cfg['load_extensions'].pop(nb_key) self.cm.set(_type, cfg) self._echo("Disabling {} as {}".format(self.name, _type), 'ok') except KeyError: self._echo("{} wasn't enabled as a {}. Nothing to do.".format( self.name, _type)) def _disable_server_extension(self): cfg = self.cm_server.get("jupyter_notebook_config") try: server_extensions = cfg["NotebookApp"]["server_extensions"] if "{}.nbextension".format(self.name) in server_extensions: server_extensions.remove("{}.nbextension".format(self.name)) self.cm_server.update("jupyter_notebook_config", cfg) self._echo( "{} was disabled as a server extension".format(self.name), 'ok') except KeyError: self._echo( "{} was't enabled as a server extension. Nothing to do.". format(self.name)) def _install_params(self): params = copy.deepcopy(self.kwargs) params['destination'] = self.name if params.get('verbose', False): params['verbose'] = 2 else: params['verbose'] = 0 for key in ['enable', 'static', 'version', 'main', 'path']: try: del params[key] except KeyError: pass return params def _echo(self, msg, status=None): if status == 'ok': print(' '.join([msg, '\033[92m', 'OK' + '\033[0m'])) elif status == 'fail': print(' '.join([msg, '\033[91m', 'FAIL' + '\033[0m'])) else: print(msg) def _enable_client_extensions(self): directory = self.kwargs['static'] for key, filename in list(self.extensions_map.items()): if filename in os.listdir(directory): self.cm.update( key, { "load_extensions": { "{}/{}".format(self.name, filename[:-3]): True } }) def _enable_server_extensions(self): cfg = self.cm_server.get("jupyter_notebook_config") server_extensions = (cfg.setdefault("NotebookApp", {}).setdefault( "server_extensions", [])) if "{}.nbextension".format(self.name) not in server_extensions: cfg["NotebookApp"]["server_extensions"] += [ "{}.nbextension".format(self.name) ] self.cm_server.update("jupyter_notebook_config", cfg)
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(abspath(dirname(__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: if "prefix" in kwargs: path = join(kwargs["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 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")) cm = ConfigManager(config_dir=join(path, "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/js/nbpresent.min": True }, } )
class NBSetup(object): extensions_map = { 'notebook': 'main.js', 'tree': 'tree.js', 'edit': 'edit.js' } 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) def install(self): """ Install an extension (copy or symlinks) """ try: install_nbextension(self.kwargs['static'], **self._install_params()) self._echo("Installing {}".format(self.name), 'ok') except Exception as e: self._echo(e, None) self._echo("Installing {}".format(self.name), 'fail') def enable(self): mkdir_p(self.cm.config_dir) self._enable_client_extensions() try: __import__(self.name) self._enable_server_extensions() except ImportError: pass self._echo('Enabling {}'.format(self.name), 'ok') def disable(self): # Client side self._disable_client_extension() self._disable_server_extension() def _disable_client_extension(self): for _type, filename in list(self.extensions_map.items()): cfg = self.cm.get(_type) try: nb_key = "{}/{}".format(self.name, filename[:-3]) nb_extensions = list(cfg['load_extensions'].keys()) if nb_key in nb_extensions: cfg['load_extensions'].pop(nb_key) self.cm.set(_type, cfg) self._echo("Disabling {} as {}".format(self.name, _type), 'ok') except KeyError: self._echo("{} wasn't enabled as a {}. Nothing to do.".format(self.name, _type)) def _disable_server_extension(self): cfg = self.cm_server.get("jupyter_notebook_config") try: server_extensions = cfg["NotebookApp"]["server_extensions"] if "{}.nbextension".format(self.name) in server_extensions: server_extensions.remove("{}.nbextension".format(self.name)) self.cm_server.update("jupyter_notebook_config", cfg) self._echo("{} was disabled as a server extension".format(self.name), 'ok') except KeyError: self._echo("{} was't enabled as a server extension. Nothing to do.".format(self.name)) def _install_params(self): params = copy.deepcopy(self.kwargs) params['destination'] = self.name if params.get('verbose', False): params['verbose'] = 2 else: params['verbose'] = 0 for key in ['enable', 'static', 'version', 'main', 'path']: try: del params[key] except KeyError: pass return params def _echo(self, msg, status=None): if status == 'ok': print(' '.join([msg, '\033[92m', 'OK' + '\033[0m'])) elif status == 'fail': print(' '.join([msg, '\033[91m', 'FAIL' + '\033[0m'])) else: print(msg) def _enable_client_extensions(self): directory = self.kwargs['static'] for key, filename in list(self.extensions_map.items()): if filename in os.listdir(directory): self.cm.update( key, { "load_extensions": { "{}/{}".format(self.name, filename[:-3]): True } } ) def _enable_server_extensions(self): cfg = self.cm_server.get("jupyter_notebook_config") server_extensions = ( cfg.setdefault("NotebookApp", {}) .setdefault("server_extensions", []) ) if "{}.nbextension".format(self.name) not in server_extensions: cfg["NotebookApp"]["server_extensions"] += ["{}.nbextension".format(self.name)] self.cm_server.update("jupyter_notebook_config", cfg)
import tornado from tornado import web import json import io import re import logging import random import string from server.db import ipy_mongodb_steps as db logger = logging.getLogger() from notebook.services.config import ConfigManager cm = ConfigManager() nb = cm.get('notebook') class Encoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, ObjectId): return str(obj) else: return obj ''' /distprojects: post: description: Create new Step put:
def install(nb_path=None, server_config=True, DEBUG=False): install_path = None print('Starting hide_code.js install...') current_dir = path.abspath(path.dirname(__file__)) config_dirs = j_path.jupyter_config_path() site_packages_path = Utils.get_site_package_dir() # check for config directory with a "custom" folder # TODO update this logic to check if custom.js file exists for dir in config_dirs: custom_dir = path.join(dir, "custom") if path.isdir(custom_dir): install_path = custom_dir break # last ditch effort in case jupyter config directories don't contain custom/custom.js if install_path == None: print("No config directories contain \"custom\" folder. Trying site-packages...") install_path = path.join(site_packages_path, "notebook", "static", "custom") if nb_path != None: install_path = nb_path print("Using argument supplied path: " + install_path) if DEBUG: print(install_path) # copy js into static/custom directory in Jupyter/iPython directory if path.isdir(install_path): shutil.copyfile(path.join(current_dir, "hide_code.js"), path.join(install_path, "hide_code.js")) print('Copying hide_code.js to ' + install_path) # add require to end of custom.js to auto-load on notebook startup print("Attempting to configure custom.js to auto-load hide_code.js...") try: with open(path.join(current_dir, "auto-load.txt")) as auto: auto_load_txt = auto.read(); auto_loaded = False # check if auto-load.txt is already in custom.js with open(path.join(install_path, "custom.js"), 'r') as customJS: if auto_load_txt in customJS.read(): auto_loaded = True print("Custom.js already configured to auto-load hide_code.js.") if not auto_loaded: # append auto load require to end of custom.js with open(path.join(install_path, "custom.js"), 'a') as customJS: customJS.write(auto_load_txt) print("Configured custom.js to auto-load hide_code.js.") except: print("Custom.js not in custom directory.") else: print('Unable to install into ' + install_path) print('Directory doesn\'t exist.') print('Make sure Jupyter is installed.') if server_config: print("Attempting to configure auto-loading for hide_code export handlers.") try: # Activate the Python server extension 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: cfg['NotebookApp']['server_extensions'] += [extension] server_cm.update('jupyter_notebook_config', cfg) print('Configured jupyter to auto-load hide_code export handlers.') else: print("Jupyter already configured to auto-load export handlers.") except: print('Unable to install server extension.')
def install_bootstrapped_files(nb_path=None, server_config=True, DEBUG=False): """ Installs javascript and exporting server extensions in Jupyter notebook. Args: nb_path (string): Path to notebook module. server_config (boolean): Install exporting server extensions. DEBUG (boolean): Verbose mode. """ install_path = None print('Starting hide_code.js install...') current_dir = path.abspath(path.dirname(__file__)) config_dirs = j_path.jupyter_config_path() notebook_module_path = Utils.get_notebook_module_dir() # check for config directory with a "custom" folder # TODO update this logic to check if custom.js file exists for dir in config_dirs: custom_dir = path.join(dir, "custom") if path.isdir(custom_dir): install_path = custom_dir break # last ditch effort in case jupyter config directories don't contain custom/custom.js if install_path == None: print("No config directories contain \"custom\" folder. Trying Jupyter notebook module path...") install_path = path.join(notebook_module_path, "static", "custom") if nb_path != None: install_path = nb_path print("Using argument supplied path: " + install_path) if DEBUG: print(install_path) # copy js into static/custom directory in Jupyter/iPython directory if path.isdir(install_path): shutil.copyfile(path.join(current_dir, "hide_code.js"), path.join(install_path, "hide_code.js")) print('Copying hide_code.js to ' + install_path) # add require to end of custom.js to auto-load on notebook startup print("Attempting to configure custom.js to auto-load hide_code.js...") try: with open(path.join(current_dir, "auto-load.txt")) as auto: auto_load_txt = auto.read(); auto_loaded = False # check if auto-load.txt is already in custom.js with open(path.join(install_path, "custom.js"), 'r') as customJS: if auto_load_txt in customJS.read(): auto_loaded = True print("Custom.js already configured to auto-load hide_code.js.") if not auto_loaded: # append auto load require to end of custom.js with open(path.join(install_path, "custom.js"), 'a') as customJS: customJS.write(auto_load_txt) print("Configured custom.js to auto-load hide_code.js.") except: print("Custom.js not in custom directory.") else: print('Unable to install into ' + install_path) print('Directory doesn\'t exist.') print('Make sure Jupyter is installed.') if server_config: print("Attempting to configure auto-loading for hide_code export handlers.") try: # Activate the Python server extension 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: cfg['NotebookApp']['server_extensions'] += [extension] server_cm.update('jupyter_notebook_config', cfg) print('Configured jupyter to auto-load hide_code export handlers.') else: print("Jupyter already configured to auto-load export handlers.") except: print('Unable to install server extension.')
def uninstall_bootstrapped_files(nb_path=None, server_config=True, DEBUG=False): install_path = None print('Starting hide_code.js removal...') current_dir = path.abspath(path.dirname(__file__)) config_dirs = j_path.jupyter_config_path() notebook_module_path = Utils.get_notebook_module_dir() # check for config directory with a "custom" folder # TODO update this logic to check if custom.js file exists for dir in config_dirs: custom_dir = path.join(dir, "custom") if path.isdir(custom_dir): install_path = custom_dir break # last ditch effort in case jupyter config directories don't contain custom/custom.js if install_path == None: print("No config directories contain \"custom\" folder. Trying Jupyter notebook module path...") install_path = path.join(notebook_module_path, "static", "custom") if nb_path != None: install_path = nb_path print("Using argument supplied path: " + install_path) if DEBUG: print(install_path) # copy js into static/custom directory in Jupyter/iPython directory if path.isdir(install_path): custom_js = path.join(install_path, 'custom.js') hide_code_js = path.join(install_path, 'hide_code.js') try: if path.exists(custom_js): with open(path.join(current_dir, "auto-load.txt")) as auto: auto_load_txt = auto.read(); auto_loaded = False text = None # check if auto-load.txt is already in custom.js with open(custom_js, 'r') as customJS: text = customJS.read() if auto_load_txt in text: text = text.replace(auto_load_txt, '') auto_loaded = True if auto_loaded: with open(custom_js, 'w') as customJS: customJS.write(text) print("Successfully removed auto-loaded javascript.") if path.exists(hide_code_js): os.remove(hide_code_js) print("Successfully removed hide_code.js") except: pass if server_config: print("Attempting to remove auto-loading for hide_code export handlers.") try: # Activate the Python server extension 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 in server_extensions: cfg['NotebookApp']['server_extensions'] = None server_cm.update('jupyter_notebook_config', cfg) print('Successfully removed auto-loaded hide_code export handlers.') else: print("No hide_code server extensions to remove.") except: print("No hide_code server extensions to remove.")
def _migrate_require_paths(logger=None): """Migrate require paths from old to new values.""" if logger: logger.info('- Migrating require paths from old to new locations') mappings = { 'notebook': [ ('config/config_menu/main', 'nbextensions_configurator/config_menu/main'), ] + [(req, req.split('/', 1)[1]) for req in [ 'codemirrormode/skill/skill', 'publishing/gist_it/main', 'publishing/printview/main', 'styling/table_beautifier/main', 'styling/zenmode/main', 'usability/autosavetime/main', 'usability/autoscroll/main', 'usability/chrome-clipboard/main', 'usability/code_font_size/code_font_size', 'usability/codefolding/main', 'usability/collapsible_headings/main', 'usability/comment-uncomment/main', 'usability/datestamper/main', 'usability/dragdrop/main', 'usability/equation-numbering/main', 'usability/execute_time/ExecuteTime', 'usability/exercise/main', 'usability/exercise2/main', 'usability/freeze/main', 'usability/help_panel/help_panel', 'usability/hide_input/main', 'usability/hide_input_all/main', 'usability/highlighter/highlighter', 'usability/hinterland/hinterland', 'usability/init_cell/main', 'usability/keyboard_shortcut_editor/main', 'usability/latex_envs/latex_envs', 'usability/limit_output/main', 'usability/move_selected_cells/main', 'usability/navigation-hotkeys/main', 'usability/notify/notify', 'usability/python-markdown/main', 'usability/qtconsole/qtconsole', 'usability/rubberband/main', 'usability/ruler/main', 'usability/runtools/main', 'usability/scratchpad/main', 'usability/search-replace/main', 'usability/skip-traceback/main', 'usability/spellchecker/main', 'usability/splitcell/splitcell', 'usability/toc2/main', 'usability/toggle_all_line_numbers/main', ]], 'tree': [ ('usability/tree-filter/index', 'tree-filter/index'), ] } fecm = FrontendConfigManager() for section in mappings: conf = fecm.get(section) load_extensions = conf.get('load_extensions', {}) for old, new in mappings[section]: status = load_extensions.pop(old, None) if status is not None: if logger: logger.debug('-- Migrating {!r} -> {!r}'.format(old, new)) load_extensions[new] = status fecm.set(section, conf)
def _migrate_require_paths(logger=None): """Migrate require paths from old to new values.""" if logger: logger.info('- Migrating require paths from old to new locations') mappings = { 'notebook': [ ('config/config_menu/main', 'nbextensions_configurator/config_menu/main'), ] + [(req, req.split('/', 1)[1]) for req in [ 'codemirrormode/skill/skill', 'publishing/gist_it/main', 'publishing/printview/main', 'styling/table_beautifier/main', 'styling/zenmode/main', 'usability/autosavetime/main', 'usability/autoscroll/main', 'usability/chrome-clipboard/main', 'usability/code_font_size/code_font_size', 'usability/codefolding/main', 'usability/collapsible_headings/main', 'usability/comment-uncomment/main', 'usability/datestamper/main', 'usability/dragdrop/main', 'usability/equation-numbering/main', 'usability/execute_time/ExecuteTime', 'usability/exercise/main', 'usability/exercise2/main', 'usability/freeze/main', 'usability/help_panel/help_panel', 'usability/hide_input/main', 'usability/hide_input_all/main', 'usability/highlighter/highlighter', 'usability/hinterland/hinterland', 'usability/init_cell/main', 'usability/keyboard_shortcut_editor/main', 'usability/latex_envs/latex_envs', 'usability/limit_output/main', 'usability/move_selected_cells/main', 'usability/navigation-hotkeys/main', 'usability/notify/notify', 'usability/python-markdown/main', 'usability/qtconsole/qtconsole', 'usability/rubberband/main', 'usability/ruler/main', 'usability/runtools/main', 'usability/scratchpad/main', 'usability/search-replace/main', 'usability/skip-traceback/main', 'usability/spellchecker/main', 'usability/splitcell/splitcell', 'usability/toc2/main', 'usability/toggle_all_line_numbers/main', ]], 'tree': [ ('usability/tree-filter/index', 'tree-filter/index'), ] } fecm = FrontendConfigManager() for section in mappings: conf = fecm.get(section) load_extensions = conf.get('load_extensions', {}) for old, new in mappings[section]: status = load_extensions.pop(old, None) if status is not None: if logger: logger.debug('-- Migrating {!r} -> {!r}'.format(old, new)) load_extensions[new] = status fecm.set(section, conf)
def install(enable=False, **kwargs): """ Install the nbbrowserpdf 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', 'nbbrowserpdf') kwargs = {k: v for k, v in kwargs.items() if not (v is None)} kwargs["destination"] = "nbbrowserpdf" install_nbextension(directory, **kwargs) if enable: if "prefix" in kwargs: path = join(kwargs["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 nbbrowserpdf 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 "nbbrowserpdf" not in server_extensions: cfg["NotebookApp"]["server_extensions"] += ["nbbrowserpdf"] cm.update("jupyter_notebook_config", cfg) print("New config...") pprint(cm.get("jupyter_notebook_config")) cm = ConfigManager(config_dir=join(path, "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": { "nbbrowserpdf/index": True }, } )