-
Notifications
You must be signed in to change notification settings - Fork 386
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
Jupytext plugin for the pre-commit package, compatible with black and flake8 #142
Comments
Hello @phaustin , thanks for letting me know about the pre-commit package, which looks great! We started exploring pre-commit hooks at #121 recently, and have implemented a Is that pre-commit mode good for you? Otherwise, you seem to be interested in a full python implementation of the conversion, right? Do you want to have a look here and try something like: import sys
import jupytext
nbfile = Path(sys.argv[1])
pyfile = str(notebook.with_suffix(".py"))
nb = jupytext.readf(nbfile)
jupytext.writef(nb, pyfile, format='percent') Please let me know which way is best for you! |
using the readf/writef pair is certainly cleaner that having to spawn a subprocess, thanks for that suggestion. The issue I found with the jupytext --pre-commit flag is that the pre-commit package interprets the jupytext stdout message as a failure code, and won't proceed with the commit. Swallowing that output as part of my own commit script also allows me to commit the py:percent files to a python subfolder, which I think students will find easier to grep as they look for code tips from assignment notebooks. The pre-commit package also allows me to add flake8 and black to the hook. Thanks for jupytext, I've been looking for something like this for quite a while. |
Oh, that's interesting! So if I summarize, you have prepared a flake8 + black pre-commit hook for notebooks? That's going to improve very significantly the quality of notebooks!! Do you plan to write a post about that ? Now, returning to our discussion, do you think a |
(not sure, but there might be another change needed as part of this to prevent a race condition as pre-commit and jupytext both try to add the python file to the index?) This would allow a natural progression from standalone precommit hook to pre-commit package-registered hook, to a user-defined hook that does roundtrip cleaning. |
Well, that are very nice perspectives! Thanks also for sharing your course. I gave a quick look at it, and saw that you have a lot of material there. For this issue, I will try to see
|
I found a few implementations of black for notebooks at psf/black#298:
|
Definitely looks like there's a market for this -- happy to help with testing and spreading the word. |
Hello @phaustin , I see you are exporting the python representation of your notebooks to a python folder. That is a use case that I would like to support in the upcoming version 1.0. Can I ask your advice on how to encode this in the format specification? I have been thinking of the following:
Therefore,
Do you deem this as useful? Do you see any other alternative to the double slash for folders (single slash means prefix)? |
By the way: I plan to improve the interaction between jupytext and reformating tools with #154 |
Hi Marc -- yes, we'd definitely like/use this. At the moment we're doing this by hand: |
Hello @phaustin . I am trying to figure out how jupytext could best work with the pre-commit package. I would like to be able to do each of the following:
As I am just discovering the pre-commit package, may I ask you a few questions: Thanks ! |
Hi @phaustin , I have prepared a release candidate for Jupytext 1.0. It has many new features, including the Could you please give a try to the new version, available with
and tell me if you can write a simple and satisfactory hook for the pre-commit package with the new release? Maybe we should also have a specific section on this in the documentation. Thanks! |
Yes, this is working well with our pre-commit workflow. Currently we
|
Hello @phaustin, again! I am trying to converge on release 1.0.0 (now with rc3), and was wondering how to resolve this issue. You seem satisfied with the solution you describe above, and this is great ! Also, I've been experimenting on my side with the following pre-commit hooks:
(the second being the Now, do you think there could be a 'standard' jupytext pre-commit hook for the pre-commit hook package ? Possibly one of
Are they generic enough? Or, should we instead let the users write their own pre-commit hook using the many possible Jupytext's combinations ? |
Currently I think that by the time people adopt the pre-commit package they are beyond the simpler cases, and it's too hard to predict what their filter choices are going to be to bless one particular combination. For those users I believe the clearest approach is to let pre-commit handle the python files and jupytext the roundtrip conversion between python and notebooks. |
Sure! So we're on the same line. Thanks @phaustin for your feedback. |
I am using following script for my pre-commit hook but it does not add the .py file (.pynb does does get added and it does the conversion to .py script) automatically to the commit. Am I missing something or this is not supported yet? jupytext --from ipynb --to python//py:light --pre-commit |
Thanks @mwouts |
How about this https://github.com/KwatME/clean_ipynb? |
Thanks @KwatME for sharing! I guess the difference between Jupytext + Black and |
Agree. I think jupytext is wonderful because it can do so much and well liked by the community. I want to see how I can start using it :) Thanks @mwouts |
Thank you @KwatME for your kind words. Well you may be interested in the |
the pre-commit package (described in this blog) provides a framework for managing precommit hooks. It would be great if jupytext had an option that would allow it to be a pre-commit hook with that package -- essentially it would just need to suppress the stdout message and return 0, adding the translated file to the index. Specifically, this script is working for me now:
when I save it as pre_commit_jupytext.py and call it with a pre-commit config entry in
.pre-commit-config.yaml that looks like this:
but I think smoothing this a little with a command line option would be a nice addition.
The text was updated successfully, but these errors were encountered: