def test_two_raw_cells_are_preserved(
        nb=new_notebook(cells=[new_raw_cell("---\nX\n---"),
                               new_raw_cell("Y")])):
    """Test the pattern described at https://github.com/mwouts/jupytext/issues/466"""
    py = jupytext.writes(nb, "py")
    nb2 = jupytext.reads(py, "py")
    compare_notebooks(nb2, nb)
Example #2
0
def test_read_simple_file(script="""# ---
# title: Simple file
# ---

# %% [markdown]
# This is a markdown cell

# %% [md]
# This is also a markdown cell

# %% [raw]
# This is a raw cell

# %%% sub-cell title
# This is a sub-cell

# %%%% sub-sub-cell title
# This is a sub-sub-cell

# %% And now a code cell
1 + 2 + 3 + 4
5
6
# %%magic # this is a commented magic, not a cell

7
"""):
    nb = jupytext.reads(script, 'py:percent')
    compare_notebooks(
        new_notebook(cells=[
            new_raw_cell('---\ntitle: Simple file\n---'),
            new_markdown_cell('This is a markdown cell'),
            new_markdown_cell('This is also a markdown cell',
                              metadata={'region_name': 'md'}),
            new_raw_cell('This is a raw cell'),
            new_code_cell('# This is a sub-cell',
                          metadata={
                              'title': 'sub-cell title',
                              'cell_depth': 1
                          }),
            new_code_cell('# This is a sub-sub-cell',
                          metadata={
                              'title': 'sub-sub-cell title',
                              'cell_depth': 2
                          }),
            new_code_cell('''1 + 2 + 3 + 4
5
6
%%magic # this is a commented magic, not a cell

7''',
                          metadata={'title': 'And now a code cell'})
        ]), nb)

    script2 = jupytext.writes(nb, 'py:percent')
    compare(script2, script)
Example #3
0
def test_read_raw_cell_markdown_version_1_1_with_mimetype(header="""---
jupyter:
  jupytext:
    text_representation:
      extension: .md
      format_name: markdown
      format_version: '1.1'
      jupytext_version: 1.1.0-rc0
  kernelspec:
    display_name: Python 3
    language: python
    name: python3
---
""", markdown_11="""```raw_mimetype="text/restructuredtext"
.. meta::
   :description: Topic: Integrated Development Environments, Difficulty: Easy, Category: Tools
   :keywords: python, introduction, IDE, PyCharm, VSCode, Jupyter, recommendation, tools
```
""", markdown_12="""<!-- #raw raw_mimetype="text/restructuredtext" -->
.. meta::
   :description: Topic: Integrated Development Environments, Difficulty: Easy, Category: Tools
   :keywords: python, introduction, IDE, PyCharm, VSCode, Jupyter, recommendation, tools
<!-- #endraw -->
"""):
    nb = jupytext.reads(header + '\n' + markdown_11, 'md')
    compare(nb.cells[0], new_raw_cell(source=""".. meta::
   :description: Topic: Integrated Development Environments, Difficulty: Easy, Category: Tools
   :keywords: python, introduction, IDE, PyCharm, VSCode, Jupyter, recommendation, tools""",
                                      metadata={'raw_mimetype': 'text/restructuredtext'}))
    md2 = jupytext.writes(nb, 'md')
    assert "format_version: '1.1'" not in md2
    nb.metadata['jupytext']['notebook_metadata_filter'] = '-all'
    md2 = jupytext.writes(nb, 'md')
    compare(md2, markdown_12)
Example #4
0
def test_raise_on_different_cell_type():
    ref = new_notebook(cells=[new_markdown_cell('Cell one'),
                              new_code_cell('Cell two')])
    test = new_notebook(cells=[new_markdown_cell('Cell one'),
                               new_raw_cell('Cell two')])
    with pytest.raises(AssertionError):
        compare_notebooks(ref, test)
Example #5
0
def test_raw_with_metadata(no_jupytext_version_number, text="""# + key="value" active=""
# Raw cell
# # Commented line
""", notebook=new_notebook(cells=[new_raw_cell('Raw cell\n# Commented line', metadata={'key': 'value'})])):
    nb2 = jupytext.reads(text, 'py')
    compare_notebooks(nb2, notebook)
    text2 = jupytext.writes(notebook, 'py')
    compare(text2, text)
