Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autosaving (autosyncing) jupyter notebook to a python file while saving in vscode? #875

Open
FeryET opened this issue Nov 9, 2021 · 4 comments

Comments

@FeryET
Copy link

FeryET commented Nov 9, 2021

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.

@mwouts
Copy link
Owner

mwouts commented Nov 21, 2021

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:

  1. Install a development version of Jupytext that provides the --sync-assert-latest argument with pip install git+https://github.com/mwouts/jupytext.git@sync_assert_latest
  2. 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
  3. 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}"
            }
        ]
    }
  1. 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..), or formats = "notebooks///ipynb,scripts///py:percent" assuming that your notebooks are under notebooks

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" ?

@yasirroni
Copy link

yasirroni commented Feb 15, 2022

Dear @mwouts , there is a discussion on vs-code microsoft/vscode-jupyter#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.

@mwouts
Copy link
Owner

mwouts commented Feb 16, 2022

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?

@td-anne
Copy link

td-anne commented Jan 30, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants