microsoft / vscode-jupyter

VS Code Jupyter extension
https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter
MIT License
1.27k stars 284 forks source link

Exporting notebook to Python script and back erroneously retains the `# %%` cell delimiter comments #15961

Open bshoshany opened 2 weeks ago

bshoshany commented 2 weeks ago

Environment data

Expected behaviour

When VS Code exports a notebook into a Python script, it indicates where each cell starts by adding a comment of the form # %%. When exporting a Python script in this format back into a notebook, these comments, which only indicate where each cell starts and are not actual code comments, should not be included in the actual cell.

Actual behaviour

The # %% comments are included in the cells, creating messy and redundant code.

Steps to reproduce:

  1. Open an empty folder in VS Code
  2. Create empty notebook test.ipynb
  3. Add Markdown cell on top with the content: "This is a Markdown cell"
  4. Add the code print("This is a code cell") to the code cell.
  5. Save the notebook
  6. Click on the ... on top of the notebook, Export, select "Python script"
  7. Save the script as test.py

The contents of the script will be:

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

# %%
print("This is a code cell")

Note: Steps 1-7 aren't actually necessary, they are only meant to demonstrate that this file is what VS Code considers to be the equivalent of the notebook we created.

  1. In the notebook, click "Select Kernel" and select Python 3.12.5 (otherwise you will get an error "Extension 'vscode.ipynb' is not known or not activated" in the next step)
  2. Open Command Palette, choose "Jupyter: Export Current Python File as Jupyter Notebook", save as test2.ipynb
  3. Open the generated notebook. It will look like this:

image

Note how the comment # %%, which is used to indicate the beginning of a cell and is not part of the actual code in the cell, is erroneously included in the cell. In larger notebooks, this happens in every code cell (but not Markdown cells):

image

Note that jupytext, which can also export and import notebook from the "percent" format, doesn't have this issue:

jupytext --to notebook --output test3.ipynb test.py

will correctly generate the notebook without the # %% comments.

korsbakken commented 1 week ago

I can confirm this at my end as well. The # %% comments are also kept if you write a .py script with cell comments (# %%) from scratch and export that to an .ipynb file using the "Jupyter: Export Current File as Jupyter Notebook" command. You don't need to have converted from an .ipynb notebook file to a .py script first to observe this behavior.

This behavior is new in 2024.7.0. If I install 2024.2.0, I get the old behavior which just converts the blocks starting with # %% to code cells and leaves out the # %% comments themselves, as expected. In versions after 2024.2.0 and before 2024.7.0, the export fails with the error message Extension 'vscode.ipynb' is not known or not activated. View Jupyter [log](command:jupyter.viewOutput) for further details,

I am using VS Code 1.92.2 running on Windows with a remote in WSL 2 (Ubuntu 22.04.4 LTS), in a Python 3.11.9 environment.