Exemplo n.º 1
0
 def test_load_extension(self):
     app = NotebookApp()
     stderr = sys.stderr
     sys.stderr = self.devnull
     app.initialize()
     sys.stderr = stderr
     load_jupyter_server_extension(app)
Exemplo n.º 2
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.º 3
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()
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()
Exemplo n.º 5
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.º 6
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.º 8
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.º 9
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
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 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.º 15
0
def test_server_extension_is_initialized():
    app = NotebookApp()
    app.initialize()
    assert hasattr(app.contents_manager, 'load_dag')
Exemplo n.º 16
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()
Exemplo n.º 17
0
def test_server_extension_is_initialized():
    app = NotebookApp()
    app.initialize()
    assert isinstance(app.contents_manager, PloomberContentsManager)