Skip to content
Advertisement

Best way to convert .ipynb to .py in VSCode

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.

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement