Exemple #1
0
def kernelspec_from_language(language):
    """Return the python kernel that matches the current env, or the first kernel that matches the given language"""
    try:
        if language == 'python':
            # Return the kernel that matches the current Python executable
            for name in find_kernel_specs():
                kernel_specs = get_kernel_spec(name)
                if kernel_specs.language == 'python' and kernel_specs.argv[
                        0] == sys.executable:
                    return {
                        'name': name,
                        'language': language,
                        'display_name': kernel_specs.display_name
                    }

        for name in find_kernel_specs():
            kernel_specs = get_kernel_spec(name)
            if kernel_specs.language == language or (
                    language == 'c++'
                    and kernel_specs.language.startswith('C++')):
                return {
                    'name': name,
                    'language': language,
                    'display_name': kernel_specs.display_name
                }
    except (KeyError, ValueError):
        pass
    return None
Exemple #2
0
def kernelspec_from_language(language):
    """Return the python kernel that matches the current env, or the first kernel that matches the given language"""
    if language == "python":
        # Return the kernel that matches the current Python executable
        for name in find_kernel_specs():
            kernel_specs = get_kernel_spec(name)
            cmd = kernel_specs.argv[0]
            if (
                kernel_specs.language == "python"
                and os.path.isfile(cmd)
                and os.path.samefile(cmd, sys.executable)
            ):
                return {
                    "name": name,
                    "language": language,
                    "display_name": kernel_specs.display_name,
                }
        raise ValueError(
            "No kernel found that matches the current python executable {}\n".format(
                sys.executable
            )
            + "Install one with 'python -m ipykernel install --name kernel_name [--user]'"
        )

    for name in find_kernel_specs():
        kernel_specs = get_kernel_spec(name)
        if same_language(kernel_specs.language, language):
            return {
                "name": name,
                "language": language,
                "display_name": kernel_specs.display_name,
            }

    raise ValueError("No kernel found for the language {}".format(language))
Exemple #3
0
    def write_notebook(self,specname=None,backup=None,keyname=None):
        '''
        write keyvalue into database notebook kernel file

        This is an experimantal feature ... wouldnt advise using it
        '''
        verbose = self.verbose
        # see https://jupyter-client.readthedocs.io/en/latest/kernels.html
        specname = specname or self.database
        backup = backup or self.backup
        keyname = keyname or self.keyname
        try:
            # exists
            spec = kernelspec.get_kernel_spec(specname)
        except:
            # doesnt exist
            spec = kernelspec.get_kernel_spec(backup)
            kernel_file = kernelspec.find_kernel_specs()[backup]
            # create specname
            # try as user then not
            try:
                kernelspec.install_kernel_spec(kernel_file,
                kernel_name=specname, user=False,replace=True)
            except:
                kernelspec.install_kernel_spec(kernel_file,
                                               kernel_name=specname, 
                                               user=True,replace=True)
        try:
            spec = kernelspec.get_kernel_spec(specname)
            # now load specname
            # make a name derived from specname
            spec.display_name = ' '.join([i.capitalize() for i in specname.split('_')])
            # make sure language is python
            spec.language='python'
            # add env variable if we can
            if spec.has_trait('env'):
                spec.env[keyname] = keyvalue
            else:
                # work out how to add env trait at some point
                pass

            # Serializing json 
            json_object  = spec.to_json()

            # write this out
            kernel = Path(spec.resource_dir + '/kernel.json')
            kernel.parent.mkdir(parents=True, exist_ok=True)
            with open(kernel, 'w') as f:
                if verbose:
                    print(f'jupyter kernel written to {kernel}')
                f.write(json_object) 

            self.make_icons(spec.resource_dir)
            # load again for luck
            spec = kernelspec.get_kernel_spec(specname)
            return spec
        except:
            return None
Exemple #4
0
def remove_kernelspecs():
    km = kernelspec.KernelSpecManager()

    kernel_id = None
    while kernel_id != "done":

        remote_kernels = {
            kernelspec.get_kernel_spec(k).display_name: k
            for k, v in kernelspec.find_kernel_specs().items()
            if k.startswith("ssh_")
        }
        if remote_kernels == {}:
            print_ok("   => No databricklabs_jupyterlab kernel spec left")
            break

        answer = question(
            "kernel_name",
            "Which kernel spec to delete (Ctrl-C to finish)?",
            list(remote_kernels.keys()),
        )

        if answer is None:
            break

        kernel_name = answer["kernel_name"]
        if kernel_id != "done":
            answer = input("Really delete kernels spec '%s' (y/n) " %
                           kernel_name)
            if answer.lower() == "y":
                km.remove_kernel_spec(remote_kernels[kernel_name])
def main():
    kernel_name = args.kernel_name
    kernel_env_kwargs = args.env_kwargs
    kernelspecs = kernelspec.find_kernel_specs()
    if not kernelspecs:
        if args.verbose:
            print("No kernelspecs were found: {}".format(kernelspecs))
        exit(0)

    if kernel_name not in kernelspecs:
        print(
            "The target kernel: {} does not exist on this system, the avilable are: {}"
            .format(kernel_name, kernelspecs))
        exit(0)

    kernel_spec = kernelspec.get_kernel_spec(kernel_name)
    # Update the kernel spec env with provided environment
    if kernel_env_kwargs:
        for key, value in kernel_env_kwargs.items():
            # Expand value if environement variables are used
            expanded_value = os.path.expandvars(value)
            kernel_spec.env.update({key: expanded_value})

    if update_kernel(kernel_spec) and args.verbose:
        print("Updated kernel: {}".format(kernel_name))
Exemple #6
0
 def look_in_notebook_specs(self, keyname=None, speclist=None):
     '''
     find keyname in kernelspec
     and return the first we find 
     
     store (specitem,keyname,keyvalue) in self.specitem
     '''
     keyname = keyname or self.keyname
     keyvalue = None
     speclist = speclist or kernelspec.find_kernel_specs().keys()
     speclist = list(speclist)
     # look over list
     try:
         for specitem in speclist:
             try:
                 spec = kernelspec.get_kernel_spec(specitem)
                 env = spec.env
                 if keyname in env:
                     keyvalue = spec[keyname]
                     self.specitem = (specitem, keyname, keyvalue)
                     return keyvalue
             except:
                 pass
     except:
         pass
     return keyvalue
def get_test_kernels():
    test_kernels = [
        k for k, v in kernelspec.find_kernel_specs().items()
        if (k.startswith("ssh_")) and (
            "TEST" in kernelspec.get_kernel_spec(k).display_name)
    ]
    return test_kernels
Exemple #8
0
    def notebook_generator(cells: Optional[List[NotebookNode]] = None) -> NotebookNode:
        """Generate an executable notebook.

        The notebook cells are the one passed as arguments or the hard-coded cells
        if no cells is provided.
        """
        metadata = {}
        for name in kernelspec.find_kernel_specs():
            ks = kernelspec.get_kernel_spec(name)
            metadata = {
                'kernelspec': {
                    'name': name,
                    'language': ks.language,
                    'display_name': ks.display_name,
                }
            }
            break

        if cells is not None:
            all_cells = cells
        else:  # Default cells
            all_cells = [
                new_code_cell('a = 2', metadata={"tags": []}),
                new_code_cell('b=22\nb', metadata={"tags": ["test"]}),
                new_code_cell(
                    "",
                    metadata={"tags": ["dummy-outputs"]},
                    outputs=[new_output('execute_result', data={"text/plain": "text"})],
                ),
            ]

        return new_notebook(metadata=metadata, cells=all_cells)
Exemple #9
0
 def setkernel(self, kernel):
     """Set the kernel for jupyter_client"""
     self.kernel = kernel
     if kernel is not None:
         ks = kernelspec.get_kernel_spec(kernel) 
         self.language = ks.language
         self.kernel_spec = ks.to_dict()
         self.kernel_spec['name'] = kernel
Exemple #10
0
def _build_parameter_code(kernel_name, parameters):
    kernelspec = get_kernel_spec(kernel_name)
    if kernel_name in _parameter_code_builders:
        return _parameter_code_builders[kernel_name](parameters)
    elif kernelspec.language in _parameter_code_builders:
        return _parameter_code_builders[kernelspec.language](parameters)
    raise PapermillException(
        "No parameter builder functions specified for kernel '%s' or language '%s'"
        % (kernel_name, kernelspec.language))
Exemple #11
0
 def kernel_translator(self, kernel_name):
     kernelspec = get_kernel_spec(kernel_name)
     if kernel_name in self._translators:
         return self._translators[kernel_name]
     elif kernelspec.language in self._translators:
         return self._translators[kernelspec.language]
     raise PapermillException(
         "No parameter translator functions specified for kernel '{}' or language '{}'"
         .format(kernel_name, kernelspec.language))
def get_env():
    env = get_kernel_spec("python3").env
    # jupyter-proxy-server will try to format the env strings, which might fail
    # i.e, we have '"EOS_PATH_FORMAT": "/eos/user/{username[0]}/{username}/"', which fails
    # looking for 'username'
    for key in env:
        env[key] = env[key].replace("{", "{{").replace("}", "}}")

    return env
Exemple #13
0
def test_set_kernel_inplace(py_file, tmpdir):
    tmp_py = str(tmpdir.join('notebook.py'))

    copyfile(py_file, tmp_py)

    jupytext([tmp_py, '--set-kernel', '-'])

    nb = read(tmp_py)
    kernel_name = nb.metadata['kernelspec']['name']
    assert get_kernel_spec(kernel_name).argv[0] in ['python', sys.executable]
Exemple #14
0
def kernelspec_from_language(language):
    """Return the python kernel that matches the current env, or the first kernel that matches the given language"""
    try:
        if language == "python":
            # Return the kernel that matches the current Python executable
            for name in find_kernel_specs():
                kernel_specs = get_kernel_spec(name)
                cmd = kernel_specs.argv[0]
                if (
                    kernel_specs.language == "python"
                    and os.path.isfile(cmd)
                    and os.path.samefile(cmd, sys.executable)
                ):
                    return {
                        "name": name,
                        "language": language,
                        "display_name": kernel_specs.display_name,
                    }

            # If none was found, return the first kernel that has 'python' as argv[0]
            for name in find_kernel_specs():
                kernel_specs = get_kernel_spec(name)
                if (
                    kernel_specs.language == "python"
                    and kernel_specs.argv[0] == "python"
                ):
                    return {
                        "name": name,
                        "language": language,
                        "display_name": kernel_specs.display_name,
                    }

        for name in find_kernel_specs():
            kernel_specs = get_kernel_spec(name)
            if same_language(kernel_specs.language, language):
                return {
                    "name": name,
                    "language": language,
                    "display_name": kernel_specs.display_name,
                }
    except (KeyError, ValueError):
        pass
    return None
def kernelspec_from_language(language):
    """Return the kernel specification for the first kernel with a matching language"""
    try:
        for name in find_kernel_specs():
            kernel_specs = get_kernel_spec(name)
            if kernel_specs.language == language or (language == 'c++' and kernel_specs.language.startswith('C++')):
                return {'name': name, 'language': language, 'display_name': kernel_specs.display_name}
    except (KeyError, ValueError):
        pass
    return None
Exemple #16
0
def test_set_kernel_inplace(py_file, tmpdir):
    tmp_py = str(tmpdir.join("notebook.py"))

    copyfile(py_file, tmp_py)

    jupytext([tmp_py, "--set-kernel", "-"])

    nb = read(tmp_py)
    kernel_name = nb.metadata["kernelspec"]["name"]
    cmd = get_kernel_spec(kernel_name).argv[0]
    assert cmd == "python" or os.path.samefile(cmd, sys.executable)
Exemple #17
0
def test_set_kernel_auto(py_file, tmpdir):
    tmp_py = str(tmpdir.join('notebook.py'))
    tmp_ipynb = str(tmpdir.join('notebook.ipynb'))

    copyfile(py_file, tmp_py)

    jupytext(['--to', 'ipynb', tmp_py, '--set-kernel', '-'])

    nb = read(tmp_ipynb)
    kernel_name = nb.metadata['kernelspec']['name']
    cmd = get_kernel_spec(kernel_name).argv[0]
    assert cmd == 'python' or os.path.samefile(cmd, sys.executable)
Exemple #18
0
def blank_nb(kernel_name):
    try:
        spec = get_kernel_spec(kernel_name)
    except NoSuchKernel as e:
        raise ExtensionError('Unable to find kernel', orig_exc=e)
    return nbformat.v4.new_notebook(metadata={
        'kernelspec': {
            'display_name': spec.display_name,
            'language': spec.language,
            'name': kernel_name,
        }
    })
def get_kernel_path(cluster_id, with_spark):
    def cond(k):
        s = k.endswith("spark")
        return s if with_spark else not s

    test_kernels = [
        (k, v) for k, v in kernelspec.find_kernel_specs().items()
        if (k.startswith("ssh_")) and ("TEST" in kernelspec.get_kernel_spec(
            k).display_name) and (cluster_id in k) and cond(k)
    ]
    assert len(test_kernels) == 1
    return test_kernels[0]
