def test_alternate_tree_four_five_backticks(
    no_jupytext_version_number,
    nb=new_notebook(
        metadata={"main_language": "python"},
        cells=[
            new_code_cell('a = """\n```\n"""'),
            new_code_cell("b = 2"),
            new_code_cell('c = """\n````\n"""'),
        ],
    ),
    text='''---
jupyter:
  jupytext:
    main_language: python
---

````python
a = """
```
"""
````

```python
b = 2
```

`````python
c = """
````
"""
`````
''',
):
    actual_text = writes(nb, fmt="md")
    compare(actual_text, text)

    actual_nb = reads(text, fmt="md")
    compare_notebooks(actual_nb, nb)
Exemple #2
0
def test_read_nbconvert_script(script="""
# coding: utf-8

# A markdown cell

# In[1]:


import pandas as pd

pd.options.display.max_rows = 6
pd.options.display.max_columns = 20


# Another markdown cell

# In[2]:


1 + 1


# Again, a markdown cell

# In[33]:


2 + 2


# <codecell>


3 + 3
"""):
    assert jupytext.formats.guess_format(script, '.py') == 'percent'
    nb = jupytext.reads(script, '.py')
    assert len(nb.cells) == 5
def test_read_mostly_R_markdown_file(
    markdown="""```R
ls()
```

```R
cat(stringi::stri_rand_lipsum(3), sep='\n\n')
```
""",
):
    nb = jupytext.reads(markdown, "md")
    assert nb.metadata["jupytext"]["main_language"] == "R"
    compare_cells(
        nb.cells,
        [
            new_code_cell("ls()"),
            new_code_cell("cat(stringi::stri_rand_lipsum(3), sep='\n\n')"),
        ],
        compare_ids=False,
    )

    markdown2 = jupytext.writes(nb, "md")
    compare(markdown2, markdown)
def test_multiline_python_magic(no_jupytext_version_number):
    nb = new_notebook(cells=[
        new_code_cell("""%load_ext watermark
%watermark -u -n -t -z \\
    -p jupytext -v

def g(x):
    return x+1""")
    ])

    text = jupytext.writes(nb, "py")
    compare(
        text,
        """# +
# %load_ext watermark
# %watermark -u -n -t -z \\
#     -p jupytext -v

def g(x):
    return x+1
""",
    )
    compare_notebooks(jupytext.reads(text, "py"), nb)
Exemple #5
0
def test_indented_comment(text="""def f():
    return 1

    # f returns 1


def g():
    return 2


# h returns 3
def h():
    return 3
""", ref=new_notebook(cells=[new_code_cell("""def f():
    return 1

    # f returns 1"""),
                             new_code_cell('def g():\n    return 2'),
                             new_code_cell('# h returns 3\ndef h():\n    return 3')])):
    nb = jupytext.reads(text, 'py')
    compare_notebooks(nb, ref)
    py = jupytext.writes(nb, 'py')
    compare(py, text)
def test_escape_start_pattern(pynb="""# The code start pattern '# +' can
# appear in code and markdown cells.

# In markdown cells it is escaped like here:
# # + {"sample_metadata": "value"}

# In code cells like this one, it is also escaped
# # + {"sample_metadata": "value"}
1 + 1
"""):
    nb = jupytext.reads(pynb, ext='.py')
    assert len(nb.cells) == 3
    assert nb.cells[0].cell_type == 'markdown'
    assert nb.cells[1].cell_type == 'markdown'
    assert nb.cells[2].cell_type == 'code'
    assert nb.cells[1].source == '''In markdown cells it is escaped like here:
# + {"sample_metadata": "value"}'''
    assert (nb.cells[2].source ==
            '''# In code cells like this one, it is also escaped
# + {"sample_metadata": "value"}
1 + 1''')
    pynb2 = jupytext.writes(nb, ext='.py')
    compare(pynb, pynb2)
Exemple #7
0
def test_read_mostly_R_markdown_file(markdown="""```R
ls()
```

```R
cat(stringi::stri_rand_lipsum(3), sep='\n\n')
```
"""):
    nb = jupytext.reads(markdown, 'md')
    assert nb.metadata['jupytext']['main_language'] == 'R'
    compare(nb.cells, [{'cell_type': 'code',
                        'metadata': {},
                        'execution_count': None,
                        'source': 'ls()',
                        'outputs': []},
                       {'cell_type': 'code',
                        'metadata': {},
                        'execution_count': None,
                        'source': "cat(stringi::stri_rand_lipsum(3), sep='\n\n')",
                        'outputs': []}])

    markdown2 = jupytext.writes(nb, 'md')
    compare(markdown2, markdown)
