Example #1
0
def test_python_kernel_preserves_R_files(nb_file, tmpdir):
    """Opening a R file with a Jupyter server that has no R kernel should not modify the file"""
    tmp_r_file = str(tmpdir.join('script.R'))
    with open(nb_file) as fp:
        script = fp.read()
    with open(tmp_r_file, 'w') as fp:
        fp.write(script)

    # create contents manager
    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)

    # open notebook, set Python kernel and save
    model = cm.get('script.R')
    model['content'].metadata['kernelspec'] = kernelspec_from_language(
        'python')
    cm.save(model=model, path='script.R')

    with open(tmp_r_file) as fp:
        script2 = fp.read()

    compare(script, script2)
Example #2
0
    def read(cls, path):
        resolved_path, fmt = cls.extract_format(path)
        text = _resolved_handler(resolved_path).read(resolved_path)

        # Read the document
        nb = jupytext.reads(text, fmt=fmt)

        # Set a kernel if there was none
        if nb.metadata.get('kernelspec', {}).get('name') is None:
            language = nb.metadata.get('jupytext', {}).get(
                'main_language') or nb.metadata['kernelspec']['language']
            if not language:
                raise ValueError(
                    'Cannot infer a kernel as the document language is not defined'
                )

            kernelspec = kernelspec_from_language(language)
            if not kernelspec:
                raise ValueError('Found no kernel for {}'.format(language))

            nb.metadata['kernelspec'] = kernelspec

        # Return the notebook as a JSON string
        return nbformat.writes(nb)
Example #3
0
                                    reason="isort not found")
requires_flake8 = pytest.mark.skipif(not tool_version("flake8"),
                                     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(kernelspec_from_language("R") is None,
                                        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) +
                list_notebooks("ipynb_py", skip=skip) +