Exemple #20
0
 def kernel_names(self) -> Dict[str, list]:
     """Kernel name dictionary for languages. """
     if self._kernel_names:
         return self._kernel_names
     kernel_specs = find_kernel_specs()
     for kernel_name in kernel_specs:
         kernel_spec = get_kernel_spec(kernel_name)
         language = kernel_spec.language
         if language not in self._kernel_names:
             self._kernel_names[language] = [kernel_name]
         else:
             self._kernel_names[language].append(kernel_name)
     return self._kernel_names
Exemple #21
0
def test_set_kernel_auto(py_file, tmpdir):
    tmp_py = str(tmpdir.join('notebook.py'))
    tmp_ipynb = str(tmpdir.join('notebook.ipynb'))

    copyfile(py_file, tmp_py)

    jupytext(['--to', 'ipynb', tmp_py, '--set-kernel', '-'])

    nb = read(tmp_ipynb)
    kernel_name = nb.metadata['kernelspec']['name']
    assert get_kernel_spec(kernel_name).argv[0].lower() in [
        'python', sys.executable.lower()
    ]
Exemple #22
0
def blank_nb(kernel_name):
    try:
        spec = get_kernel_spec(kernel_name)
    except NoSuchKernel as e:
        raise ExtensionError("Unable to find kernel", orig_exc=e)
    return nbformat.v4.new_notebook(
        metadata={
            "kernelspec": {
                "display_name": spec.display_name,
                "language": spec.language,
                "name": kernel_name,
            }
        })
Exemple #23
0
def notebook():
    for name in kernelspec.find_kernel_specs():
        ks = kernelspec.get_kernel_spec(name)
        metadata = {
            'kernelspec': {'name': name, 'language': ks.language, 'display_name': ks.display_name}
        }
        return nbformat.v4.new_notebook(
            metadata=metadata,
            cells=[
                nbformat.v4.new_markdown_cell('This is a notebook with kernel: ' + ks.display_name)
            ],
        )

    raise EnvironmentError('No kernel found')
def kernelspec_from_language(language):
    """Return the python kernel that matches the current env, or the first kernel that matches the given language"""
    try:
        if language == 'python':
            # Return the kernel that matches the current Python executable
            for name in find_kernel_specs():
                kernel_specs = get_kernel_spec(name)
                if kernel_specs.language == 'python' and kernel_specs.argv[
                        0] == sys.executable:
                    return {
                        'name': name,
                        'language': language,
                        'display_name': kernel_specs.display_name
                    }

            # If none was found, return the first kernel that has 'python' as argv[0]
            for name in find_kernel_specs():
                kernel_specs = get_kernel_spec(name)
                if kernel_specs.language == 'python' and kernel_specs.argv[
                        0] == 'python':
                    return {
                        'name': name,
                        'language': language,
                        'display_name': kernel_specs.display_name
                    }

        for name in find_kernel_specs():
            kernel_specs = get_kernel_spec(name)
            if same_language(kernel_specs.language, language):
                return {
                    'name': name,
                    'language': language,
                    'display_name': kernel_specs.display_name
                }
    except (KeyError, ValueError):
        pass
    return None
def git_project(tmp_path):
    git_ref = tmp_path / "ref"
    for name in kernelspec.find_kernel_specs():
        ks = kernelspec.get_kernel_spec(name)
        if ks.language != "python":
            continue
        metadata = {
            "kernelspec": {
                "name": name,
                "language": ks.language,
                "display_name": ks.display_name,
            }
        }
        break

    fake_notebooks = {
        git_ref / "notebook1.ipynb":
        new_notebook(
            cells=[
                new_code_cell("a: int = 2 # Beautiful",
                              metadata={"tags": ["parameters"]}),
                new_code_cell("a"),
            ],
            metadata=metadata,
        ),
        git_ref / "subfolder" / "notebook2.ipynb":
        new_notebook(
            cells=[
                new_code_cell(
                    "b: str = 'The famous B' # Beautiful",
                    metadata={"tags": ["parameters"]},
                ),
                new_markdown_cell("# Surprise"),
                new_code_cell("f'{b}eaver'"),
            ],
            metadata=metadata,
        ),
    }

    for path, nb in fake_notebooks.items():
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text(json.dumps(nb))

    # Initialize the git repository
    check_call(["git", "init", str(git_ref)])
    check_call(["git", "add", "-A"], cwd=str(git_ref))
    check_call(["git", "commit", "-am", '"First commit"'], cwd=str(git_ref))

    yield git_ref
