11

I'm looking for a good way to convert .ipynb to .py files in VSCode. So far I've tried:

  • the "Export As" Option built into vscode. Not ideal as it produces the following at the start of the script, as well as "Run Cell", "Run Below", etc. buttons/links:

"To add a new cell, type '# %%' To add a new markdown cell, type '# %% [markdown]' %% from IPython import get_ipython"

  • nbconvert. I usually insert this as a command in the script itself (https://stackoverflow.com/a/19779226/14198216) so it's automatically converted and saved once run. But this also leaves "Run Cell" etc, as well as execution markings (ex: "In [1]")

  • jupytext. I also usually insert this as a command. At the start of the .py, it produces:

-- coding: utf-8 -- --- jupyter: jupytext: text_representation:

Is there a nice, minimalist solution that doesn't insert a bunch of gunk (which needs to be manually edited out) in the .py version of my notebooks and can be easily/automatically executed from the notebook itself? This could also be setting tweaks that I'm currently unaware of that can make one of the things I mentioned work better.

Thanks in advance.

Isaac Liu
  • 199
  • 1
  • 2
  • 7
  • Depending on your system can easily follow-up your jupytext command with [a sed command to remove the first line](https://stackoverflow.com/a/5721563/8508004). Jupytext is nice in that it allows [a nice round-trip ability](https://jupytext.readthedocs.io/en/latest/using-cli.html). However, I don't know if the round-trip conversion works as advertised without the normal first line. – Wayne Feb 06 '22 at 20:18

6 Answers6

17

You need to install the Jupyter package:

https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter

Then open "Command Palette" (⇧⌘P) and search for this line:
Jupyter: Convert/Export to Python script \

enter image description here

Mohamad Hamouday
  • 2,070
  • 23
  • 20
  • 2
    It is called now 'Jupyter: Convert to Python script' – Rene Smit Apr 29 '22 at 05:21
  • 1
    This solution produces a file that includes the "Interactive mode" cruft like "'# %%' " that the question asker specifically said they don't want. However, as another answer indicates, these are quite easy to remove. – 0012 Dec 20 '22 at 22:07
4

We can add the following settings in "settings.json", the generated python file will not have "Run Cell", "Run Above", "Debug Cell":

"jupyter.codeLenses": " ",

enter image description here

Jill Cheng
  • 9,179
  • 1
  • 20
  • 25
3

Converting to .py by Command Palette will open a file, just select one of the markdowns # %%, change all occurrences (Ctrl+F2), and remove all # %% at once. This will remove all "Run Cell" etc... Save file.

jazzyjoint
  • 31
  • 1
3

In VSCode v1.75.1, if you right-click on a notebook file (.ipynb) in the file explorer side panel, you will have an option called 'Import Notebook to Script'. This worked for me. Image shows options after right-click

2

If you don't want to modify your settings.json file (or don't know where it is stored), you can actually change how python files are exported through the VSCode IDE.

Bring up settings (ctrl + ,) or click the big cog on the bottom left and search for "jupyter code". The option you want to unselect is Jupyter > Interactive Window > Code Lens: Enable

How to find the setting

Now when you export a notebook, it should remove that option the run a cell although the %'s will still be there.

Darsh
  • 21
  • 1
1

I found that there is a way to export right within VS Code:

https://code.visualstudio.com/docs/python/jupyter-support-py

Export a Jupyter notebook

In addition to opening a Jupyter notebook, you can also use one of the following commands from the Command Palette (⇧⌘P) to export content from a Python file in VS Code to a Jupyter notebook (with the .ipynb extension).

  • Jupyter: Export Current Python File as Jupyter Notebook: creates a Jupyter notebook from the contents of the current file, using the # %% and # %% [markdown] delimiters to specify their respective cell types.
  • Jupyter: Export Current Python File and Output as Jupyter Notebook: creates a Jupyter notebook from the contents of the current file and includes output from code cells.
  • Jupyter: Export Interactive Window as Jupyter Notebook: creates a Jupyter notebook from the contents of the Python Interactive window.

After exporting the contents, VS Code displays a prompt through which you can open the notebook in a browser.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
u5easdrctd
  • 11
  • 1