Example #6
0
def test_raw_cell_with_metadata(markdown="""<!-- #raw key="value" -->
raw content
<!-- #endraw -->
"""):
    nb = jupytext.reads(markdown, 'md')
    compare(nb.cells[0], new_raw_cell(source='raw content', metadata={'key': 'value'}))
    markdown2 = jupytext.writes(nb, 'md')
    compare(markdown2, markdown)
def test_raw_cell_with_metadata(markdown="""<!-- #raw key="value" -->
raw content
<!-- #endraw -->
""", ):
    nb = jupytext.reads(markdown, "md")
    compare(nb.cells[0],
            new_raw_cell(source="raw content", metadata={"key": "value"}))
    markdown2 = jupytext.writes(nb, "md")
    compare(markdown2, markdown)
def test_raw_cell_with_metadata(markdown="""```key="value"
raw content
```
"""):
    nb = jupytext.reads(markdown, 'md')
    compare(nb.cells[0],
            new_raw_cell(source='raw content', metadata={'key': 'value'}))
    markdown2 = jupytext.writes(nb, 'md')
    compare(markdown, markdown2)
Example #9
0
def get_test_notebook(name):
    """
    Make a test notebook for the given name.
    """
    nb = new_notebook()
    nb.cells.append(new_code_cell("'code_' + '{}'".format(name)))
    nb.cells.append(new_raw_cell("raw_{}".format(name)))
    nb.cells.append(new_markdown_cell('markdown_{}'.format(name)))
    return nb
Example #10
0
def test_raise_on_different_cell_type():
    ref = new_notebook(
        cells=[new_markdown_cell('Cell one'),
               new_code_cell('Cell two')])
    test = new_notebook(
        cells=[new_markdown_cell('Cell one'),
               new_raw_cell('Cell two')])
    with pytest.raises(NotebookDifference):
        compare_notebooks(ref, test, ext='.md')
Example #11
0
def test_notebook(name):
    """
    Make a test notebook for the given name.
    """
    nb = new_notebook()
    nb.cells.append(new_code_cell("'code_' + '{}'".format(name)))
    nb.cells.append(new_raw_cell("raw_{}".format(name)))
    nb.cells.append(new_markdown_cell("markdown_{}".format(name)))
    return nb
Example #12
0
def test_inactive_cell(text='''```python active="md"
# This becomes a raw cell in Jupyter
```
''', expected=new_notebook(
    cells=[new_raw_cell('# This becomes a raw cell in Jupyter',
                        metadata={'active': 'md'})])):
    nb = jupytext.reads(text, 'md')
    compare_notebooks(nb, expected)
    text2 = jupytext.writes(nb, 'md')
    compare(text2, text)
Example #13
0
def test_inactive_cell_using_tag(text='''```python tags=["active-md"]
# This becomes a raw cell in Jupyter
```
''', expected=new_notebook(
    cells=[new_raw_cell('# This becomes a raw cell in Jupyter',
                        metadata={'tags': ['active-md']})])):
    nb = jupytext.reads(text, 'md')
    compare_notebooks(nb, expected)
    text2 = jupytext.writes(nb, 'md')
    compare(text2, text)
Example #14
0
def test_raise_on_different_cell_type(raise_on_first_difference):
    ref = new_notebook(
        cells=[new_markdown_cell("Cell one"),
               new_code_cell("Cell two")])
    test = new_notebook(
        cells=[new_markdown_cell("Cell one"),
               new_raw_cell("Cell two")])
    with pytest.raises(NotebookDifference):
        compare_notebooks(test,
                          ref,
                          "md",
                          raise_on_first_difference=raise_on_first_difference)
def test_raw_with_metadata_2(
    no_jupytext_version_number,
    text="""# + [raw] key="value"
# Raw cell
# # Commented line
""",
    notebook=new_notebook(cells=[
        new_raw_cell("Raw cell\n# Commented line", metadata={"key": "value"})
    ]),
):
    nb2 = jupytext.reads(text, "py")
    compare_notebooks(nb2, notebook)
Example #16
0
def test_metadata_and_cell_to_header():
    nb = new_notebook(metadata=dict(mainlanguage='python'),
                      cells=[
                          new_raw_cell(source="---\ntitle: Sample header\n---",
                                       metadata={'noskipline': True})
                      ])
    header = writer().metadata_and_cell_to_header(nb)
    assert '\n'.join(header) == """---
title: Sample header
jupyter:
  mainlanguage: python
---"""
    assert nb.cells == []
Example #17
0
def test_active_tag(
        text='''# + tags=["active-py"]
interpreter = 'python'

# + tags=["active-ipynb"]
# interpreter = 'ipython'
''', ref=new_notebook(cells=[
            new_raw_cell("interpreter = 'python'", metadata={'tags': ['active-py']}),
            new_code_cell("interpreter = 'ipython'", metadata={'tags': ['active-ipynb']})])):
    nb = jupytext.reads(text, 'py')
    compare_notebooks(nb, ref)
    py = jupytext.writes(nb, 'py')
    compare(py, text)
def test_root_level_metadata_as_raw_cell(
    tmpdir,
    root_level_metadata_as_raw_cell,
    rmd="""---
author: R Markdown document author
title: R Markdown notebook title
---

```{r}
1 + 1
```
""",
):
    nb_file = tmpdir.join("notebook.ipynb")
    rmd_file = tmpdir.join("notebook.Rmd")
    cfg_file = tmpdir.join("jupytext.toml")

    cfg_file.write(
        "root_level_metadata_as_raw_cell = {}".format(
            "true" if root_level_metadata_as_raw_cell else "false"
        )
    )
    rmd_file.write(rmd)

    jupytext_cli([str(rmd_file), "--to", "ipynb"])

    nb = nbformat.read(str(nb_file), as_version=4)

    if root_level_metadata_as_raw_cell:
        assert len(nb.cells) == 2
        compare(
            nb.cells[0],
            new_raw_cell(
                """---
author: R Markdown document author
title: R Markdown notebook title
---"""
            ),
        )
        compare(nb.cells[1], new_code_cell("1 + 1"))
    else:
        assert len(nb.cells) == 1
        compare(nb.cells[0], new_code_cell("1 + 1"))
        assert nb.metadata["jupytext"]["root_level_metadata"] == {
            "title": "R Markdown notebook title",
            "author": "R Markdown document author",
        }

    # Writing back to Rmd should preserve the original document
    jupytext_cli([str(nb_file), "--to", "Rmd"])
    compare(rmd_file.read(), rmd)
def test_raw_cell_with_metadata_json(
    markdown="""<!-- #raw {"key": "value"} -->
raw content
<!-- #endraw -->
""",
):
    nb = jupytext.reads(markdown, "md")
    compare_cells(
        nb.cells,
        [new_raw_cell(source="raw content", metadata={"key": "value"})],
        compare_ids=False,
    )
    markdown2 = jupytext.writes(nb, "md")
    compare(markdown2, markdown)
Example #20
0
def test_metadata_and_cell_to_header(no_jupytext_version_number):
    metadata = {'jupytext': {'mainlanguage': 'python'}}
    nb = new_notebook(
        metadata=metadata,
        cells=[new_raw_cell(source="---\ntitle: Sample header\n---")])
    header, lines_to_next_cell = metadata_and_cell_to_header(nb, metadata, get_format_implementation('.md'), '.md')
    assert '\n'.join(header) == """---
title: Sample header
jupyter:
  jupytext:
    mainlanguage: python
---"""
    assert nb.cells == []
    assert lines_to_next_cell is None
def test_inactive_cell_using_tag(
    text="""```python tags=["active-md"]
# This becomes a raw cell in Jupyter
```
""",
    expected=new_notebook(cells=[
        new_raw_cell("# This becomes a raw cell in Jupyter",
                     metadata={"tags": ["active-md"]})
    ]),
):
    nb = jupytext.reads(text, "md")
    compare_notebooks(nb, expected)
    text2 = jupytext.writes(nb, "md")
    compare(text2, text)
