Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • created file jupytext in the folder ~/.config
  • put the next code into this file:
# 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?

like image 485
Andrei Krivoshei Avatar asked Jan 18 '26 14:01

Andrei Krivoshei


2 Answers

"Jupytext Sync" VS Code Extension (2025)

There have been some exciting recent developments for syncing Python .py script (in py:percent format) and Jupyter notebooks (.ipynb) in VS Code. A new extension, Jupytext Sync (GitHub) by Victor Negîrneac, now provides the requested features and is designed to work similarly to JupyterLab's Jupytext integration, but directly in VS Code.

You can easily install the extension from the marketplace: Jupytext Sync (marketplace).

According to these GitHub discussions, the extension is even endorsed by the JupyText project itself. This is great news for anyone looking for seamless integration and automatic synchronization between Jupyter notebooks and text files in VS Code.

Original Answer (2024)

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.

like image 60
divenex Avatar answered Jan 21 '26 02:01

divenex


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.

like image 37
Jill Cheng Avatar answered Jan 21 '26 02:01

Jill Cheng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!