def test_one_blank_lines_after_endofcell(pynb="""# +
# This is a code cell with explicit end of cell
1 + 1

2 + 2
# -

# This cell is a cell with implicit start
1 + 1
"""):
    nb = jupytext.reads(pynb, ext='.py')
    assert len(nb.cells) == 2
    assert nb.cells[0].cell_type == 'code'
    assert (nb.cells[0].source ==
            '''# This is a code cell with explicit end of cell
1 + 1

2 + 2''')
    assert nb.cells[1].cell_type == 'code'
    assert nb.cells[1].source == '''# This cell is a cell with implicit start
1 + 1'''
    pynb2 = jupytext.writes(nb, ext='.py')
    compare(pynb, pynb2)
Exemple #9
0
def test_magics_are_commented(fmt):
    nb = new_notebook(
        cells=[new_code_cell("%pylab inline")],
        metadata={
            "jupytext": {
                "comment_magics": True,
                "main_language": "R"
                if fmt == "R"
                else "scheme"
                if fmt.startswith("ss")
                else "python",
            }
        },
    )

    text = jupytext.writes(nb, fmt)
    assert "%pylab inline" not in text.splitlines()
    nb2 = jupytext.reads(text, fmt)

    if "sphinx" in fmt:
        nb2.cells = nb2.cells[1:]

    compare_notebooks(nb2, nb)
def test_one_blank_lines_after_endofcell(pynb="""# +
# This is a code cell with explicit end of cell
1 + 1

2 + 2
# -

# This cell is a cell with implicit start
1 + 1
""", ):
    nb = jupytext.reads(pynb, "py")
    assert len(nb.cells) == 2
    assert nb.cells[0].cell_type == "code"
    assert (nb.cells[0].source ==
            """# This is a code cell with explicit end of cell
1 + 1

2 + 2""")
    assert nb.cells[1].cell_type == "code"
    assert (nb.cells[1].source == """# This cell is a cell with implicit start
1 + 1""")
    pynb2 = jupytext.writes(nb, "py")
    compare(pynb2, pynb)
def test_escape_start_pattern(pynb="""# The code start pattern '# +' can
# appear in code and markdown cells.

# In markdown cells it is escaped like here:
# # + {"sample_metadata": "value"}

# In code cells like this one, it is also escaped
# # + {"sample_metadata": "value"}
1 + 1
""", ):
    nb = jupytext.reads(pynb, "py")
    assert len(nb.cells) == 3
    assert nb.cells[0].cell_type == "markdown"
    assert nb.cells[1].cell_type == "markdown"
    assert nb.cells[2].cell_type == "code"
    assert (nb.cells[1].source == """In markdown cells it is escaped like here:
# + {"sample_metadata": "value"}""")
    assert (nb.cells[2].source ==
            """# In code cells like this one, it is also escaped
# + {"sample_metadata": "value"}
1 + 1""")
    pynb2 = jupytext.writes(nb, "py")
    compare(pynb2, pynb)
Exemple #12
0
def test_escape_start_pattern(ext, rnb="""#' The code start pattern '#+' can
#' appear in code and markdown cells.

#' In markdown cells it is escaped like here:
#' #+ fig.width=12

# In code cells like this one, it is also escaped
# #+ cell_name language="python"
1 + 1
"""):
    nb = jupytext.reads(rnb, ext)
    assert len(nb.cells) == 3
    assert nb.cells[0].cell_type == 'markdown'
    assert nb.cells[1].cell_type == 'markdown'
    assert nb.cells[2].cell_type == 'code'
    assert nb.cells[1].source == '''In markdown cells it is escaped like here:
#+ fig.width=12'''
    assert (nb.cells[2].source ==
            '''# In code cells like this one, it is also escaped
#+ cell_name language="python"
1 + 1''')
    rnb2 = jupytext.writes(nb, ext)
    compare(rnb, rnb2)
Exemple #13
0
def test_read_simple_file(text="""println!("Hello world");
eprintln!("Hello error");
format!("Hello {}", "world")

// A Function
pub fn fib(x: i32) -> i32 {
    if x <= 2 {0} else {fib(x - 2) + fib(x - 1)}
}

// This is a
// Markdown cell

// This is a magic instruction
// :vars

// This is a rust identifier
::std::mem::drop
""", ):
    nb = jupytext.reads(text, "rs")
    compare_notebooks(
        nb,
        new_notebook(cells=[
            new_code_cell("""println!("Hello world");
eprintln!("Hello error");
format!("Hello {}", "world")"""),
            new_code_cell("""// A Function
pub fn fib(x: i32) -> i32 {
    if x <= 2 {0} else {fib(x - 2) + fib(x - 1)}
}"""),
            new_markdown_cell("This is a\nMarkdown cell"),
            new_code_cell("""// This is a magic instruction
:vars"""),
            new_code_cell("""// This is a rust identifier
::std::mem::drop"""),
        ]),
    )
    compare(jupytext.writes(nb, "rs"), text)
