1

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
  • 79
  • 2
  • 5
  • 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

3 Answers3

2

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
  • 7,195
  • 1
  • 13
  • 22
2

First, you need to install the Jupyter package:

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

Second open "Command Palette" (⇧⌘P) and search for this line: Jupyter: Export to Python Script enter image description here

Mohamad Hamouday
  • 1,446
  • 16
  • 17
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
  • 19,150
  • 23
  • 79
  • 104
u5easdrctd
  • 11
  • 1