nbQA-dev / nbQA

Run ruff, isort, pyupgrade, mypy, pylint, flake8, and more on Jupyter Notebooks
https://nbqa.readthedocs.io/en/latest/index.html
MIT License
1.04k stars 41 forks source link

How to guide to write my own tool? #655

Closed ianozsvald closed 2 years ago

ianozsvald commented 3 years ago

Is there a how-to guide to write my own tool? I want to do some conditional stripping/modifying (e.g. insert a Python script into a cell) for a Kaggle notebook. I've been playing around for an hour and I've sort-of-got a tool working, but I can't get nbqa to pass in anything I can read. In my code sys.argv has the original Notebook filename and if I rewrite that, nbqa throws an exception.k

Here's my example call $ nbqa "nbqa_cell_modifier.nbqa_cell_modifier" scratch_nb2.ipynb where nbqa_cell_modifier is presented in setup.py as both a cmd line call and as a package with an entry point (main()).

In my simplest testing if I return a value or print (to stdout or stderr or with print), I had expect that this value would replace whatever was in a cell...but that's not the case. My mental model is probably quite broken for what you present to a tool like black.

It is very likely that my hastily thrown together script is not handling whatever you pass in, or presenting an appropriate interface, but I'm not sure what I'm supposed to receive.

Do you pass through a filename for the Python version of a Notebook's extracted code as a "module" (i.e. 1 temp file per cell)? Do you pass through on stdin and expect a result on stdout/stderr? Neither of the above seem true in my very limited testing, so I'm a bit lost.

Can you give me a point for what you expect from a receiving tool - or point me at some code that correctly calls e.g. black? I can see that the work is done in your __main__.py but I'm having trouble following the simple path through that.

MarcoGorelli commented 3 years ago

Hey @ianozsvald , good to hear from you, and thanks for your question!

nbqa expects to receive a tool which would work on a Python file. To check that it'll work you should first do

python -m nbqa_cell_modifier.nbqa_cell_modifier t.py

where t.py is a Python file with some code. Check that t.py was changed successfully (or just use --nbqa-diff to preview the result without modifying t.py). Then, if you run

nbqa nbqa_cell_modifier.nbqa_cell_modifier scratch2_nb.ipynb

I'd expect it to work.

Do you have a repo for nbqa_cell_modifier.nbqa_cell_modifier? If you add me to it I can take a look

Is there a how-to guide to write my own tool?

Not currently, but I'll add one - thanks for the suggestion!

ianozsvald commented 3 years ago

Much obliged, my code runs with -m as per your example. I need to tidy it up and post it to github, then I'll message back (in a few days). I've long-wanted an easy way to programmatically edit cells from an external tool so I'm keen to get this working. Cheers!