def test_mark_cell_with_no_title_and_inner_region(
    script="""# This is a markdown cell

# region {"key": "value"}
a = 1

# region An inner region
b = 2
# endregion

def f(x):
    return x + 1


# endregion


d = 4
""", ):
    nb = jupytext.reads(script, "py")
    assert nb.cells[0].cell_type == "markdown"
    assert nb.cells[0].source == "This is a markdown cell"
    assert nb.cells[1].cell_type == "code"
    assert nb.cells[1].source == '# region {"key": "value"}\na = 1'
    assert nb.cells[2].cell_type == "code"
    assert nb.cells[2].metadata["title"] == "An inner region"
    assert nb.cells[2].source == "b = 2"
    assert nb.cells[3].cell_type == "code"
    assert nb.cells[3].source == "def f(x):\n    return x + 1"
    assert nb.cells[4].cell_type == "code"
    assert nb.cells[4].source == "# endregion"
    assert nb.cells[5].cell_type == "code"
    assert nb.cells[5].source == "d = 4"
    assert len(nb.cells) == 6

    script2 = jupytext.writes(nb, "py")
    compare(script2, script)
Exemple #15
0
def test_read_md_and_markdown_regions(markdown="""Some text

<!-- #md -->
A


long
cell
<!-- #endmd -->

<!-- #markdown -->
Another


long
cell
<!-- #endmarkdown -->
"""):
    nb = jupytext.reads(markdown, 'md')
    assert nb.metadata['jupytext']['main_language'] == 'python'
    compare(nb.cells, [
        new_markdown_cell('Some text'),
        new_markdown_cell("""A


long
cell""", metadata={'region_name': 'md'}),
        new_markdown_cell("""Another


long
cell""",
                          metadata={'region_name': 'markdown'})
    ])

    markdown2 = jupytext.writes(nb, 'md')
    compare(markdown, markdown2)
def test_mark_cell_with_no_title_and_inner_region(
    script="""# This is a markdown cell

# region {"key": "value"}
a = 1

# region An inner region
b = 2
# endregion

def f(x):
    return x + 1


# endregion


d = 4
"""):
    nb = jupytext.reads(script, 'py')
    assert nb.cells[0].cell_type == 'markdown'
    assert nb.cells[0].source == 'This is a markdown cell'
    assert nb.cells[1].cell_type == 'code'
    assert nb.cells[1].source == '# region {"key": "value"}\na = 1'
    assert nb.cells[2].cell_type == 'code'
    assert nb.cells[2].metadata['title'] == 'An inner region'
    assert nb.cells[2].source == 'b = 2'
    assert nb.cells[3].cell_type == 'code'
    assert nb.cells[3].source == 'def f(x):\n    return x + 1'
    assert nb.cells[4].cell_type == 'code'
    assert nb.cells[4].source == '# endregion'
    assert nb.cells[5].cell_type == 'code'
    assert nb.cells[5].source == 'd = 4'
    assert len(nb.cells) == 6

    script2 = jupytext.writes(nb, 'py')
    compare(script, script2)
def test_read_less_simple_file(pynb="""# ---
# title: Less simple file
# ---

# Here we have some text
# And below we have some python code

# This is a comment about function f
def f(x):
    return x+1


# And a comment on h
def h(y):
    return y-1
""", ):
    nb = jupytext.reads(pynb, "py")

    assert len(nb.cells) == 4
    assert nb.cells[0].cell_type == "raw"
    assert nb.cells[0].source == "---\ntitle: Less simple file\n---"
    assert nb.cells[1].cell_type == "markdown"
    assert (nb.cells[1].source == "Here we have some text\n"
            "And below we have some python code")
    assert nb.cells[2].cell_type == "code"
    compare(
        nb.cells[2].source,
        "# This is a comment about function f\n"
        "def f(x):\n"
        "    return x+1",
    )
    assert nb.cells[3].cell_type == "code"
    compare(nb.cells[3].source,
            """# And a comment on h\ndef h(y):\n    return y-1""")

    pynb2 = jupytext.writes(nb, "py")
    compare(pynb2, pynb)
Exemple #18
0
def remove_solution(input_py_str):
    """Removes solution from python scripts content str.

    This is based on:
    - cells having "solution" in their metadata tags when removing full cells
    - a specific comment matching "# solution" that will keep only the content
      before this comment and add "# Write your code here." at the end of the
      cell.
    """
    nb = jupytext.reads(input_py_str, fmt='py:percent')

    cell_tags_list = [c['metadata'].get('tags') for c in nb.cells]
    is_solution_list = [
        tags is not None and 'solution' in tags for tags in cell_tags_list
    ]
    # Completely remove cells with "solution" tags
    nb.cells = [
        cell for cell, is_solution in zip(nb.cells, is_solution_list)
        if not is_solution
    ]

    # Partial cell removal based on "# solution" comment
    marker = "# solution"
    pattern = re.compile(f"^{marker}.*", flags=re.MULTILINE | re.DOTALL)

    cells_to_modify = [
        c for c in nb.cells
        if c["cell_type"] == "code" and marker in c["source"]
    ]

    for c in cells_to_modify:
        c["source"] = pattern.sub("# Write your code here.", c["source"])

    # TODO: we could potentially try to avoid changing the input file jupytext
    # header since this info is rarely useful. Let's keep it simple for now.
    py_nb_str = jupytext.writes(nb, fmt='py:percent')
    return py_nb_str
def test_read_markdown_IDL(
    no_jupytext_version_number,
    text="""---
jupyter:
  kernelspec:
    display_name: IDL [conda env:gdl] *
    language: IDL
    name: conda-env-gdl-idl
---

# A sample IDL Markdown notebook

```IDL
a = 1
```
""",
):
    nb = jupytext.reads(text, "md")
    assert len(nb.cells) == 2
    assert nb.cells[1].cell_type == "code"
    assert nb.cells[1].source == "a = 1"

    text2 = jupytext.writes(nb, "md")
    compare(text2, text)
def test_read_simple_file(
    script="""// ---
// title: Simple file
// ---

// Here we have some text
// And below we have some code

System.out.println("Hello World");
""",
):
    nb = jupytext.reads(script, "java")
    assert len(nb.cells) == 3
    assert nb.cells[0].cell_type == "raw"
    assert nb.cells[0].source == "---\ntitle: Simple file\n---"
    assert nb.cells[1].cell_type == "markdown"
    assert (
        nb.cells[1].source == "Here we have some text\n" "And below we have some code"
    )
    assert nb.cells[2].cell_type == "code"
    assert nb.cells[2].source == """System.out.println("Hello World");"""

    script2 = jupytext.writes(nb, "java")
    compare(script2, script)
def test_ipython_help_are_commented_297(
    text="""# This is a markdown cell
# that ends with a question: float?

# The next cell is also a markdown cell,
# because it has no code marker:

# float?

# +
# float?

# +
# float??

# +
# Finally a question in a code
# # cell?
""",
    nb=new_notebook(cells=[
        new_markdown_cell(
            "This is a markdown cell\nthat ends with a question: float?"),
        new_markdown_cell(
            "The next cell is also a markdown cell,\nbecause it has no code marker:"
        ),
        new_markdown_cell("float?"),
        new_code_cell("float?"),
        new_code_cell("float??"),
        new_code_cell("# Finally a question in a code\n# cell?"),
    ]),
):
    nb2 = jupytext.reads(text, "py")
    compare_notebooks(nb2, nb)

    text2 = jupytext.writes(nb2, "py")
    compare(text2, text)
def test_mark_cell_with_vim_folding_markers(script="""# This is a markdown cell

# {{{ And this is a foldable code region with metadata {"key": "value"}
a = 1

b = 2

c = 3
# }}}
""", ):
    nb = jupytext.reads(script, "py")
    assert nb.metadata["jupytext"]["cell_markers"] == "{{{,}}}"
    assert len(nb.cells) == 2
    assert nb.cells[0].cell_type == "markdown"
    assert nb.cells[0].source == "This is a markdown cell"
    assert nb.cells[1].cell_type == "code"
    assert nb.cells[1].source == "a = 1\n\nb = 2\n\nc = 3"
    assert nb.cells[1].metadata == {
        "title": "And this is a foldable code region with metadata",
        "key": "value",
    }

    script2 = jupytext.writes(nb, "py")
    compare(script2, script)
