jupytext
jupytext copied to clipboard
autosaving (autosyncing) jupyter notebook to a python file while saving in vscode?
I'm wondering if it's possible to sync a Jupyter notebook with a python script whenever a save event is happening for that file in vscode? I know that there are such options in JupyterLab and Jupyter Notebook, but I could not find such options in VS Code Jupyter renderer.
Hello @FeryET , that is a great question.
At the moment there is no documented way to do this, but if you'd like to experiment a bit maybe we can try to do something.
This is what I tried this evening:
- Install a development version of Jupytext that provides the
--sync-assert-latest
argument withpip install git+https://github.com/mwouts/jupytext.git@sync_assert_latest
- Install an extension for VS Code that lets you run a command every time you save a notebook (or a Python file), for instance RunOnSave or FileWatcher
- Configure the extension to run on every type of notebook, e.g. with this settings:
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.(ipynb|md|py)$",
"cmd": "jupytext --sync-assert-latest ${file}"
}
]
}
- Add a metadata to the notebook to turn it into a paired file (sorry I don't know how to edit the notebook metadata with VS Code), or create a
jupytext.toml
file with e.g. this content:formats = "ipynb,py:percent"
(but then every.py
file will get an paired.ipynb
file..), orformats = "notebooks///ipynb,scripts///py:percent"
assuming that your notebooks are undernotebooks
The above seems to work partially, in the sense that every time I save a .py
file then the corresponding .ipynb
file is created or updated, however the reverse direction des not seem to work - the "RunOnSave" extension does not seem to be triggered when I save an .ipynb
file... Would you have an idea on how to activate it on .ipynb
files? Do you think it will work better with "FileWatcher" ?
Dear @mwouts , there is a discussion on vs-code https://github.com/microsoft/vscode-jupyter/issues/1240 about this. Someone also make the extension to make .py
file generated on saving .ipynb
. See here
Maybe you can look at that repo and reimplement jupytext
there to make a full support.
Thank you @yasirroni for the update, that is very helpful.
Well I am confident that @DonJayamanne will do a great work on this - If I am correct he was the initial author of the Python extension for VS Code, so I'm sure he is one of the most qualified persons to find out how it should be done for VS Code.
@DonJayamanne I see the roadmap for the vscode-jupytext extension, and I see that you are willing to remove the dependency on Python. At some point I also wondered if the conversion routines (and the function that merges a text notebook with a notebook with output) should not be coded in TypeScript (for better interoperability with Jupyter Lab and plugins like jupyterlab-gitlab
or jupyterlab-github
). What is the native language used by VS Code extension? Would a TypeScript implementation be useful for your project?
A brute-force approach to the problem would be to write a little program that watches files that are to be kept in sync and responds to changes in them by running jupytext --sync
. One could probably arrange for such a program to be started when opening a project that contains jupytext content.
Of course it is nicer if the sync is triggered by the editor, but that doesn't seem possible at the moment. And on the plus side this should work for any editor.
Possibly jupytext itself could be told to operate in such a mode.