Exemple #26
0
def main(argv):
    if len(argv) != 1:
        print('Usage: {}'.format(argv[0]))
        return 1

    npm_output = subprocess.run(['npm', 'ls', 'socket.io'],
                                cwd='/datalab/web',
                                stdout=subprocess.PIPE)
    socketio_version = npm_output.stdout.strip().decode('utf8').split('@')[-1]

    for name, path in kernelspec.find_kernel_specs().items():
        spec = kernelspec.get_kernel_spec(name)
        colab_metadata = spec.metadata.setdefault('colab', {})
        colab_metadata['socketio_version'] = socketio_version
        with open(os.path.join(path, 'kernel.json'), 'wt') as f:
            json.dump(spec.to_dict(), f, sort_keys=True, indent=1)
Exemple #27
0
 def __init__(self, **kwargs):
     Kernel.__init__(self, **kwargs)
     self.dir = get_kernel_spec('vim_kernel').resource_dir
     self.vim = Popen([
         'vim',
         '-X',
         '-N',
         '-u',
         'NONE',
         '-i',
         'NONE',
         '-e',
         '-s',
         '-S',
         path.join(self.dir, 'kernel.vim')
         ], stdout=PIPE, stderr=STDOUT, shell=False, env=environ.copy())
Exemple #28
0
    def __post_init__(self):
        self.report["total"] = datetime.timedelta(0)
        kernel_spec = get_kernel_spec(self.name)
        self.language = kernel_spec.language
        if self.language == "python":
            self.init_code = (
                "from pheasant.renderers.jupyter.ipython import register_formatters\n"
                "register_formatters()")

        def shutdown():  # pragma: no cover
            if self.manager:
                print(f"Shutting down kernel [{self.name}]...", end="")
                self.manager.shutdown_kernel()
                print("Done.")

        atexit.register(shutdown)
Exemple #29
0
    def _install_spec(self):
        """
        Install the Sage IPython kernel
        
        It is safe to call this method multiple times, only one Sage
        kernel spec is ever installed for any given Sage
        version. However, it resets the IPython kernel spec directory
        so additional resources symlinked there are lost. See
        :meth:`symlink_resources`.

        EXAMPLES::

            sage: from sage.repl.ipython_kernel.install import SageKernelSpec
            sage: spec = SageKernelSpec()
            sage: spec._install_spec()    # not tested
        """
        import json
        temp = tmp_dir()
        kernel_spec = os.path.join(temp, 'kernel.json')
        with open(kernel_spec, 'w') as f:
            json.dump(self.kernel_spec(), f)
        identifier = self.identifier()
        install_kernel_spec(temp, identifier, user=True, replace=True)
        self._spec = get_kernel_spec(identifier)
Exemple #30
0
                                     reason="flake8 not found")
requires_autopep8 = pytest.mark.skipif(not tool_version("autopep8"),
                                       reason="autopep8 not found")
requires_nbconvert = pytest.mark.skipif(not tool_version("jupyter nbconvert"),
                                        reason="nbconvert not found")
requires_sphinx_gallery = pytest.mark.skipif(
    not rst2md, reason="sphinx_gallery is not available")
requires_pandoc = pytest.mark.skipif(
    not is_pandoc_available() or sys.version_info < (3, ),
    reason="pandoc>=2.7.2 is not available",
)
requires_no_pandoc = pytest.mark.skipif(is_pandoc_available(),
                                        reason="Pandoc is installed")
requires_ir_kernel = pytest.mark.skipif(
    not any(
        get_kernel_spec(name).language == "R" for name in find_kernel_specs()),
    reason="irkernel is not installed",
)
requires_myst = pytest.mark.skipif(not is_myst_available(),
                                   reason="myst_parser not found")
requires_no_myst = pytest.mark.skipif(is_myst_available(),
                                      reason="myst is available")
skip_on_windows = pytest.mark.skipif(sys.platform.startswith("win"),
                                     reason="Issue 489")


def list_notebooks(path="ipynb", skip="World"):
    """All notebooks in the directory notebooks/path,
    or in the package itself"""
    if path == "ipynb":
        return (list_notebooks("ipynb_julia", skip=skip) +