Ejemplo n.º 1
0
def test_read_simple_file(script='''# -*- coding: utf-8 -*-
"""
This is a markdown cell
"""

1 + 2 + 3 + 4
5
6

""
7

#################################
# Another markdown cell

def f(x):
   """Sample docstring"""
   return 4
''', ):
    nb = jupytext.reads(script, "py:sphinx")
    assert nb.cells[0].cell_type == "code"
    assert nb.cells[0].source == "%matplotlib inline"

    assert nb.cells[1].cell_type == "markdown"
    assert nb.cells[1].source == "This is a markdown cell"

    assert nb.cells[2].cell_type == "code"
    compare(
        nb.cells[2].source,
        """1 + 2 + 3 + 4
5
6""",
    )

    assert nb.cells[3].cell_type == "code"
    assert nb.cells[3].source == "7"

    assert nb.cells[4].cell_type == "markdown"
    assert nb.cells[4].source == "Another markdown cell"

    assert nb.cells[5].cell_type == "code"
    assert (nb.cells[5].source == '''def f(x):
   """Sample docstring"""
   return 4''')
    assert len(nb.cells) == 6

    script2 = jupytext.writes(nb, "py:sphinx")
    compare(script2, script)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
    def render(self, name, params):
        if name == 'task.ipynb':
            p = Path(name)
            convert_to = p.suffix[1:]
            name = str(p.with_suffix('.py'))
        else:
            convert_to = None

        t = self.get_template(name)
        out = t.render(**params)

        if convert_to:
            nb = jupytext.reads(out, fmt='py:light')
            out = jupytext.writes(nb, fmt=convert_to)

        return out
Ejemplo n.º 4
0
def test_notebook_one_blank_line_before_first_markdown_cell(
    script="""
# This is a markdown cell

1 + 1
""", ):
    notebook = jupytext.reads(script, "py")
    script2 = jupytext.writes(notebook, "py")
    compare(script2, script)

    assert len(notebook.cells) == 3
    for cell in notebook.cells:
        lines = cell.source.splitlines()
        if len(lines):
            assert lines[0]
            assert lines[-1]
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
def test_markdown_cell_with_code_inside_multiline_string_419(
    text='''```python
readme = """
above

```python
x = 2
```

below
"""
```
'''):
    nb = jupytext.reads(text, 'md')
    compare(jupytext.writes(nb, 'md'), text)
    assert len(nb.cells) == 1
Ejemplo n.º 7
0
def test_read_markdown_cell_with_triple_quote_307(
        script="""# This script test that commented triple quotes '''
# do not impede the correct identification of Markdown cells

# Here is Markdown cell number 2 '''
"""):
    notebook = jupytext.reads(script, 'py')
    assert len(notebook.cells) == 2
    assert notebook.cells[0].cell_type == 'markdown'
    assert notebook.cells[0].source == """This script test that commented triple quotes '''
do not impede the correct identification of Markdown cells"""
    assert notebook.cells[1].cell_type == 'markdown'
    assert notebook.cells[1].source == "Here is Markdown cell number 2 '''"

    script2 = jupytext.writes(notebook, 'py')
    compare(script2, script)
Ejemplo n.º 8
0
def test_split_on_header(markdown="""A paragraph

# H1 Header

## H2 Header

Another paragraph
"""):
    fmt = {'extension': '.md', 'split_at_heading': True}
    nb = jupytext.reads(markdown, fmt)
    assert nb.cells[0].source == 'A paragraph'
    assert nb.cells[1].source == '# H1 Header'
    assert nb.cells[2].source == '## H2 Header\n\nAnother paragraph'
    assert len(nb.cells) == 3
    markdown2 = jupytext.writes(nb, fmt)
    compare(markdown, markdown2)
Ejemplo n.º 9
0
def test_notebook_with_magic_and_bash_cells(script="""# This is a test for issue #181

# %load_ext line_profiler

# !head -4 data/president_heights.csv
"""):
    notebook = jupytext.reads(script, 'py')
    for cell in notebook.cells:
        lines = cell.source.splitlines()
        assert lines[0]
        assert lines[-1]
        assert not cell.metadata, cell.source

    script2 = jupytext.writes(notebook, 'py')

    compare(script2, script)
Ejemplo n.º 10
0
def test_read_cell_with_metadata(
    script="""# %% a code cell with parameters {"tags": ["parameters"]}
a = 3
""",
):
    nb = jupytext.reads(script, "py:percent")
    assert len(nb.cells) == 1
    assert nb.cells[0].cell_type == "code"
    assert nb.cells[0].source == "a = 3"
    assert nb.cells[0].metadata == {
        "title": "a code cell with parameters",
        "tags": ["parameters"],
    }

    script2 = jupytext.writes(nb, "py:percent")
    compare(script2, script)
Ejemplo n.º 11
0
def saveAsScript(fileName):
    if not isJupyterNotebook(): return
    fileName = fixPath(fileName)
    notebook_path = this_notebook()
    if notebook_path is None:
        print(
            'Can\'t find notebook file. Do you use non standard connection to jupyter server? Save this file as script in main menu'
        )
        return
    with open(notebook_path, 'r', encoding='utf-8') as fp:
        notebook = nbformat.read(fp, as_version=4)
    script = jupytext.writes(notebook, ext='.py', fmt='py')
    script_path = fileName
    if script_path[-3:] != '.py': script_path += '.py'
    with open(script_path, 'w', encoding='utf-8') as fp:
        fp.write(script)
def test_round_trip_python_with_js_cell_no_cell_metadata():
    notebook = new_notebook(cells=[
        new_code_cell('''import notebook.nbextensions
notebook.nbextensions.install_nbextension('jupytext.js', user=True)'''),
        new_code_cell('''%%javascript
Jupyter.utils.load_extensions('jupytext')''')
    ],
                            metadata={
                                'jupytext': {
                                    'notebook_metadata_filter': '-all',
                                    'cell_metadata_filter': '-all'
                                }
                            })
    text = jupytext.writes(notebook, 'py')
    notebook2 = jupytext.reads(text, 'py')
    compare_notebooks(notebook, notebook2)
Ejemplo n.º 13
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)
Ejemplo n.º 14
0
def test_split_on_header(markdown="""A paragraph

# H1 Header

## H2 Header

Another paragraph
""", ):
    fmt = {"extension": ".md", "split_at_heading": True}
    nb = jupytext.reads(markdown, fmt)
    assert nb.cells[0].source == "A paragraph"
    assert nb.cells[1].source == "# H1 Header"
    assert nb.cells[2].source == "## H2 Header\n\nAnother paragraph"
    assert len(nb.cells) == 3
    markdown2 = jupytext.writes(nb, fmt)
    compare(markdown2, markdown)
Ejemplo n.º 15
0
def test_markdown_cell_with_metadata_json(
    markdown="""<!-- #region {"key": "value"} -->
A long


markdown cell
<!-- #endregion -->
""", ):
    nb = jupytext.reads(markdown, "md")
    compare(
        nb.cells[0],
        new_markdown_cell(source="A long\n\n\nmarkdown cell",
                          metadata={"key": "value"}),
    )
    markdown2 = jupytext.writes(nb, "md")
    compare(markdown2, markdown)
Ejemplo n.º 16
0
def test_cell_metadata_filter_is_updated():
    text = """---
jupyter:
  jupytext:
    cell_metadata_filter: -all
---

```{r cache=FALSE}
1+1
```
"""
    nb = reads(text, "Rmd")
    assert nb.metadata["jupytext"]["cell_metadata_filter"] == "cache,-all"

    text2 = writes(nb, "Rmd")
    assert text.splitlines()[-3:] == text2.splitlines()[-3:]
Ejemplo n.º 17
0
def test_multiple_empty_cells():
    nb = new_notebook(
        cells=[new_code_cell(), new_code_cell(), new_code_cell()],
        metadata={"jupytext": {"notebook_metadata_filter": "-all"}},
    )
    text = jupytext.writes(nb, "py:percent")
    expected = """# %%

# %%

# %%
"""
    compare(text, expected)
    nb2 = jupytext.reads(text, "py:percent")
    nb2.metadata = nb.metadata
    compare_notebooks(nb2, nb)
Ejemplo n.º 18
0
def test_hide_code_tag(
    no_jupytext_version_number,
    nb=new_notebook(
        metadata={"jupytext": {
            "main_language": "matlab"
        }},
        cells=[new_code_cell("1 + 1", metadata={"tags": ["hide_code"]})],
    ),
    text="""% + tags=["hide_code"]
1 + 1
""",
):
    text2 = jupytext.writes(nb, "m")
    compare(text2, text)
    nb2 = jupytext.reads(text, "m")
    compare_notebooks(nb2, nb)
Ejemplo n.º 19
0
def test_cell_metadata_filter_is_updated():
    text = """---
jupyter:
  jupytext:
    cell_metadata_filter: -all
---

```{r cache=FALSE}
1+1
```
"""
    nb = reads(text, 'Rmd')
    assert nb.metadata['jupytext']['cell_metadata_filter'] == 'cache,-all'

    text2 = writes(nb, 'Rmd')
    assert text.splitlines()[-3:] == text2.splitlines()[-3:]
Ejemplo n.º 20
0
def test_multiline_comments_format_option():
    text = '''# %% [markdown]
"""
a
long
cell
"""
'''
    nb = new_notebook(
        cells=[new_markdown_cell("a\nlong\ncell")],
        metadata={
            "jupytext": {"cell_markers": '"""', "notebook_metadata_filter": "-all"}
        },
    )
    py = jupytext.writes(nb, "py:percent")
    compare(py, text)
Ejemplo n.º 21
0
def test_notebook_blank_lines(script="""# +
# This is a comment
# followed by two variables
a = 3

b = 4
# -

# New cell is a variable
c = 5


# +
# Now we have two functions
def f(x):
    return x + x


def g(x):
    return x + x + x


# -


# A commented block that is two lines away
# from previous cell

# A function again
def h(x):
    return x + 1


# variable
d = 6
""", ):
    notebook = jupytext.reads(script, "py")
    assert len(notebook.cells) >= 6
    for cell in notebook.cells:
        lines = cell.source.splitlines()
        if len(lines) != 1:
            assert lines[0], cell.source
            assert lines[-1], cell.source

    script2 = jupytext.writes(notebook, "py")

    compare(script2, script)
Ejemplo n.º 22
0
def test_markdown_cell_with_code_inside_multiline_string_419(
    text='''```python
readme = """
above

```python
x = 2
```

below
"""
```
''', ):
    """A code cell containing triple backticks is converted to a code cell encapsulated with four backticks"""
    nb = jupytext.reads(text, "md")
    compare(jupytext.writes(nb, "md"), "`" + text[:-1] + "`\n")
    assert len(nb.cells) == 1
Ejemplo n.º 23
0
def test_read_prev_function(
    pynb="""def test_read_cell_explicit_start_end(pynb='''
import pandas as pd
# +
def data():
    return pd.DataFrame({'A': [0, 1]})


data()
'''):
    nb = jupytext.reads(pynb, 'py')
    pynb2 = jupytext.writes(nb, 'py')
    compare(pynb2, pynb)
""", ):
    nb = jupytext.reads(pynb, "py")
    pynb2 = jupytext.writes(nb, "py")
    compare(pynb2, pynb)
Ejemplo n.º 24
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)
Ejemplo n.º 25
0
def test_multiline_comments_format_option():
    text = '''# %% [markdown]
"""
a
long
cell
"""
'''
    nb = new_notebook(cells=[new_markdown_cell("a\nlong\ncell")],
                      metadata={
                          'jupytext': {
                              'cell_markers': '"""',
                              'notebook_metadata_filter': '-all'
                          }
                      })
    py = jupytext.writes(nb, 'py:percent')
    compare(py, text)
Ejemplo n.º 26
0
def test_read_simple_file(script="""# -*- coding: utf-8 -*-
'''
This is a markdown cell
'''

1 + 2 + 3 + 4
5
6
''
7

#################################
# Another markdown cell

def f(x):
   '''Sample docstring'''
   return 4
"""):
    nb = jupytext.reads(script, ext='.py', format_name='sphinx')
    assert nb.cells[0].cell_type == 'code'
    assert nb.cells[0].source == '%matplotlib inline'

    assert nb.cells[1].cell_type == 'markdown'
    assert nb.cells[1].source == 'This is a markdown cell'

    assert nb.cells[2].cell_type == 'code'
    compare(nb.cells[2].source, '''1 + 2 + 3 + 4
5
6''')
    assert nb.cells[3].cell_type == 'markdown'
    assert nb.cells[3].source == ''

    assert nb.cells[4].cell_type == 'code'
    assert nb.cells[4].source == '7'

    assert nb.cells[5].cell_type == 'markdown'
    assert nb.cells[5].source == 'Another markdown cell'

    assert nb.cells[6].cell_type == 'code'
    assert nb.cells[6].source == """def f(x):
   '''Sample docstring'''
   return 4"""
    assert len(nb.cells) == 7

    script2 = jupytext.writes(nb, ext='.py', format_name='sphinx')
    compare(script, script2)
Ejemplo n.º 27
0
def test_questions_in_unmarked_cells_are_not_uncommented_297(
    text="""# This cell has no explicit marker
# question?
1 + 2
""",
    nb=new_notebook(cells=[
        new_code_cell(
            "# This cell has no explicit marker\n# question?\n1 + 2",
            metadata={"comment_questions": False},
        )
    ]),
):
    nb2 = jupytext.reads(text, "py")
    compare_notebooks(nb2, nb)

    text2 = jupytext.writes(nb2, "py")
    compare(text2, text)
Ejemplo n.º 28
0
def test_pep8_bis():
    text = """# This is a markdown cell

# a code cell
def f(x):
    return x + 1

# And another markdown cell
# Separated from f by just one line
# As there is no code here
"""
    nb = reads(text, "py")
    for cell in nb.cells:
        assert not cell.metadata

    text2 = writes(nb, "py")
    compare(text2, text)
Ejemplo n.º 29
0
def convert_notebook_files(nb_files,
                           nb_dest,
                           test_round_trip=False,
                           preserve_outputs=True):
    """
    Export R markdown notebooks, python or R scripts, or Jupyter notebooks,
    to the opposite format
    :param nb_files: one or more notebooks files
    :param nb_dest: destination file, extension ('.py') or format ('py')
    :param test_round_trip: should round trip conversion be tested?
    :param preserve_outputs: preserve the current outputs of .ipynb file
    when possible
    :return:
    """

    if len(nb_files) > 1 and nb_dest not in NOTEBOOK_EXTENSIONS:
        raise ValueError("Converting multiple files requires "
                         "that destination be one of '{}'".format(
                             "', '".join(NOTEBOOK_EXTENSIONS)))

    for nb_file in nb_files:
        file, current_ext = os.path.splitext(nb_file)
        if current_ext not in NOTEBOOK_EXTENSIONS:
            raise TypeError('File {} is not a notebook'.format(nb_file))

        notebook = readf(nb_file)
        dest, dest_ext = os.path.splitext(nb_dest)
        if not dest_ext:
            dest = file
            if nb_dest in NOTEBOOK_EXTENSIONS:
                dest_ext = nb_dest
            else:
                dest_ext = '.' + nb_dest

        if dest_ext not in NOTEBOOK_EXTENSIONS:
            raise TypeError(
                'Destination extension {} is not a notebook'.format(dest_ext))

        if test_round_trip:
            test_round_trip_conversion(notebook, dest_ext, preserve_outputs)

        if '.' in nb_dest:
            save_notebook_as(notebook, nb_file, dest + dest_ext,
                             preserve_outputs)
        elif not test_round_trip:
            print(writes(notebook, ext=dest_ext))
Ejemplo n.º 30
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)