Exemple #23
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)
def test_read_simple_file(pynb="""# ---
# title: Simple file
# ---

# Here we have some text
# And below we have some python code

def f(x):
    return x+1


def h(y):
    return y-1
""", ):
    nb = jupytext.reads(pynb, "py")
    assert len(nb.cells) == 4
    assert nb.cells[0].cell_type == "raw"
    assert nb.cells[0].source == "---\ntitle: Simple file\n---"
    assert nb.cells[1].cell_type == "markdown"
    assert (nb.cells[1].source == "Here we have some text\n"
            "And below we have some python code")
    assert nb.cells[2].cell_type == "code"
    compare(
        nb.cells[2].source,
        """def f(x):
    return x+1""",
    )
    assert nb.cells[3].cell_type == "code"
    compare(
        nb.cells[3].source,
        """def h(y):
    return y-1""",
    )

    pynb2 = jupytext.writes(nb, "py")
    compare(pynb2, pynb)
Exemple #25
0
def test_fix_139():
    text = """# ---
# jupyter:
#   jupytext:
#     metadata_filter:
#       cells:
#         additional:
#           - "lines_to_next_cell"
#         excluded:
#           - "all"
# ---

# + {"lines_to_next_cell": 2}
1 + 1
# -


1 + 1
"""

    nb = jupytext.reads(text, 'py:light')
    text2 = jupytext.writes(nb, 'py:light')
    assert 'cell_metadata_filter: -all' in text2
    assert 'lines_to_next_cell' not in text2
Exemple #26
0
def test_docstring_with_quadruple_quote(
    nb=new_notebook(
        cells=[
            new_code_cell(
                '''def fun_1(df):
  """"
  docstring starting with 4 double quotes and ending with 3
  """
  return df'''
            ),
            new_code_cell(
                '''def fun_2(df):
  """
  docstring
  """
  return df'''
            ),
        ]
    )
):
    """Reproduces https://github.com/mwouts/jupytext/issues/460"""
    py = jupytext.writes(nb, "py:percent")
    nb2 = jupytext.reads(py, "py")
    compare_notebooks(nb2, nb)
Exemple #27
0
def test_notebook_with_python3_magic(
    no_jupytext_version_number,
    nb=new_notebook(metadata={
        'kernelspec': {
            'display_name': 'Python 3',
            'language': 'python',
            'name': 'python3'
        }
    },
                    cells=[
                        new_code_cell('%%python2\na = 1\nprint a'),
                        new_code_cell('%%python3\nb = 2\nprint(b)')
                    ]),
    text="""---
jupyter:
  kernelspec:
    display_name: Python 3
    language: python
    name: python3
---

```python2
a = 1
print a
```

```python3
b = 2
print(b)
```
"""):
    md = jupytext.writes(nb, 'md')
    compare(md, text)

    nb2 = jupytext.reads(md, 'md')
    compare_notebooks(nb2, nb)
Exemple #28
0
def test_read_write_script_with_metadata_241(
    no_jupytext_version_number,
    rsnb="""#!/usr/bin/env scriptisto
// ---
// jupyter:
//   jupytext:
//     text_representation:
//       extension: .rs
//       format_name: light
//   kernelspec:
//     display_name: Rust
//     language: rust
//     name: rust
// ---

let mut a: i32 = 2;
a += 1;
""",
):
    nb = jupytext.reads(rsnb, "rs")
    assert "executable" in nb.metadata["jupytext"]
    rsnb2 = jupytext.writes(nb, "rs")

    compare(rsnb, rsnb2)
def test_mark_cell_with_vim_folding_markers(script="""# This is a markdown cell

# {{{ And this is a foldable code region with metadata {"key": "value"}
a = 1

b = 2

c = 3
# }}}
"""):
    nb = jupytext.reads(script, 'py')
    assert nb.metadata['jupytext']['cell_markers'] == '{{{,}}}'
    assert len(nb.cells) == 2
    assert nb.cells[0].cell_type == 'markdown'
    assert nb.cells[0].source == 'This is a markdown cell'
    assert nb.cells[1].cell_type == 'code'
    assert nb.cells[1].source == 'a = 1\n\nb = 2\n\nc = 3'
    assert nb.cells[1].metadata == {
        'title': 'And this is a foldable code region with metadata',
        'key': 'value'
    }

    script2 = jupytext.writes(nb, 'py')
    compare(script, script2)
Exemple #30
0
def test_read_write_script_with_metadata_241(no_jupytext_version_number, pynb="""#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

a = 2

a + 1
"""):
    nb = jupytext.reads(pynb, 'py')
    assert 'executable' in nb.metadata['jupytext']
    assert 'encoding' in nb.metadata['jupytext']
    pynb2 = jupytext.writes(nb, 'py')

    compare(pynb2, pynb)