Exemplo n.º 1
0
def n_action(option, opt_str, value, parser):
    args = [arg for arg in sys.argv[1:] if arg != opt_str]
    options, args = parser.parse_args(args)
    sys.argv = ['notebook']
    try:  # Jupyter/IPython >= 4.0
        from notebook.notebookapp import NotebookApp
        jupyter = True
    except:  # IPython <4.0
        from IPython.html.notebookapp import NotebookApp
        jupyter = False

    if jupyter:
        ipython_dir = param.resolve_path('platform/ipython',
                                         path_to_file=False)
        os.environ['IPYTHONDIR'] = ipython_dir
        config_dir = param.resolve_path('platform/jupyter', path_to_file=False)
        NotebookApp.config_dir = config_dir
    else:
        if options.Profile is None:
            config_dir = param.resolve_path('platform/ipython/',
                                            path_to_file=False)
            NotebookApp.ipython_dir = config_dir
        else:
            NotebookApp.profile = options.Profile
    if options.IP is not None:
        NotebookApp.ip = options.IP
    if options.Port is not None:
        NotebookApp.port = options.Port
    NotebookApp().launch_instance()
    global something_executed
    something_executed = True
Exemplo n.º 2
0
 def start_thread():
     if 'asyncio' in sys.modules:
         import asyncio
         asyncio.set_event_loop(asyncio.new_event_loop())
     app = cls.notebook = NotebookApp(
         port=cls.port,
         port_retries=0,
         open_browser=False,
         config_dir=cls.config_dir,
         data_dir=cls.data_dir,
         runtime_dir=cls.runtime_dir,
         notebook_dir=cls.notebook_dir,
         base_url=cls.url_prefix,
         config=config,
         allow_root=True,
         token=cls.token,
     )
     # don't register signal handler during tests
     app.init_signal = lambda: None
     # clear log handlers and propagate to root for nose to capture it
     # needs to be redone after initialize, which reconfigures logging
     app.log.propagate = True
     app.log.handlers = []
     app.initialize(argv=[])
     app.log.propagate = True
     app.log.handlers = []
     loop = IOLoop.current()
     loop.add_callback(started.set)
     try:
         app.start()
     finally:
         # set the event, so failure to start doesn't cause a hang
         started.set()
         app.session_manager.close()
Exemplo n.º 3
0
 def test_load_extension(self):
     app = NotebookApp()
     stderr = sys.stderr
     sys.stderr = self.devnull
     app.initialize(argv=[])
     sys.stderr = stderr
     load_jupyter_server_extension(app)
Exemplo n.º 4
0
    def pwnbook(_parser, cmd, args):  # pragma: no cover
        """
        Start a jupyter notebook with the pwnypack enhanced kernel pre-loaded.
        """

        notebook = NotebookApp(kernel_spec_manager_class=KSM)
        notebook.initialize(args)
        notebook.start()
    def test_load_ordered(self):
        app = NotebookApp()
        app.nbserver_extensions = OrderedDict([('mockextension2',True),('mockextension1',True)])

        app.init_server_extensions()

        assert app.mockII is True, "Mock II should have been loaded"
        assert app.mockI is True, "Mock I should have been loaded"
        assert app.mock_shared == 'II', "Mock II should be loaded after Mock I"
def main():
    current_directory = os.path.dirname(os.path.realpath(__file__))
    TestPlugin.app = NotebookApp(open_browser=False, notebook_dir=current_directory)
    TestPlugin.app.initialize()
    TestPlugin.mock_docker = MagicMock()

    io_loop = IOLoop.current()
    io_loop.call_later(1, _run_tests_in_thread)
    with patch("docker.from_env", return_value=TestPlugin.mock_docker):
        TestPlugin.app.start()
Exemplo n.º 7
0
 def test_load_extension(self):
     app = NotebookApp()
     stderr = sys.stderr
     sys.stderr = self.devnull
     app.initialize()
     sys.stderr = stderr
     web_app = app.web_app
     prev = len(web_app.handlers)
     load_jupyter_server_extension(app)
     assert len(web_app.handlers) > prev
Exemplo n.º 8
0
 def test_add_handlers(self):
     app = NotebookApp()
     stderr = sys.stderr
     sys.stderr = self.devnull
     app.initialize()
     sys.stderr = stderr
     web_app = app.web_app
     prev = len(web_app.handlers)
     add_handlers(web_app, {})
     assert len(web_app.handlers) > prev
def nb_app():
    from notebook.notebookapp import NotebookApp
    app = NotebookApp()
    app.log_level = logging.DEBUG
    app.ip = '127.0.0.1'
    app.token = ''
    app.password = ''
    app.disable_check_xsrf = True
    app.initialize()
    return app.web_app
