Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When notebook ends in a code cell, generated python code has two empty lines at the end instead of one #682

Closed
stanleygu opened this issue Nov 28, 2020 · 2 comments
Milestone

Comments

@stanleygu
Copy link

Steps to reproduce:

  • Have a notebook that ends in a python code cell (with no empty lines at the end of the cell).
  • run jupytext --to py:percent notebookWithCodeCell.ipynb.
  • See that the generated python code file has two empty lines at the end.

I would expect there to just be one new line.

The main motivation for this is that black reformats the code to just have one new line, and it would be nice if the generated code was stable when using black.

An alternative is to use --pipe black in the conversion, but I use the JupyterLab paired notebook, which runs the sync automatically without the pipe.

@mwouts
Copy link
Owner

mwouts commented Dec 2, 2020

Hi @stanleygu , thanks for paying attention to this. This is a point that is not explicitly stated in the documentation, but indeed, it is also my expectation that the files generated by Jupytext are PEP8 compliant when the input cells are.

I gave a try to your report and I have not been able (yet) to reproduce your issue. May I ask you, therefore, to have a look at the metatada for that cell? Do you have, for some reason, a metadata named lines_to_next_cell equal to 2? That metadata is introduced by Jupytext when the .py file is not PEP8, to avoid changing the .py file when converting it to a notebook and back.

@mwouts
Copy link
Owner

mwouts commented Jan 15, 2021

I have added a test, in which I find only one empty line at the end of scripts:

def test_notebook_ends_with_exactly_one_empty_line_682():
    """(Issue #682)
    Steps to reproduce:
        Have a notebook that ends in a python code cell (with no empty lines at the end of the cell).
        run jupytext --to py:percent notebookWithCodeCell.ipynb.
        See that the generated python code file has two empty lines at the end.
    I would expect there to just be one new line."""
    nb = new_notebook(
        cells=[new_code_cell("1+1")], metadata={"jupytext": {"main_language": "python"}}
    )
    py = writes(nb, "py:percent")
    assert py.endswith("1+1\n")

As I cannot reproduce the issue I am going to close it. Nevertheless, if you have another example I am interested - please submit it in the form of a test as seen above. Thanks

@mwouts mwouts closed this as completed Jan 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants