예제 #1
0
def notebook_server():
    info = {}
    temp_dir = TemporaryDirectory()
    td = temp_dir.name
    # do not use context manager because of https://github.com/vatlab/sos-notebook/issues/214
    if True:
        nbdir = info['nbdir'] = pjoin(td, 'notebooks')
        os.makedirs(pjoin(nbdir, u'sub ∂ir1', u'sub ∂ir 1a'))
        os.makedirs(pjoin(nbdir, u'sub ∂ir2', u'sub ∂ir 1b'))
        # print(nbdir)
        info['extra_env'] = {
            'JUPYTER_CONFIG_DIR': pjoin(td, 'jupyter_config'),
            'JUPYTER_RUNTIME_DIR': pjoin(td, 'jupyter_runtime'),
            'IPYTHONDIR': pjoin(td, 'ipython'),
        }
        env = os.environ.copy()
        env.update(info['extra_env'])

        command = [
            sys.executable,
            '-m',
            'notebook',
            '--no-browser',
            '--notebook-dir',
            nbdir,
            # run with a base URL that would be escaped,
            # to test that we don't double-escape URLs
            '--NotebookApp.base_url=/a@b/',
        ]
        print("command=", command)
        proc = info['popen'] = Popen(command, cwd=nbdir, env=env)
        info_file_path = pjoin(td, 'jupyter_runtime',
                               'nbserver-%i.json' % proc.pid)
        info.update(_wait_for_server(proc, info_file_path))

        print("Notebook server info:", info)
        yield info

    # manually try to clean up, which would fail under windows because
    # a permission error caused by iPython history.sqlite.
    try:
        temp_dir.cleanup()
    except:
        pass
    # Shut the server down
    requests.post(
        urljoin(info['url'], 'api/shutdown'),
        headers={'Authorization': 'token ' + info['token']})
예제 #2
0
def notebook_server():
    info = {}
    temp_dir = TemporaryDirectory()
    td = temp_dir.name
    # do not use context manager because of https://github.com/vatlab/sos-notebook/issues/214
    if True:
        nbdir = info["nbdir"] = pjoin(td, "notebooks")
        os.makedirs(pjoin(nbdir, u"sub ∂ir1", u"sub ∂ir 1a"))
        os.makedirs(pjoin(nbdir, u"sub ∂ir2", u"sub ∂ir 1b"))
        # print(nbdir)
        info["extra_env"] = {
            "JUPYTER_CONFIG_DIR": pjoin(td, "jupyter_config"),
            "JUPYTER_RUNTIME_DIR": pjoin(td, "jupyter_runtime"),
            "IPYTHONDIR": pjoin(td, "ipython"),
        }
        env = os.environ.copy()
        env.update(info["extra_env"])

        command = [
            sys.executable,
            "-m",
            "notebook",
            "--no-browser",
            "--notebook-dir",
            nbdir,
            # run with a base URL that would be escaped,
            # to test that we don't double-escape URLs
            "--NotebookApp.base_url=/a@b/",
        ]
        print("command=", command)
        proc = info["popen"] = Popen(command, cwd=nbdir, env=env)
        info_file_path = pjoin(td, "jupyter_runtime", "nbserver-%i.json" % proc.pid)
        info.update(_wait_for_server(proc, info_file_path))

        print("Notebook server info:", info)
        yield info

    # manually try to clean up, which would fail under windows because
    # a permission error caused by iPython history.sqlite.
    try:
        temp_dir.cleanup()
    except:
        pass
    # Shut the server down
    requests.post(
        urljoin(info["url"], "api/shutdown"),
        headers={"Authorization": "token " + info["token"]},
    )