def test_inactive_cell(
    text="""```python active="md"
# This becomes a raw cell in Jupyter
```
""",
    expected=new_notebook(cells=[
        new_raw_cell("# This becomes a raw cell in Jupyter",
                     metadata={"active": "md"})
    ]),
):
    nb = jupytext.reads(text, "md")
    compare_notebooks(nb, expected)
    text2 = jupytext.writes(nb, "md")
    compare(text2, text)
Example #23
0
def test_metadata_and_cell_to_header():
    metadata = {'jupytext': {'mainlanguage': 'python'}}
    nb = new_notebook(
        metadata=metadata,
        cells=[new_raw_cell(source="---\ntitle: Sample header\n---")])
    with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', False):
        header, lines_to_next_cell = metadata_and_cell_to_header(nb, metadata, get_format_implementation('.md'), '.md')
    assert '\n'.join(header) == """---
title: Sample header
jupyter:
  jupytext:
    mainlanguage: python
---"""
    assert nb.cells == []
    assert lines_to_next_cell is None
Example #24
0
def test_metadata_and_cell_to_header(no_jupytext_version_number):
    metadata = {"jupytext": {"mainlanguage": "python"}}
    nb = new_notebook(
        metadata=metadata,
        cells=[new_raw_cell(source="---\ntitle: Sample header\n---")])
    header, lines_to_next_cell = metadata_and_cell_to_header(
        nb, metadata, get_format_implementation(".md"), ".md")
    assert ("\n".join(header) == """---
title: Sample header
jupyter:
  jupytext:
    mainlanguage: python
---""")
    assert nb.cells == []
    assert lines_to_next_cell is None
Example #25
0
def test_invalid_raw_cell():
    cell = new_raw_cell()

    cell["source"] = 5
    with pytest.raises(ValidationError):
        validate4(cell, "raw_cell")

    cell = new_raw_cell()
    del cell["metadata"]

    with pytest.raises(ValidationError):
        validate4(cell, "raw_cell")

    cell = new_raw_cell()
    del cell["source"]

    with pytest.raises(ValidationError):
        validate4(cell, "raw_cell")

    cell = new_raw_cell()
    del cell["cell_type"]

    with pytest.raises(ValidationError):
        validate4(cell, "raw_cell")
Example #26
0
def test_read_mostly_py_rmd_file(rmd="""---
title: Simple file
---

```{python, echo=TRUE}
import numpy as np
x = np.arange(0, 2*math.pi, eps)
```

```{python, echo=TRUE}
x = np.arange(0,1,eps)
y = np.abs(x)-.5
```

```{r}
ls()
```

```{r, results="asis", magic_args="-i x"}
cat(stringi::stri_rand_lipsum(3), sep='\n\n')
```
""", ):
    nb = jupytext.reads(rmd, "Rmd")
    compare_cells(
        nb.cells,
        [
            new_raw_cell("---\ntitle: Simple file\n---"),
            new_code_cell(
                "import numpy as np\n"
                "x = np.arange(0, 2*math.pi, eps)",
                metadata={"echo": True},
            ),
            new_code_cell("x = np.arange(0,1,eps)\ny = np.abs(x)-.5",
                          metadata={"echo": True}),
            new_code_cell("%%R\nls()"),
            new_code_cell(
                "%%R -i x\ncat(stringi::"
                "stri_rand_lipsum(3), sep='\n\n')",
                metadata={"results": "asis"},
            ),
        ],
        compare_ids=False,
    )

    rmd2 = jupytext.writes(nb, "Rmd")
    rmd2 = re.sub(r"```{r ", "```{r, ", rmd2)
    rmd2 = re.sub(r"```{python ", "```{python, ", rmd2)
    compare(rmd2, rmd)
