Andrei Krivoshei
Andrei Krivoshei

Reputation: 511

How to config automatic sync Jupyter notebook .ipynb and .py files in VSCode e.g. by using Jupytext

I have written some Jupyter notebooks using original Jupyter notebook web interface. All notebooks are synced nicely in this way.

But now, I would like to edit my notebooks in the VSCode. But I cannot configure syncing notebook file with its python script.

I tried this using jupytext:

# Always pair ipynb notebooks to py:percent files
default_jupytext_formats = "ipynb,py:percent"

But no effect!

(Update) Can this be achieved, as a first solution, using VSCode Tasks (I am not used tasks yet)?

May be it possible to run the task with jupytext command if the notebook file is opened/saved/modified?

Upvotes: 14

Views: 6836

Answers (5)

adir
adir

Reputation: 11

I also suggest to add

%matplotlib inline
%load_ext autoreload
%autoreload 2

to the head of py file in case "Open as a Jupyter Notebook" not responding

Upvotes: 0

divenex
divenex

Reputation: 17178

One can create a Visual Studio Code hotkey to quickly synchronize the .py script (in py:percent format) and the .ipynb Jupyter Notebook on demand with JupyText as described in this article:

  • Install jupytext e.g. with pip install jupytext.
  • Create a task.json file (see below) in the .vscode/ folder of your workspace. This defines a default VSCode task that runs the jupytext command to synchronize the two files.
  • While editing either of the two .py or .ipynb files, type the shortcut Ctrl+Shift+B to synchronize their content.

The file task.json should contain

{
  "tasks": [
    {
      "label": "JupyText Sync",
      "type": "shell",
      "command": "jupytext --sync ${file}",
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "presentation": {
        "reveal": "never",
      }
    }
  ]
}

This is not fully automatic, but nearly as convenient.

Upvotes: 4

Stefnotch
Stefnotch

Reputation: 694

This is currently unsupported by Visual Studio Code.

A feature request with 50 upvotes for it exists at https://github.com/microsoft/vscode-jupyter/issues/1240

The relevant Jupytext feature request is https://github.com/mwouts/jupytext/issues/875 . It sadly has stagnated, so I'm hoping that someone with the right knowledge manages to fix it.

Upvotes: 1

Franco Milanese
Franco Milanese

Reputation: 430

I found some issues and really hard to collaborate on Jupytext extensions available, so I have created my own version at: https://marketplace.visualstudio.com/items?itemName=FrancoMilanese.datascientists-utils

It's similar to those mentioned in other answers and comments, but it includes also export to HTML with a TOC, and also a selection of a user defined python interpreter, if you are using python under a virtual env or something like that, please feel free to try it out.

Collaborations are always welcome.

Upvotes: 1

Jill Cheng
Jill Cheng

Reputation: 10354

Currently, VSCode does not support such a function. The Jupyter function in VSCode is provided by a Python extension, which supports us to convert between .ipynb files and .py files in VSCode.

.ipynb files to .py files : Export as python script.

.py files to .ipynb files : Right click, "Export Current Python File as Jupyter Notebook"

I have submitted the requirement you described, and we look forward to the realization of this feature. Giuhub link: How to synchronize the jupyter file and python file of VSCode.

Upvotes: 3

Related Questions