Exemplo n.º 10
0
def dask_setup(scheduler):
    app = NotebookApp()
    ip = socket.gethostbyname(socket.gethostname())
    app.ip = "0.0.0.0"
    app.initialize([])
    Run.get_context().log(
        "jupyter-url",
        "http://" + ip + ":" + str(app.port) + "/?token=" + app.token)
    Run.get_context().log("jupyter-port", app.port)
    Run.get_context().log("jupyter-token", app.token)
    Run.get_context().log("jupyter-ip", ip)
Exemplo n.º 11
0
def nb_app():
    sys.argv = ["--port=6005", "--ip=127.0.0.1", "--no-browser", "--debug"]
    from notebook.notebookapp import NotebookApp
    app = NotebookApp()
    app.log_level = logging.DEBUG
    app.ip = '127.0.0.1'
    app.token = ''
    app.password = ''
    app.disable_check_xsrf = True
    app.initialize()
    return app.web_app
Exemplo n.º 12
0
def test_ignores_tasks_whose_source_is_not_a_file(monkeypatch, capsys,
                                                  tmp_directory,
                                                  no_sys_modules_cache):
    """
    Context: jupyter extension only applies to tasks whose source is a script,
    otherwise it will break, trying to get the source location. This test
    checks that a SQLUpload (whose source is a data file) task is ignored
    from the extension
    """
    monkeypatch.setattr(sys, 'argv', ['jupyter'])
    spec = {
        'meta': {
            'extract_product': False,
            'extract_upstream': False,
            'product_default_class': {
                'SQLUpload': 'SQLiteRelation'
            }
        },
        'clients': {
            'SQLUpload': 'db.get_client',
            'SQLiteRelation': 'db.get_client'
        },
        'tasks': [{
            'source': 'some_file.csv',
            'name': 'task',
            'class': 'SQLUpload',
            'product': ['some_table', 'table']
        }]
    }

    with open('pipeline.yaml', 'w') as f:
        yaml.dump(spec, f)

    Path('db.py').write_text("""
from ploomber.clients import SQLAlchemyClient

def get_client():
    return SQLAlchemyClient('sqlite://')
""")

    Path('file.py').touch()

    app = NotebookApp()
    app.initialize()
    app.contents_manager.get('file.py')

    out, err = capsys.readouterr()

    assert 'Traceback' not in err
Exemplo n.º 13
0
    def __call__(self, args):
        kwargs = {}
        from notebook.notebookapp import NotebookApp

        print(
            "You must choose a password so that others cannot connect to "
            "your notebook."
        )
        pw = ytcfg.get("yt", "notebook_password")
        if len(pw) == 0 and not args.no_password:
            import IPython.lib

            pw = IPython.lib.passwd()
            print("If you would like to use this password in the future,")
            print("place a line like this inside the [yt] section in your")
            print("yt configuration file at ~/.config/yt/yt.toml")
            print()
            print(f"notebook_password = {pw}")
            print()
        elif args.no_password:
            pw = None
        if args.port != 0:
            kwargs["port"] = int(args.port)
        if args.profile is not None:
            kwargs["profile"] = args.profile
        if pw is not None:
            kwargs["password"] = pw
        app = NotebookApp(open_browser=args.open_browser, **kwargs)
        app.initialize(argv=[])
        print()
        print("***************************************************************")
        print()
        print("The notebook is now live at:")
        print()
        print(f"     http://127.0.0.1:{app.port}/")
        print()
        print("Recall you can create a new SSH tunnel dynamically by pressing")
        print(f"~C and then typing -L{app.port}:localhost:{app.port}")
        print("where the first number is the port on your local machine. ")
        print()
        print(
            "If you are using %s on your machine already, try "
            "-L8889:localhost:%s" % (app.port, app.port)
        )
        print()
        print("***************************************************************")
        print()
        app.start()
Exemplo n.º 14
0
    def test_merge_config(self):
        # enabled at sys level
        mock_sys = self._inject_mock_extension('mockext_sys')
        # enabled at sys, disabled at user
        mock_both = self._inject_mock_extension('mockext_both')
        # enabled at user
        mock_user = self._inject_mock_extension('mockext_user')
        # enabled at Python
        mock_py = self._inject_mock_extension('mockext_py')

        toggle_serverextension_python('mockext_sys', enabled=True, user=False)
        toggle_serverextension_python('mockext_user', enabled=True, user=True)
        toggle_serverextension_python('mockext_both', enabled=True, user=False)
        toggle_serverextension_python('mockext_both', enabled=False, user=True)

        app = NotebookApp(nbserver_extensions={'mockext_py': True})
        app.init_server_extensions()

        assert mock_user.loaded
        assert mock_sys.loaded
        assert mock_py.loaded
        assert not mock_both.loaded
