question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jupytext plugin for the pre-commit package, compatible with black and flake8

See original GitHub issue

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:

import sys
import subprocess
from pathlib import Path

cmd = f"jupytext --to py:percent {sys.argv[1]}"
notebook = Path(sys.argv[1])
pyfile = str(notebook.with_suffix(".py"))
status1, output1 = subprocess.getstatusoutput(cmd)
cmd = f"git add {pyfile}"
status2, output2 = subprocess.getstatusoutput(cmd)
with open("jupytext_logfile.txt", "w") as f:
    f.write(
        (f"{sys.argv}\n" f"{status1}\n" f"{output1}\n" f"{status2}\n" f"{output2}\n")
    )
sys.exit(0)

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:

repos
-   repo: local
    hooks:
    - id: jupytext
      name: jupytext
      entry: python a301/scripts/pre_commit_jupytext.py
      language: system

but I think smoothing this a little with a command line option would be a nice addition.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:23 (19 by maintainers)

github_iconTop GitHub Comments

1reaction
mwoutscommented, Oct 14, 2020

Thank you @KwatME for your kind words. Well you may be interested in the jupytext.read(s)/write(s) methods. They work similarly to nbformat.read(s)/write(s) (which you could use in place of json.load and json.dump to open/save notebook files).

1reaction
KwatMDPhDcommented, Oct 14, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jupytext documentation
Jupytext is a plugin for Jupyter that can save Jupyter notebooks as ... package to run pre-commit scripts like black and flake8 on...
Read more >
Supported hooks
cheetah-reorder-imports - This hook reorders imports in cheetah files. cheetah-flake - Lint cheetah code using flake8 and some other checks.
Read more >
jupytext
On the command line. jupytext converts Jupyter notebooks to their text ... jupytext --from ipynb --pipe black --check flake8 --pre-commit ...
Read more >
Stylising your Python code: An introduction to linting and ...
It should be noted that Flake8 does not, by default, look for as many PEP8 violations as Pylint (unless you install some plugins)....
Read more >
Pre-Commit — Using Automation To Support Quality Assured ...
Writing pre-commit scripts can be a complicated affair, but the pre-commit Python package provides a much simpler and more powerful framework for writing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found