goerz / jupytext.vim

Vim plugin for editing Jupyter ipynb files via jupytext

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ipynb file may get overwritten, causing updates to fail.

BlueDrink9 opened this issue · comments

Describe the bug

If the ipynb file is overwritten, for example if the user has an autosave autocmd, the update command fails.

  • Does converting the notebook to/from ipynb with jupytext on the command line work?

No if using the --update argument, yes otherwise.

I'm unsure about the best way to go about this, but possibly, if jupytext fails to update, try again without the update? Potentially with a check for the ipynb file contents now being the same as the buffer?

@goerz do you have any thoughts here?

Hello @goerz , @BlueDrink9. I am not an advanced VIM user, but maybe I can contribute my own experience with this.

I use paired notebooks (py/ipynb), and open them simultaneously in Jupyter (ipynb) and in my text editor (py file, in PyCharm Pro). Conflicts do occur, either because of the autosave, or because I have changed the notebook in both Jupyter and my text editor. In this situation, Jupyter and the editor both tell me that the file has changed on disk, and give me the option to reload or overwrite. Most of the time I am happy with overriding the notebook with one of the two versions. But sometimes I prefer to merge the two edits, and here I find PyCharm very convenient, as it lets me compare the two different version of the .py file (in memory vs on disk), and merge them.

Would that work in VIM as well? Do you need something else than paired notebooks (i.e. jupytext --set-formats ipynb,py notebook.ipynb)?

The problem is not that they become out of sync. This plugin opens a buffer called [].ipynb, but fills it with the translated content. The write command that this plugin uses to then translate it back to the ipynb format in the file doesn't always replace the default vim write command, meaning the ipynb file sometimes ends up as a plaintext file, rather than a valid json.

Are you able to reproduce a situation where the plain text ends up being written to the ipynb file? I think I’ve had that happen once or twice, but wasn’t able to determine the cause.

@mwouts To be clear: I think if you actually have linked notebooks in a directory, it’s best not to use the jupytext.vim plugin and to just open the actual py or md file in vim. I don’t have much experience with this, though, as I haven’t really used linked notebooks so far.

I think this is one of the things causing it.

function! Autosave()
    " Don't autosave if there is no buffer name.
    if bufname('%') != '' && &ro != 1 && &modifiable == 1
        " Automatically save before commands like :next and :make
        set autowrite
        " If no changes, don't touch modified timestamp.
        silent! update
    endif
endfunction
au! FocusLost,InsertLeave,BufLeave * call Autosave()