Exemplo n.º 15
0
    def test_get_labconfig(self):
        # enabled at sys level
        self._inject_mock_extension('mockext_sys')
        install_labextension_python('mockext_sys', user=False)
        # enabled at sys, disabled at user
        self._inject_mock_extension('mockext_both')
        install_labextension_python('mockext_both', user=False)
        install_labextension_python('mockext_both', user=True)
        # enabled at user
        self._inject_mock_extension('mockext_user')
        install_labextension_python('mockext_user', user=True)

        enable_labextension_python('mockext_sys', user=False)
        enable_labextension_python('mockext_user', user=True)
        enable_labextension_python('mockext_both', user=False)
        disable_labextension_python('mockext_both', user=True)

        app = NotebookApp()
        config = get_labconfig(app).get('labextensions')

        assert config['mockext_user']['enabled']
        assert config['mockext_sys']['enabled']
        assert not config['mockext_both']['enabled']
Exemplo n.º 16
0
def launch_notebook(argv=None):
    """
    Launch a Jupyter Notebook, with custom Untitled filenames and
    a prepopulated first cell with necessary boilerplate code.

    Notes
    -----
    To populate the first cell, the function `new_notebook` imported
    in notebook.services.contents needs to be monkey patched. Dirty
    but functional. Same thing could be achieved with custom.js or
    a ContentsManager subclass, but this is easier!
    """
    try:
        import nbformat.v4 as nbf
        from notebook.notebookapp import NotebookApp
        from notebook.services.contents import manager
        from traitlets.config import Config
    except ImportError:
        sys.exit("ERROR: Jupyter Notebook not installed in this environment. "
                 "Try with `conda install ipython jupyter notebook`")
    else:
        nbf._original_new_notebook = nbf.new_notebook
        def _prepopulate_nb_patch():
            nb = nbf._original_new_notebook()
            cell = nbf.new_code_cell("# Run this cell to complete Chimera initialization\n"
                                     "from pychimera import enable_chimera, enable_chimera_inline, chimera_view\n"
                                     "enable_chimera()\nenable_chimera_inline()\nimport chimera")
            nb['cells'].append(cell)
            return nb
        manager.new_notebook = _prepopulate_nb_patch
        app = NotebookApp()
        c = Config()
        c.FileContentsManager.untitled_notebook = "Untitled PyChimera Notebook"
        app.update_config(c)
        app.initialize(argv)
        app.start()
Exemplo n.º 17
0
def test_server_extension_is_initialized():
    app = NotebookApp()
    app.initialize()
    assert hasattr(app.contents_manager, 'load_dag')
Exemplo n.º 18
0
def test_server_extension_is_initialized():
    app = NotebookApp()
    app.initialize()
    assert isinstance(app.contents_manager, PloomberContentsManager)
Exemplo n.º 19
0
Defaults for these options can also be set by creating a file named
``jupyter_notebook_config.py`` in your Jupyter folder. The Jupyter
folder is in your home directory, ``~/.jupyter``.

To create a ``jupyter_notebook_config.py`` file, with all the defaults
commented out, you can use the following command line::

  $ jupyter notebook --generate-config


.. _options:

Options
-------

This list of options can be generated by running the following and hitting 
enter::

  $ jupyter notebook --help

"""
try:
    destination = os.path.join(os.path.dirname(__file__), 'source/config.rst')
except:
    destination = os.path.join(os.getcwd(), 'config.rst')

with open(destination, 'w') as f:
    f.write(header)
    f.write(NotebookApp().document_config_options())
Exemplo n.º 20
0
    parameter_path = os.path.join(notebook_dir, 'parameters.json')
    if not os.path.isfile(parameter_path):
        raise IOError('parameter file does not exist: ' + parameter_path)
    with open(parameter_path, 'r') as parameter_fd:
        parameters = json.load(parameter_fd)

    # set further parameters
    info_file = os.path.join(notebook_dir, 'server_info.json')
    connection_file = os.path.join(notebook_dir, 'connection_file.json')
    _set_parameter_if_absent(parameters, 'extra_services',
                             ['jupyter_singleton.handlers'])
    _set_parameter_if_absent(parameters, 'info_file', info_file)
    _set_parameter_if_absent(parameters, 'connection_file', connection_file)
    _set_parameter_if_absent(parameters, 'connection_dir', notebook_dir)
    _set_parameter_if_absent(parameters, 'open_browser', False)
    _set_parameter_if_absent(parameters, 'notebook_dir', notebook_dir)

    # start kernel connector, telling the server to connect to **the** client immediately after start
    threading.Thread(target=start_kernel_connection,
                     args=(info_file, )).start()

    # wrap the kernel manager
    notebook_app = NotebookApp(**parameters)
    singleton_kernel_manager_class = singletonkernelmanager.get_class(
        notebook_app.kernel_manager_class)
    notebook_app.kernel_manager_class = singleton_kernel_manager_class

    # start the jupyter server
    notebook_app.initialize(argv=[])
    notebook_app.start()