mwouts / jupytext

Jupyter Notebooks as Markdown Documents, Julia, Python or R scripts
https://jupytext.readthedocs.io
MIT License
6.65k stars 386 forks source link

python file created through pre-commit hook is not paired #1039

Open kfchou opened 1 year ago

kfchou commented 1 year ago

Scenario

Issue

I did all of the above, then

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.

mwouts commented 1 year ago

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

kfchou commented 1 year ago

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
mwouts commented 1 year ago

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.