Skip to content

Parse Jupyter notebook cell outputs and replace user paths "/home/user/..." with links relative to the project root.

License

Notifications You must be signed in to change notification settings

bdice/nb-strip-paths

Repository files navigation

nb-strip-paths

This is a pre-commit hook that strips user paths from Jupyter notebooks. For example:

# When executing from a repository "my_awesome_code"
/home/bdice/my_awesome_code/some_directory/file.txt
# becomes...
some_directory/file.txt

This makes cleaner (and safer) examples for projects with tutorials that deal with filesystems and must print absolute paths. Created for use with signac-examples.

Much of this project's code is copied from nbqa under the MIT license. nbqa is a wonderful tool for using Python linters/formatters with Jupyter notebooks, and I strongly recommend trying it!

Usage

Add this snippet to .pre-commit-config.yaml:

  - repo: https://github.com/bdice/nb-strip-paths
    rev: 'v0.1.0'
    hooks:
      - id: nb-strip-paths

Tests

The tests require nbconvert and nbstripout.

For now, there is only a regression test. The output must be verified manually for correctness if committing changes to the test file.

To run the tests, execute:

cd tests
./run_tests.sh

If the test notebook is updated, execute:

cd tests
./generate_reference_notebooks.sh

and verify the notebook looks correct before committing.

About

Parse Jupyter notebook cell outputs and replace user paths "/home/user/..." with links relative to the project root.

Resources

License

Stars

Watchers

Forks

Packages

No packages published