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

4

14

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?

Prostitute answered 8/10, 2020 at 12:42 Comment(3)
How to understand "syncing notebook file with its python script"? Currently VSCode supports exporting Jupyter as a python file.Howlett
Jupytext extension in the Jupyter (web version) automatically synchronize the .py file with the .ipynb. So, I working only with .ipynb file and .py file is synced "itself". In VSCode I can manually export the.ipynb into .py. But how I can config the VSCode or Jupytext to synchronize these files in background, by working with notebook file in the VSCode?Prostitute
An extension (still in preview) attempts to do just this: marketplace.visualstudio.com/…Evelineevelinn
H
3

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.

Howlett answered 9/10, 2020 at 9:33 Comment(4)
Can this be achieved, as a first solution, using 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?Prostitute
@Andrei Krivoshei -"jupytext" is an extension of "jupyter" and is currently not supported in VSCode.Howlett
jupytext can be executed for files syncing from a command line. May be it can be automated using Task in VSCode for open notebook file on the save operation (I am not familiar with Tasks)?Prostitute
@Andrei Krivoshei -For the use of "jupytext" in "Task", we need to do some research, and we will update it here once there is any progress.Howlett
E
3

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.

Euler answered 7/6 at 12:27 Comment(1)
This is great, thanks for your post! Note that here jupytext will be run from new shell. To instead use the Python interpreter that your VSCode workspace uses, amend that line to: "command": "${command:python.interpreterPath} -m jupytext --sync ${file}",Oliviaolivie
S
1

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.

Scutiform answered 17/7, 2023 at 18:47 Comment(0)
H
1

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.

Harass answered 4/11, 2023 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.