Skip to content
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

How to use with Jupytext Markdown notebooks #668

Closed
basnijholt opened this issue Oct 21, 2021 · 12 comments · Fixed by #745
Closed

How to use with Jupytext Markdown notebooks #668

basnijholt opened this issue Oct 21, 2021 · 12 comments · Fixed by #745
Labels
enhancement New feature or request

Comments

@basnijholt
Copy link

We keep our notebooks in Markdown format using Jupytext. Is there an easy way to get nbQA to work with these files?

I can probably write a pre-commit hook that

  • converts md -> ipynb
  • runs nbQA
  • converts back to md

But I wonder whether there is a simpler solution.

@MarcoGorelli
Copy link
Collaborator

Hey @basnijholt

I think you can sync Python files with notebooks in Jupytext? If so, you could run your Python tools on those files directly

If you want to keep them in markdown, I don't think there's a simpler solution, but thanks anyway for your issue!

@rgommers
Copy link

We just ran into this for SciPy. Wouldn't it be useful for nbqa to be able to do this? Either by natively understanding jupytext files, or by doing the "convert to ipynb, run nbqa, convert back" internally?

Jupytext is the best way to keep Jupyter notebooks in version control without making a mess, so it would be nice if Jupytext had broad tooling support.

@MarcoGorelli
Copy link
Collaborator

Hey @rgommers

Looks like there's a few people interested in this. Given this line in the shed readme, I think this might include @Zac-HD

I'll take a look, just want to make sure this doesn't accidentally open up a rabbit hole of features to support

Reopening for now then - in the meantime, some local script which does "convert to ipynb, run nbqa, convert back" sounds like it should be fine

As a side-note, you don't need nbqa to run black on notebooks, I'd suggest using nbqa for running other linters

@MarcoGorelli MarcoGorelli reopened this Sep 12, 2022
@MarcoGorelli MarcoGorelli added enhancement New feature or request and removed question Further information is requested labels Sep 12, 2022
@MarcoGorelli
Copy link
Collaborator

@basnijholt if you have some .md notebooks handy, would really appreciate it if you could try this out and help unearth bugs before a release comes out later this week:

$ pip install git+https://github.com/MarcoGorelli/nbQA.git@jupytext jupytext black
$ nbqa black . --nbqa-files '\.md$'

@basnijholt
Copy link
Author

basnijholt commented Sep 13, 2022

Thanks a lot @MarcoGorelli!

I have tested this on the Adaptive documentation: https://github.com/python-adaptive/adaptive/tree/master/docs/source/tutorial

However, it's not doing anything and simply reporting:

All done! ✨ 🍰 ✨
48 files left unchanged.

When I call it directly:

(base) basnijholt-macbook  ➜  tutorial git:(master) ✗  nbqa black . --nbqa-files tutorial.AverageLearner.md
No valid Python notebooks found in given path(s)

@MarcoGorelli
Copy link
Collaborator

Thanks @basnijholt for trying this out!

I've had a look at that notebook, and it looks like it's missing

  language: python

in kernelspec, which nbqa was expecting (to be safe, I was thinking of not formatting notebooks not explicitly marked as Python)

I'll see if there's a way around this - perhaps just checking kernelspec->name is enough

@MarcoGorelli
Copy link
Collaborator

Hey @basnijholt - I've pushed another commit now, to check the kernelspec name as well. Now, from python-adaptive, I get:

$ nbqa black . --nbqa-files '\.md$'
reformatted docs/source/tutorial/tutorial.Learner2D.md
reformatted docs/source/tutorial/tutorial.LearnerND.md
reformatted docs/source/algorithms_and_examples.md

All done! ✨ 🍰 ✨
3 files reformatted, 44 files left unchanged.

Note that, to keep execution: in the metadata, you'd probably need to set that in a jupytext config file, e.g. like
https://github.com/pymc-devs/pymc-examples/blob/b05301c39be2fdeebfc1c0ed9549c72e53bba61f/.jupytext.toml
(but with execution instead of substitutions)

@MarcoGorelli
Copy link
Collaborator

MarcoGorelli commented Sep 18, 2022

Should 🤞 be available, as of version 1.5.0 1.5.1

basnijholt added a commit to python-adaptive/adaptive that referenced this issue Sep 19, 2022
This is now possible after nbQA-dev/nbQA#745 which solved this issue (nbQA-dev/nbQA#668) I opened a year ago.
basnijholt added a commit to python-adaptive/adaptive that referenced this issue Sep 19, 2022
This is now possible after nbQA-dev/nbQA#745 which solved this issue (nbQA-dev/nbQA#668) I opened a year ago.
@MarcoGorelli
Copy link
Collaborator

@all-contributors please add @basnijholt and @rgommers for ideas

@allcontributors
Copy link
Contributor

@MarcoGorelli

I've put up a pull request to add @basnijholt! 🎉

@MarcoGorelli
Copy link
Collaborator

looks like these need doing separately

@all-contributors please add @rgommers for ideas

@allcontributors
Copy link
Contributor

@MarcoGorelli

I've put up a pull request to add @rgommers! 🎉

basnijholt added a commit to python-adaptive/adaptive that referenced this issue Apr 7, 2023
* Add NBQA for notebook and docs linting

This is now possible after nbQA-dev/nbQA#745 which solved this issue (nbQA-dev/nbQA#668) I opened a year ago.

* Run pre-commit filters on all files

* Lint

* bump

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update os

* Fix all nbqa issues

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants