jupytext icon indicating copy to clipboard operation
jupytext copied to clipboard

python file created through pre-commit hook is not paired

Open kfchou opened this issue 2 years ago • 3 comments

Scenario

  • Create a notebook hello.ipynb, stage it
  • run pre-commit with the following config:
-   repo: https://github.com/mwouts/jupytext
    rev: v1.14.0  # CURRENT_TAG/COMMIT_HASH
    hooks:
    - id: jupytext
      args: [--from, ipynb, --to, "py:percent", --pipe, black, ruff]
      additional_dependencies:
        - black==22.12.0
        - ruff==0.0.218
  • jupytext creates hello.py.
  • stage and commit hello.py, all is well

Issue

I did all of the above, then

  • I edit hello.py, stage it
  • run pre-commit hook
  • hello.ipynb is not updated <------------------
  • try to run jupytext --sync in command line, but got
[jupytext] Warning: hello.ipynb is not a paired notebook

Is the pre-commit hook incorrectly set up?

I would like to avoid using global configs (described here) because some of my python script files should not be converted into notebooks.

kfchou avatar Feb 16 '23 22:02 kfchou

Hello @kfchou , thanks for sharing your use case!

Well the reason why you don't get a paired file is because you use jupytext --to rather than jupytext --sync.

Would you like to replace the arguments to the jupytext command above with e.g. [--set-formats', 'ipynb,py:percent'] like in this test? https://github.com/mwouts/jupytext/blob/main/tests/test_pre_commit_1_sync_with_no_config.py#L29-L41

Note that a possible drawback of doing so is that every .py file is going to be paired as well, so you might want to do this only in specific directories (then we're back to configs, why not using a jupytext.toml config only in the notebook directories?)

mwouts avatar Feb 19 '23 14:02 mwouts

every .py file is going to be paired as well

I'd like to avoid this if possible

do this only in specific directories

I tried the following and it doesn't seem to work, as in both notebook directories would get copies of python scripts from both scripts directories.

[formats]
"notebooks1/" = "ipynb"
"scripts1/" = "py:percent"
"notebooks2/" = "ipynb"
"scripts2/" = "py:percent"

why not using a jupytext.toml config only in the notebook directories?

Is this what you mean?

root
| notebook dir 1
  | jupytext.toml
| src
| notebook dir 2
  | jupytext.toml

kfchou avatar Mar 01 '23 18:03 kfchou

Indeed, if you have two notebook directories you will have to use two jupytext.toml files located in each directory.

Both files should have the same content, e.g. formats="ipynb,py:percent" if you want .py and .ipynb files in the same directory. You can also use prefix as above, but with a single path for each extension.

mwouts avatar Mar 02 '23 07:03 mwouts