Code Monkey home page Code Monkey logo

pre-commit-jupyter's Introduction

pre-commit hook to remove cell output of .ipynb notebook and some metadata for better security.

Sample config:

repos:
  - repo: https://github.com/roy-ht/pre-commit-jupyter
    rev: v1.2.1
    hooks:
      - id: jupyter-notebook-cleanup
        args:
          # - --remove-kernel-metadata
          - --pin-patterns
          - "[pin];[donotremove]"

If you have "pin patterns", You can keep cell outputs like that:

# [pin]
some_function()
print("some info")
# [donotremove]
some_function()
print("some info")

pre-commit-jupyter's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

pre-commit-jupyter's Issues

pre-commit hook fails

Hey, thanks for this pre-commit hook, it looks really useful!

Unfortunately I haven't been able to get it working. If I run pre-commit run --all-files it does actually seem to execute (all of my .ipynb files get changed), but the task returns a "Failed" state without any explanation for the failure:

jupyter-notebook-cleanup.................................................Failed
- hook id: jupyter-notebook-cleanup
- files were modified by this hook

So even though the changes have been make the commit will not go through since a failed state has been returned.

Failes with json.decode Expecting ',' error

Just trying it out. I used the default configuration

  - repo: https://github.com/aflc/pre-commit-jupyter
  ¦ rev: v1.1.0
  ¦ hooks:
  ¦ ¦ - id: jupyter-notebook-cleanup
  ¦ ¦ ¦ args:
  ¦ ¦ ¦ ¦ - --remove-kernel-metadata
  ¦ ¦ ¦ ¦ - --pin-patterns
  ¦ ¦ ¦ ¦ - "[pin];[donotremove]"
  ¦ ¦ ¦ files: \.ipynb$
  ¦ ¦ ¦ types: ["file"]

Judging by the output it's the cleanup line in remove_output_file.

 File "/Users/rich/.cache/pre-commit/repotx7s5n95/py_env-python3/lib/python3.8/site-packages/jupyter_notebook_cleanup/cli.py", line 27, in main
    remove_output_file(
  File "/Users/rich/.cache/pre-commit/repotx7s5n95/py_env-python3/lib/python3.8/site-packages/jupyter_notebook_cleanup/cli.py", line 49, in remove_output_file
    data = json.load(f, object_pairs_hook=OrderedDict)

pre-commit hook fails, version 3.9 issue

Not quite sure what is going on, but could it be a v3.9 issue and it looks like it is filling in json.load of the temple in line 49

jupyter-notebook-cleanup.................................................Failed
- hook id: jupyter-notebook-cleanup
- exit code: 1
- files were modified by this hook

Traceback (most recent call last):
  File "/Users/rich/.cache/pre-commit/repogf80rbub/py_env-python3/bin/jupyter-notebook-cleanup", line 8, in <module>
    sys.exit(main())
  File "/Users/rich/.cache/pre-commit/repogf80rbub/py_env-python3/lib/python3.9/site-packages/jupyter_notebook_cleanup/cli.py", line 27, in main
    remove_output_file(
  File "/Users/rich/.cache/pre-commit/repogf80rbub/py_env-python3/lib/python3.9/site-packages/jupyter_notebook_cleanup/cli.py", line 49, in remove_output_file
    data = json.load(f, object_pairs_hook=OrderedDict)
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):
  File "/Users/rich/.cache/pre-commit/repogf80rbub/py_env-python3/bin/jupyter-notebook-cleanup", line 8, in <module>
    sys.exit(main())
  File "/Users/rich/.cache/pre-commit/repogf80rbub/py_env-python3/lib/python3.9/site-packages/jupyter_notebook_cleanup/cli.py", line 27, in main
    remove_output_file(
  File "/Users/rich/.cache/pre-commit/repogf80rbub/py_env-python3/lib/python3.9/site-packages/jupyter_notebook_cleanup/cli.py", line 49, in remove_output_file
    data = json.load(f, object_pairs_hook=OrderedDict)
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):
  File "/Users/rich/.cache/pre-commit/repogf80rbub/py_env-python3/bin/jupyter-notebook-cleanup", line 8, in <module>
    sys.exit(main())
  File "/Users/rich/.cache/pre-commit/repogf80rbub/py_env-python3/lib/python3.9/site-packages/jupyter_notebook_cleanup/cli.py", line 27, in main
    remove_output_file(
  File "/Users/rich/.cache/pre-commit/repogf80rbub/py_env-python3/lib/python3.9/site-packages/jupyter_notebook_cleanup/cli.py", line 49, in remove_output_file
    data = json.load(f, object_pairs_hook=OrderedDict)
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/[email protected]/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.