Example #27
0
def test_metadata_and_cell_to_header():
    nb = new_notebook(metadata={'jupytext': {
        'mainlanguage': 'python'
    }},
                      cells=[
                          new_raw_cell(source="---\ntitle: Sample header\n---",
                                       metadata={'noskipline': True})
                      ])
    header = metadata_and_cell_to_header(nb, get_format('.md'), '.md')
    assert '\n'.join(header) == """---
title: Sample header
jupyter:
  jupytext:
    mainlanguage: python
---"""
    assert nb.cells == []
def test_active_tag(
    text="""# + tags=["active-py"]
interpreter = 'python'

# + tags=["active-ipynb"]
# interpreter = 'ipython'
""",
    ref=new_notebook(cells=[
        new_raw_cell("interpreter = 'python'",
                     metadata={"tags": ["active-py"]}),
        new_code_cell("interpreter = 'ipython'",
                      metadata={"tags": ["active-ipynb"]}),
    ]),
):
    nb = jupytext.reads(text, "py")
    compare_notebooks(nb, ref)
    py = jupytext.writes(nb, "py")
    compare(py, text)
Example #29
0
def test_read_raw_cell_markdown_version_1_1(markdown="""---
jupyter:
  jupytext:
    text_representation:
      extension: .md
      format_name: markdown
      format_version: '1.1'
      jupytext_version: 1.1.0
---

```key="value"
raw content
```
"""):
    nb = jupytext.reads(markdown, 'md')
    compare(nb.cells[0], new_raw_cell(source='raw content', metadata={'key': 'value'}))
    md2 = jupytext.writes(nb, 'md')
    assert "format_version: '1.1'" not in md2
Example #30
0
def translatenb_v4(cells):
    from nbformat.v4 import (new_code_cell, new_markdown_cell, new_notebook)
    from nbformat.v4.nbbase import new_raw_cell

    nb_cells = []
    for cell_type, language, block in cells:
        block = '\n'.join(block)

        if cell_type == 'markdown':
            if block != "":
                nb_cells.append(new_markdown_cell(source=block))
        elif cell_type == 'code':
            nb_cells.append(new_code_cell(source=block))
        elif cell_type == 'raw':
            nb_cells.append(new_raw_cell(source=block))
        else:
            raise ValueError(
                'Wrong cell_type was given [{}]'.format(cell_type))

    nb = new_notebook(cells=nb_cells)
    from nbformat import writes
    return writes(nb, version=4)
Example #31
0
def create_cells(rdfgraph, cell, cell_index, **kwargs):
    provenance_metadata = create_provenance(rdfgraph, cell, cell_index)
    cell_type = str(kwargs['cell_type'])
    source = get_cell_source(rdfgraph, cell_index)
    if source:
        kwargs['source'] = source
    kwargs['metadata'] = provenance_metadata
    if cell_type == 'code':
        output = get_cell_output(rdfgraph, cell_index)
        if output:
            output = create_cell_output(**output)
        if output:
            kwargs['outputs'] = [output]
        else:
            kwargs['outputs'] = []
        notebook_cell = nbbase.new_code_cell(**kwargs)
    elif cell_type == 'markdown':
        notebook_cell = nbbase.new_markdown_cell(**kwargs)
    elif cell_type == 'raw':
        notebook_cell = nbbase.new_raw_cell(**kwargs)

    return notebook_cell
Example #32
0
      offset = -1
    else:
      cells_new.append(new_code_cell(
      source=cell.source,
      metadata=cell.metadata,
      outputs=[],
      # print cell.execution_count
      execution_count=cell.execution_count+offset))
      # print cell.execution_count+offset
  elif cell["cell_type"] == "markdown":
    cells_new.append(new_markdown_cell(
    source=cell.source,
    metadata=cell.metadata))
  else:
    cells_new.append(new_raw_cell(
    source=cell.source,
    metadata=cell.metadata))

  # print cell.cell_type

nb_new = new_notebook(cells=cells_new,
    metadata=nb.metadata,
    nbformat=nb.nbformat,
    nbformat_minor=nb.nbformat_minor
)

with codecs.open(outfilename, encoding='utf-8', mode='w') as out:
    nbformat.write(nb_new, out)

# nbformat.write(nb, fp, version=nbformat.NO_CONVERT, **kwargs)
#################################################################