pax_global_header00006660000000000000000000000064147544000030014507gustar00rootroot0000000000000052 comment=ad72c7f444c3dc8cbc23fe4f9dd9f5a2b4394796 python-nbstripout-0.8.1/000077500000000000000000000000001475440000300152455ustar00rootroot00000000000000python-nbstripout-0.8.1/.github/000077500000000000000000000000001475440000300166055ustar00rootroot00000000000000python-nbstripout-0.8.1/.github/workflows/000077500000000000000000000000001475440000300206425ustar00rootroot00000000000000python-nbstripout-0.8.1/.github/workflows/tests.yml000066400000000000000000000022411475440000300225260ustar00rootroot00000000000000name: nbstripout on: [push, pull_request, workflow_dispatch] jobs: test: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] steps: - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | python -m pip install wheel python -m pip install -r tests/requirements.txt python -m pip install . - name: Configure Git run: git config --global init.defaultBranch main - name: Run tests (Linux) if: matrix.os == 'ubuntu-latest' shell: bash run: | pytest - name: Run tests (MacOS) if: matrix.os == 'macos-latest' shell: bash run: | pytest - name: Run tests (Windows) if: matrix.os == 'windows-latest' shell: bash env: NBSTRIPOUT_EXE: ${{ env.pythonLocation }}\Scripts\nbstripout.exe run: | git config --global core.autocrlf true pytest python-nbstripout-0.8.1/.gitignore000066400000000000000000000040561475440000300172420ustar00rootroot00000000000000# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ share/python-wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .nox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover *.py,cover .hypothesis/ .pytest_cache/ cover/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 db.sqlite3-journal # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder .pybuilder/ target/ # Jupyter Notebook .ipynb_checkpoints # IPython profile_default/ ipython_config.py # pyenv # For a library or package, you might want to ignore these files since the code is # intended to run in multiple environments; otherwise, check them in: # .python-version # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. # However, in case of collaboration, if having platform-specific dependencies or dependencies # having no cross-platform support, pipenv may install dependencies that don't work, or not # install all needed dependencies. #Pipfile.lock # PEP 582; used by e.g. github.com/David-OConnor/pyflow __pypackages__/ # Celery stuff celerybeat-schedule celerybeat.pid # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ .dmypy.json dmypy.json # Pyre type checker .pyre/ # pytype static type analyzer .pytype/ # Cython debug symbols cython_debug/ # tmp files *~ *.sw[op] # PyCharm .idea # VSCode .vscodepython-nbstripout-0.8.1/.pre-commit-hooks.yaml000066400000000000000000000002671475440000300214110ustar00rootroot00000000000000- id: nbstripout name: nbstripout description: 'nbstripout: strip output from Jupyter and IPython notebooks' entry: nbstripout language: python types: [jupyter] python-nbstripout-0.8.1/CHANGELOG.md000066400000000000000000000223361475440000300170640ustar00rootroot00000000000000# Changelog ## 0.8.1 - 2024-11-17 - In dry run mode, only print output if there would have been a change (#152). - Declare git filter to be `required` when installing, such that it *must* succeed (#191). ## 0.8.0 - 2024-11-03 - Adds `--verify` flag, similar to `--dry-run` but returning 1 if any affected files would have changed (@jspaezp, #153, #195). - Adds script to apply `nbstripout` retroactively using `git-filter-repo` to README (@LunarLanding @tokleine, #194 #197). - Documents nbstripout on all files in the current directory and subdirectories recursively (#127). - Accepts notebooks without `nbformat` version specified. - Improves test coverage. ## 0.7.1 - 2024-02-04 - Fix regression where input file was truncated before reading (#190). ## 0.7.0 - 2024-02-04 - **This release has been yanked from PyPI due to a major regression (#190).** - Drop support for Python 3.7 (end of life 2023-06-27), require Python 3.8. - Add support for Python 3.12. - Drop backwards compability with IPython.nbformat for IPython <4. - Rename cell ids to be sequential by default. Disable by passing `--keep-id` (@JasonJooste, #184). - Improve documentation for notebook and cell metadata stripping (#187). - Switch from pytest-flake8 to pytest-ruff. - Convert all text assets from rST to Markdown format. ## 0.6.2 - 2024-02-03 - Add `--python` option for `nbstripout --install` to allow overriding the Python interpreter specified in `.git/config` (@nobodyinperson, #181 #182). - Add option `--keep-metadata-keys` to keep specific metadata keys that are stripped by default (@davidxia, #78 #177). - Replace [Cram](https://bitheap.org/cram/) as test runner for integration tests with a custom framework which also supports testing on Windows (@arobrien, #176 #178). - Use `SystemExit` instead of `sys.exit` and do not exit from functions (@janosh, #173). ## 0.6.1 - 2022-09-24 - Removed `setup_requires` and `tests_require` and no longer rely on `pytest-runner` for test execution but invoke `pytest` directly (#168). ## 0.6.0 - 2022-07-24 - Support for stripping init cells (@Pugio, #157). - Added `--drop-tagged-cells="some tags"` option (@boun, #161). - Renamed `--strip-empty-cells` to `--drop-empty-cells`. - Dropped support for Python 3.5, added support for Python 3.10. ## 0.5.0 - 2021-06-28 - Support only stripping outputs larger than a given size (@cjblocker, #135). - Support stripping output from Zeppelin Notebooks (@ankitrokdeonsns, #130). - Switch CI to GitHub actions (#151). - Support attributes file without leading path component (#155). ## 0.4.0 - 2021-04-25 - Add support for system wide installation, `--system` flag (@PLPeeters, #149). - Use `~` instead of `$HOME` for config dir (#136). - Document stripping kernelspec (#141). - Add support for removing empty cells (#131). - Create directory for attributes file if needed (#139). - Add support for stripping metadata keys containing periods (@baldwint, #143). - Strip collapsible headings by default (@rpytel1, #142). ## 0.3.10 - 2021-04-24 - Python 2.7 only release, to make `pip install nbstripout` work in Python 2.7. Previously, this was picking up 0.3.8 which is *not* Python 2.7 compatible. - Drop Python 3.4 support, add support for Python 3.7, 3.8. - Windows compatibility: `""` quote Python interpreter path (@fcollonval, #115). - Add `--dry-run` flag (#122). - Support specifying `keep_output` as a cell tag (@scottcode, #117). ## 0.3.9 - 2020-06-28 - Document Python 3 support only. Fail to install on Python 2 (@casperdcl, #128) - Drop support for Python 3.4 (end of life 2019-03-18). - Ignore warnings from `nbformat.{read,write}`. - Support nbformat 2 notebooks without cell metadata. - Add `--extra-keys` flag to pass extra keys to strip (#119). - Apply pre-commit hook to files of type Jupyter. ## 0.3.8 - 2020-06-06 - Drop Python 2 support. - Windows compatibility: `""` quote Python interpreter path (@fcollonval, #115). - Add `--dry-run` flag (#122). - Support specifying `keep_output` as a cell tag (@scottcode, #117). - Improved error handling for the case where git is not installed (#124). - Nicer error message when input file is not found. - Use universal newlines without conversion (@ooiM, #110, #126). - Strip execution timing from cell metadata (#118). - Document which metadata is stripped by default. - Make `--global` commands work outside of git repository (#123). ## 0.3.7 - 2020-01-05 - Notebook-level `keep_output` (@jonashaag, #112). - Fix quoting of Python path and call module entrypoint (@jonashaag, #111). - Do not run `git add` in pre-commit hook (@SimonBiggs, #106). - Troubleshooting instructions (#65). - Exclusion instructions for folders (@jraviotta, #104). - Only remove `filter.nbstripout.{clean,smudge}` on `--uninstall`. - Remove unnecessary `filter.nbstripout.required` config setting. - pre-commit configuration (@Ohjeah, #79). ## 0.3.6 - 2019-07-18 - Document global installation in README (#100). - Document how to exclude folders in README (#99). - Expand `~` when looking up attributes file. - Add `--global` flag for `--install` / `--uninstall` to write the filter config to `~/.gitconfig` (#98). ## 0.3.5 - 2019-04-02 - Make nbstripout package executable and fix regression (#94). - Add package docstring. ## 0.3.4 - 2019-03-26 - Fix `WindowsError` not defined on POSIX systems (#90). - Add support for blacklisting custom metadata fields (@casperdcl, #92). ## 0.3.3 - 2018-08-04 - Distribute tests in source package (@jluttine, #73 #76). - Fix git diff tests for newer Git versions (@jluttine, #74 #76). - Install full path for diff.ipynb.textconv (@ibressler, #68 #82). - Make sure sys.stdin is not None before reading from it (@ibressler, #68 #82). ## 0.3.2 - 2018-07-09 - Gracefully deal with empty/malformed input (#66). - Add Code of Conduct (#63). - Add MANIFEST.in (#64). - Document `git filter-branch` use case in README (@belteshassar, #28). - Flush output when using `-t` (@tnilanon, #67). - Add `nbformat` and `setuptools >= 30` to `setup_requires` (@tnilanon, #67). - Use `travis_retry` (@tnilanon, #67). - Drop support for Python 3.3 (no longer supported by setuptools). ## 0.3.1 - 2017-07-30 - Add option `-t`/`--textconv` to write to stdout e.g. for use as diff filter (@utsekaj42, #53). - Flush output stream after write (@reidpr, #55). - Add options `--keep-count` and `--keep-output` to no strip execution counts and output (@jpeacock29, #56). - Fix shell pipeline documentation (@psthomas, #59). - Catch `WindowsError` when `git` is not found in PATH (@bdforbes, #62). ## 0.3.0 - 2017-02-23 - Support whitespace in repository paths (@ehoepfner, #47 #48). - Also ignore `collapsed` and `scrolled` metadata (#34). - Define `NO_CONVERT` for IPython <3 import (#46). ## 0.2.9 - 2016-11-23 - Strip `ExecuteTime` metadata (@jdriordan, #34 #39). - Fix Python 3.5 bug: open attributes file only once (#40). - Do not add blank line at beginning of attribute file. - Strip widget state from notebook metadata (#42). ## 0.2.8 - 2016-09-19 - Drop support for Python 2.6, 3.2. - Add pip install instructions to README (@oogali, #32). - Write trailing newline to attributes file (#36). - Uninstall only removes ipynb filter (#37). ## 0.2.7 - 2016-07-30 - If you set either the `"init_cell": true` or `"keep_output": true` in the cell metadata, then these cells will not be stripped out. The former works in conjunction with the `init_cell` nbextension (@mforbes, #17). - Fix encoding for Python 2 + 3 (#11). - Add `--is-installed` and `--status` options (#29). - Normalise cell output style, setting `scroll` and `collapsed` to False (@kdmurray91, #30). - Add screencast (#31). ## 0.2.6 - 2016-03-13 - Use pytest-cram (@mforbes, #22). - Add further shields to README. - Use argparse for argument parsing. - Add `--attributes` option to specify attributes file (#25). ## 0.2.5 - 2016-03-03 - Python 3 compatibility (@boeddeker, #16 #21). - Windows compatibility (@tt293, #18). - Add support for appveyor (#24). ## 0.2.4 - 2016-02-15 - Add `__version__` and `version` command (#12). - Add bumpversion config. - Add contributing guidelines (#13). ## 0.2.3 - 2016-02-15 - Use UTF8 writer for stdout and regression test (@geggo, #11). - Minor testing fixes. ## 0.2.2 - 2016-02-04 - Add uninstall task (#8). - Minor testing fixes. ## 0.2.1 - 2016-01-27 - Add Travis CI setup (#4). - Call decode on `git_dir` (@michaelaye, #5). - Add unit tests via Cram (@mforbes). ## 0.2.0 - 2016-01-24 - Only process .ipynb files unless -f flag is used (@mforbes). - Process multiple files (@mforbes). - Add MIT License (@mforbes). ## 0.1.0 - not released - Based on Min RK's original [gist](https://gist.github.com/minrk/6176788) but supports multiple versions of IPython/Jupyter and also strips the execution count. - Add install option that fails sensibly if not in a git repository, does not clobber an existing attributes file and checks for an existing ipynb filter. - Works with both files and stdin / stdout. - Add README and documentation. - Add setup.py with script entry point. python-nbstripout-0.8.1/CODE_OF_CONDUCT.md000066400000000000000000000062431475440000300200510ustar00rootroot00000000000000# Contributor Covenant Code of Conduct ## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. ## Our Standards Examples of behavior that contributes to creating a positive environment include: * Using welcoming and inclusive language * Being respectful of differing viewpoints and experiences * Gracefully accepting constructive criticism * Focusing on what is best for the community * Showing empathy towards other community members Examples of unacceptable behavior by participants include: * The use of sexualized language or imagery and unwelcome sexual attention or advances * Trolling, insulting/derogatory comments, and personal or political attacks * Public or private harassment * Publishing others' private information, such as a physical or electronic address, without explicit permission * Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. ## Scope This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at florian.rathgeber+nbstripout@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] [homepage]: http://contributor-covenant.org [version]: http://contributor-covenant.org/version/1/4/ python-nbstripout-0.8.1/CONTRIBUTING.md000066400000000000000000000016051475440000300175000ustar00rootroot00000000000000# How to contribute Development of nbstripout happens on [GitHub](https://github.com/kynan/nbstripout) - [bug reports](https://github.com/kynan/nbstripout/issues) and [pull requests](https://github.com/kynan/nbstripout/pulls) welcome! # Releasing a new version To simplify updating the version number consistently across different files and creating the appropriate annotated tag, we use [bump-my-version](https://github.com/callowayproject/bump-my-version). For a new patch release, run bump-my-version bump patch and for a minor release, run bump-my-version bump minor Remember to also push the release tag with `git push --tags`. Use [twine](https://twine.readthedocs.io/en/latest/#using-twine) to upload the new release to PyPI: python -m build twine check dist/nbstripout-0.8.1* twine upload -r testpypi dist/nbstripout-0.8.1* twine upload dist/nbstripout-0.8.1* python-nbstripout-0.8.1/LICENSE.txt000066400000000000000000000021361475440000300170720ustar00rootroot00000000000000Copyright (c) 2015 Min RK, Florian Rathgeber, Michael McNeil Forbes 2019 Casper da Costa-Luis Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. python-nbstripout-0.8.1/MANIFEST.in000066400000000000000000000001201475440000300167740ustar00rootroot00000000000000# Include the license file include LICENSE.txt # Include tests include tests/* python-nbstripout-0.8.1/README.md000066400000000000000000000440011475440000300165230ustar00rootroot00000000000000[![tests](https://github.com/kynan/nbstripout/actions/workflows/tests.yml/badge.svg)](https://github.com/kynan/nbstripout/actions/workflows/tests.yml) [![downloads](https://img.shields.io/pypi/dm/nbstripout)](https://pypi.org/project/nbstripout) [![PyPI version](https://img.shields.io/pypi/v/nbstripout)](https://pypi.org/project/nbstripout) [![conda-forge version](https://img.shields.io/conda/vn/conda-forge/nbstripout)](https://anaconda.org/conda-forge/nbstripout) [![supported Python versions](https://img.shields.io/pypi/pyversions/nbstripout)](https://pypi.org/project/nbstripout) [![Python package formats](https://img.shields.io/pypi/format/nbstripout)](https://pypi.org/project/nbstripout) [![license](https://img.shields.io/pypi/l/nbstripout)](https://raw.githubusercontent.com/kynan/nbstripout/main/LICENSE.txt) [![GitHub stars](https://img.shields.io/github/stars/kynan/nbstripout?style=social)](https://github.com/kynan/nbstripout/stargazers) [![GitHub forks](https://img.shields.io/github/forks/kynan/nbstripout?style=social)](https://github.com/kynan/nbstripout/network/members) # nbstripout: strip output from Jupyter and IPython notebooks Reads a notebook from a file or stdin, strips output and some metadata, and writes the "cleaned" version of the notebook to the original file or stdout. Intended to be used as a Git filter or pre-commit hook for users who don't want to track output in Git. Roughly equivalent to the "Clear All Output" command in the notebook UI, but only "visible" to Git: keep your output in the file on disk, but don't commit the output to Git. This helps minimizing diffs and reduce file size. Originally based on . ## Python 3 only As of version 0.4.0, nbstripout supports Python 3 *only*. If you need to use Python 2.7, install nbstripout 0.3.10: pip install nbstripout==0.3.10 ## Screencast This screencast demonstrates the use and working principles behind the nbstripout utility and how to use it as a Git filter: [![image](https://i.imgur.com/7oQHuJ5.png)](https://www.youtube.com/watch?v=BEMP4xacrVc) ## Installation You can download and install the latest version of `nbstripout` from the Python package index [PyPI](https://pypi.org/project/nbstripout/) as follows: pip install --upgrade nbstripout When using the [Anaconda](https://www.anaconda.com/download) Python distribution, install `nbstripout` via the [conda](https://docs.conda.io) package manager from [conda-forge](https://conda-forge.org): conda install -c conda-forge nbstripout ## Usage Strip output from IPython / Jupyter / Zeppelin notebook (modifies the file in-place): nbstripout FILE.ipynb [FILE2.ipynb ...] nbstripout FILE.zpln Force processing of non `.ipynb` files: nbstripout -f FILE.ipynb.bak For using Zeppelin mode while processing files with other extensions use: nbstripout -m zeppelin -f Write to stdout e.g. to use as part of a shell pipeline: cat FILE.ipynb | nbstripout > OUT.ipynb cat FILE.zpln | nbstripout -m zeppelin > OUT.zpln or nbstripout -t FILE.ipynb | other-command Do a dry run and only list which files would have been stripped: nbstripout --dry-run FILE.ipynb [FILE2.ipynb ...] or Do a verification run, which works like dry run but will fail if any files would have been stripped: nbstripout --verify FILE.ipynb [FILE2.ipynb ...] Operate on all `.ipynb` files in the current directory and subdirectories recursively: find . -name '*.ipynb' -exec nbstripout {} + Print the version: nbstripout --version Show help and usage instructions: nbstripout --help ### Using as a Git filter Set up the [git filter](https://git-scm.com/docs/gitattributes#_filter) and attributes as described in the manual installation instructions below: nbstripout --install Note: The filter is declared as `required`, meaning the filter *must* succeed. Failures and misconfigurations will not simply cause the filter to be ignored. Set up the git filter using `.gitattributes`: nbstripout --install --attributes .gitattributes Specify a different path to the Python interpreter to be used for the git filters (default is the path to the Python interpreter used when `nbstripout` is installed). This is useful if you have Python installed in different or unusual locations across machines, e.g. `/usr/bin/python3` on your machine vs `/usr/local/bin/python3` in a container or elsewhere. nbstripout --install --python python3 Using just `python3` lets each machine find its Python itself. However, keep in mind that depending on your setup this might not be the Python version you want or even fail because an absolute path is required. Set up the git filter in your global `~/.gitconfig`: nbstripout --install --global Set up the git filter in your system-wide `$(prefix)/etc/gitconfig` (most installations will require you to `sudo`): [sudo] nbstripout --install --system Remove the git filter and attributes: nbstripout --uninstall Remove the git filter from your global `~/.gitconfig` and attributes: nbstripout --uninstall --global Remove the git filter from your system-wide `$(prefix)/etc/gitconfig` and attributes: [sudo] nbstripout --uninstall --system Remove the git filter and attributes from `.gitattributes`: nbstripout --uninstall --attributes .gitattributes Check if `nbstripout` is installed in the current repository (exits with code 0 if installed, 1 otherwise): nbstripout --is-installed Print status of `nbstripout` installation in the current repository and configuration summary of filter and attributes if installed (exits with code 0 if installed, 1 otherwise): nbstripout --status ### Configuration files The following table shows in which files the `nbstripout` filter and attribute configuration is written to for given extra flags to `--install` and `--uninstall`: | flags | filters | attributes | | ---------------------------------------- | --------------------------- | ------------------------------- | | none | `.git/config` | `.git/info/attributes` | | `--global` | `~/.gitconfig` | `~/.config/git/attributes` | | `--system` | `$(prefix)/etc/gitconfig` | `$(prefix)/etc/gitattributes` | | `--attributes=.gitattributes` | `.git/config` | `.gitattributes` | | `--global --attributes=.gitattributes` | `~/.gitconfig` | `.gitattributes` | ### Install globally Usually, `nbstripout` is installed per repository so you can choose where to use it or not. You can choose to set the attributes in `.gitattributes` and commit this file to your repository, however there is no way to have git set up the filters automatically when someone clones a repository. This is by design, to prevent you from executing arbitrary and potentially malicious code when cloning a repository. To install `nbstripout` for all your repositories such that you no longer need to run the installation once per repository, install as follows: mkdir -p ~/.config/git # This folder may not exist nbstripout --install --global --attributes=~/.config/git/attributes This will set up the filters and diff driver in your `~/.gitconfig` and instruct git to apply them to any `.ipynb` file in any repository. Note that you need to uninstall with the same flags: nbstripout --uninstall --global --attributes=~/.config/git/attributes ### Install system-wide To install `nbstripout` system-wide so that it applies to all repositories for all users, install as follows (most installations will require you to `sudo`): [sudo] nbstripout --install --system This will set up the filters and diff driver in `$(prefix)/etc/gitconfig` and instruct git to apply them to any `.ipynb` file in any repository for any user. Note that you need to uninstall with the same flags: [sudo] nbstripout --uninstall --system ### Apply retroactively `nbstripout` can be used to rewrite an existing Git repository using [`git filter-repo`](https://github.com/newren/git-filter-repo) to strip output from existing notebooks. This invocation operates on all ipynb files in the repo: ```sh #!/usr/bin/env bash # get lint-history with callback from https://github.com/newren/git-filter-repo/pull/542 ./lint-history.py --relevant 'return filename.endswith(b".ipynb")' --callback ' import json, warnings, nbformat from nbstripout import strip_output from nbformat.reader import NotJSONError try: with warnings.catch_warnings(): warnings.simplefilter("ignore", category=UserWarning) notebook = nbformat.reads(blob.data, as_version=nbformat.NO_CONVERT) # customize to your needs strip_output(notebook, keep_output=False, keep_count=False, keep_id=False, extra_keys=["metadata.widgets","metadata.execution","cell.attachments"], drop_empty_cells=True, drop_tagged_cells=[],strip_init_cells=False, max_size=0) old_len = len(blob.data) blob.data = (nbformat.writes(notebook) + "\n").encode("utf-8") if old_len != len(blob.data): print(change.blob_id, change.filename, old_len, len(blob.data)) except NotJSONError as e: print("ERROR", type(e), change.blob_id, change.filename) ' ``` ### Removing empty cells Drop empty cells i.e. cells where `source` is either empty or only contains whitespace: nbstripout --drop-empty-cells ### Removing [init]{.title-ref} cells By default `nbstripout` will keep cells with `init_cell: true` metadata. To disable this behavior use: nbstripout --strip-init-cells ### Removing entire cells In certain conditions it might be handy to remove not only the output, but the entire cell, e.g. when developing exercises. To drop all cells tagged with "solution" run: nbstripout --drop-tagged-cells="solution" The option accepts a list of tags separated by whitespace. ### Keeping some output Do not strip the execution count/prompt number: nbstripout --keep-count Do not strip outputs that are smaller that a given max size (useful for removing only large outputs like images): nbstripout --max-size 1k Do not strip the output, only metadata: nbstripout --keep-output Do not reassign the cell ids to be sequential (which is the default behavior): nbstripout --keep-id To mark special cells so that the output is not stripped, you can either: 1. Set the `keep_output` tag on the cell. To do this, enable the tags toolbar (View > Cell Toolbar > Tags) and then add the `keep_output` tag for each cell you would like to keep the output for. 2. Set the `"keep_output": true` metadata on the cell. To do this, select the "Edit Metadata" Cell Toolbar, and then use the "Edit Metadata" button on the desired cell to enter something like: { "keep_output": true, } You can also keep output for an entire notebook. This is useful if you want to strip output by default in an automated environment (e.g. CI pipeline), but want to be able to keep outputs for some notebooks. To do so, add the option above to the *notebook* metadata instead. (You can also explicitly remove outputs from a particular cell in these notebooks by adding a cell-level metadata entry.) Another use-case is to preserve initialization cells that might load customized CSS etc. critical for the display of the notebook. To support this, we also keep output for cells with: { "init_cell": true, } This is the same metadata used by the [init_cell nbextension](https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tree/master/src/jupyter_contrib_nbextensions/nbextensions/init_cell). ### Stripping metadata The following metadata is stripped by default: - Notebook metadata: `signature`, `widgets` - Cell metadata: `ExecuteTime`, `collapsed`, `execution`, `heading_collapsed`, `hidden`, `scrolled` Additional metadata to be stripped can be configured via either - `git config (--global/--system) filter.nbstripout.extrakeys`, e.g. : git config --global filter.nbstripout.extrakeys ' metadata.celltoolbar metadata.kernelspec metadata.language_info.codemirror_mode.version metadata.language_info.pygments_lexer metadata.language_info.version metadata.toc metadata.notify_time metadata.varInspector cell.metadata.heading_collapsed cell.metadata.hidden cell.metadata.code_folding cell.metadata.tags cell.metadata.init_cell' - the `--extra-keys` flag, which takes a space-delimited string as an argument, e.g. : --extra-keys="metadata.celltoolbar cell.metadata.heading_collapsed" Note: Only notebook and cell metadata is currently supported and every key specified via `filter.nbstripout.extrakeys` or `--extra-keys` must start with `metadata.` for notebook and `cell.metadata.` for cell metadata. You can keep certain metadata that would be stripped by default with either - `git config (--global/--system) filter.nbstripout.keepmetadatakeys`, e.g.: git config --global filter.nbstripout.keepmetadatakeys ' cell.metadata.collapsed cell.metadata.scrolled' - the `--keep-metadata-keys` flag, which takes a space-delimited string as an argument, e.g.: --keep-metadata-keys="cell.metadata.collapsed cell.metadata.scrolled" Note: Previous versions of Jupyter used `metadata.kernel_spec` for kernel metadata. Prefer stripping `kernelspec` entirely: only stripping some attributes inside `kernelspec` may lead to errors when opening the notebook in Jupyter (see [#141](https://github.com/kynan/nbstripout/issues/141)). ### Excluding files and folders To exclude specific files or folders from being processed by the `nbstripout` filters, add the path and exception to your filter specifications defined in `.git/info/attributes` or `.gitattributes`: docs/** filter= diff= This will disable `nbstripout` for any file in the `docs` directory.: notebooks/Analysis.ipynb filter= diff= This will disable `nbstripout` for the file `Analysis.ipynb` located in the `notebooks` directory. To check which attributes a given file has with the current config, run: git check-attr -a -- path/to/file For a file to which the filter applies you will see the following: $ git check-attr -a -- foo.ipynb foo.ipynb: diff: ipynb foo.ipynb: filter: nbstripout For a file in your excluded folder you will see the following: $ git check-attr -a -- docs/foo.ipynb foo.ipynb: diff: foo.ipynb: filter: ## Manual filter installation Set up a git filter and diff driver using nbstripout as follows: git config filter.nbstripout.clean '/path/to/nbstripout' git config filter.nbstripout.smudge cat git config filter.nbstripout.required true git config diff.ipynb.textconv '/path/to/nbstripout -t' This will add a section to the `.git/config` file of the current repository. If you want the filter to be installed globally for your user, add the `--global` flag to the `git config` invocations above to have the configuration written to your `~/.gitconfig` and apply to all repositories. If you want the filter to be installed system-wide, add the `--system` flag to the `git config` invocations above to have the configuration written to `$(prefix)/etc/gitconfig` and apply to all repositories for all users. Create a file `.gitattributes` (if you want it versioned with the repository) or `.git/info/attributes` (to apply it only to the current repository) with the following content: *.ipynb filter=nbstripout *.ipynb diff=ipynb This instructs git to use the filter named `nbstripout` and the diff driver named `ipynb` set up in the git config above for every `.ipynb` file in the repository. If you want the attributes be set for `.ipynb` files in any of your git repositories, add those two lines to `~/.config/git/attributes`. Note that this file and the `~/.config/git` directory may not exist. If you want the attributes be set for `.ipynb` files in any git repository on your system, add those two lines to `$(prefix)/etc/gitattributes`. Note that this file may not exist. ## Using `nbstripout` as a pre-commit hook [pre-commit](https://pre-commit.com) is a framework for managing git [pre-commit hooks](https://git-scm.com/docs/githooks). Once you have [pre-commit](https://pre-commit.com) installed, add the following to the `.pre-commit-config.yaml` in your repository: repos: - repo: https://github.com/kynan/nbstripout rev: 0.8.1 hooks: - id: nbstripout Then run `pre-commit install` to activate the hook. > [!WARNING] > > In this mode, `nbstripout` is used as a git hook to strip any `.ipynb` files > before committing. This also modifies your working copy! > > In its regular mode, `nbstripout` acts as a filter and only modifies what git > gets to see for committing or diffing. The working copy stays intact. ## Troubleshooting ### Known issues Certain Git workflows are not well supported by `nbstripout`: - Local changes to notebook files that are made invisible to Git due to the `nbstripout` filter do still cause conflicts when attempting to sync upstream changes (`git pull`, `git merge` etc.). This is because Git has no way of resolving a conflict caused by a non-stripped local file being merged with a stripped upstream file. Addressing this issue is out of scope for `nbstripout`. Read more and find workarounds in [#108](https://github.com/kynan/nbstripout/issues/108). ### Show files processed by nbstripout filter Git has [no builtin support](https://stackoverflow.com/a/52065333/396967) for listing files a clean or smudge filter operates on. As a workaround, change the setup of your filter in `.git/config`, `~/.gitconfig` or `$(prefix)/etc/gitconfig` as follows to see the filenames either filter operates on: [filter "nbstripout"] clean = "f() { echo >&2 \"clean: nbstripout $1\"; nbstripout; }; f %f" smudge = "f() { echo >&2 \"smudge: cat $1\"; cat; }; f %f" required = true python-nbstripout-0.8.1/nbstripout/000077500000000000000000000000001475440000300174565ustar00rootroot00000000000000python-nbstripout-0.8.1/nbstripout/__init__.py000066400000000000000000000004251475440000300215700ustar00rootroot00000000000000from ._nbstripout import install, uninstall, status, main, __doc__ as docstring from ._utils import pop_recursive, strip_output, MetadataError __all__ = ["install", "uninstall", "status", "main", "pop_recursive", "strip_output", "MetadataError"] __doc__ = docstring python-nbstripout-0.8.1/nbstripout/__main__.py000066400000000000000000000000441475440000300215460ustar00rootroot00000000000000from nbstripout import main main() python-nbstripout-0.8.1/nbstripout/_nbstripout.py000066400000000000000000000527331475440000300224120ustar00rootroot00000000000000""" Strip output from Jupyter and IPython notebooks =============================================== Opens a notebook, strips its output, and writes the outputless version to the original file. Useful mainly as a git filter or pre-commit hook for users who don't want to track output in VCS. This does mostly the same thing as the `Clear All Output` command in the notebook UI. Usage ===== Strip output from IPython / Jupyter / Zeppelin notebook (modifies the file in-place): :: nbstripout nbstripout By default, nbstripout will only modify files ending in '.ipynb' or '.zpln', to process other files us the '-f' flag to force the application. nbstripout -f For using Zeppelin mode while processing files with other extensions use: nbstripout -m zeppelin -f Use as part of a shell pipeline: :: cat FILE.ipynb | nbstripout > OUT.ipynb cat FILE.zpln | nbstripout -m zeppelin > OUT.zpln Set up the git filter and attributes as described in the manual installation instructions below: :: nbstripout --install Set up the git filter using ``.gitattributes`` :: nbstripout --install --attributes .gitattributes Set up the git filter in your global ``~/.gitconfig`` :: nbstripout --install --global Set up the git filter in your system-wide ``$(prefix)/etc/gitconfig`` (most installations will require you to ``sudo``) :: [sudo] nbstripout --install --system Remove the git filter and attributes: :: nbstripout --uninstall Remove the git filter from your global ``~/.gitconfig`` and attributes :: nbstripout --uninstall --global Remove the git filter from your system-wide ``$(prefix)/etc/gitconfig`` and attributes :: nbstripout --uninstall --system Remove the git filter and attributes from ``.gitattributes``: :: nbstripout --uninstall --attributes .gitattributes Check if ``nbstripout`` is installed in the current repository (exits with code 0 if installed, 1 otherwise): :: nbstripout --is-installed Print status of ``nbstripout`` installation in the current repository and configuration summary of filter and attributes if installed (exits with code 0 if installed, 1 otherwise): :: nbstripout --status Do a dry run and only list which files would have been stripped: :: nbstripout --dry-run FILE.ipynb [FILE2.ipynb ...] Print the version: :: nbstripout --version Show this help page: :: nbstripout --help Manual filter installation ========================== Set up a git filter using nbstripout as follows: :: git config filter.nbstripout.clean '/path/to/nbstripout' git config filter.nbstripout.smudge cat Create a file ``.gitattributes`` or ``.git/info/attributes`` with: :: *.ipynb filter=nbstripout Apply the filter for git diff of ``*.ipynb`` files: :: git config diff.ipynb.textconv '/path/to/nbstripout -t' In file ``.gitattributes`` or ``.git/info/attributes`` add: :: *.ipynb diff=ipynb """ from argparse import ArgumentParser, RawDescriptionHelpFormatter import collections import copy import io import json from os import devnull, environ, makedirs, path from pathlib import PureWindowsPath import re from subprocess import call, check_call, check_output, CalledProcessError, STDOUT import sys import warnings import nbformat from nbstripout._utils import strip_output, strip_zeppelin_output __all__ = ["install", "uninstall", "status", "main"] __version__ = '0.8.1' INSTALL_LOCATION_LOCAL = 'local' INSTALL_LOCATION_GLOBAL = 'global' INSTALL_LOCATION_SYSTEM = 'system' def _get_system_gitconfig_folder(): try: git_config_output = check_output(['git', 'config', '--system', '--list', '--show-origin'], universal_newlines=True, stderr=STDOUT).strip() # If the output is empty, it means the file exists but is empty, so we cannot get the path. # To still get it, we're setting a temporary config parameter. if git_config_output == '': check_call(['git', 'config', '--system', 'filter.nbstripoutput.test', 'test']) git_config_output = check_output(['git', 'config', '--system', '--list', '--show-origin'], universal_newlines=True).strip() check_call(['git', 'config', '--system', '--unset', 'filter.nbstripoutput.test']) output_lines = git_config_output.split('\n') system_gitconfig_file_path = re.sub(r'^file:', '', output_lines[0].split('\t')[0]) except CalledProcessError as e: git_config_output = e.output system_gitconfig_file_path = re.match(r"fatal:.*file '([^']+)'.*", git_config_output).group(1) return path.abspath(path.dirname(system_gitconfig_file_path)) def _get_attrfile(git_config, install_location=INSTALL_LOCATION_LOCAL, attrfile=None): if not attrfile: if install_location == INSTALL_LOCATION_SYSTEM: try: attrfile = check_output(git_config + ['core.attributesFile'], universal_newlines=True).strip() except CalledProcessError: config_dir = _get_system_gitconfig_folder() attrfile = path.join(config_dir, 'gitattributes') elif install_location == INSTALL_LOCATION_GLOBAL: try: attrfile = check_output(git_config + ['core.attributesFile'], universal_newlines=True).strip() except CalledProcessError: config_dir = environ.get('XDG_CONFIG_DIR', path.expanduser('~/.config')) attrfile = path.join(config_dir, 'git', 'attributes') else: git_dir = check_output(['git', 'rev-parse', '--git-dir'], universal_newlines=True).strip() attrfile = path.join(git_dir, 'info', 'attributes') attrfile = path.expanduser(attrfile) if path.dirname(attrfile): makedirs(path.dirname(attrfile), exist_ok=True) return attrfile def _parse_size(num_str): num_str = num_str.upper() if num_str[-1].isdigit(): return int(num_str) elif num_str[-1] == 'K': return int(num_str[:-1]) * (10**3) elif num_str[-1] == 'M': return int(num_str[:-1]) * (10**6) elif num_str[-1] == 'G': return int(num_str[:-1]) * (10**9) else: raise ValueError(f"Unknown size identifier {num_str[-1]}") def install(git_config, install_location=INSTALL_LOCATION_LOCAL, python=None, attrfile=None): """Install the git filter and set the git attributes.""" try: filepath = f'"{PureWindowsPath(python or sys.executable).as_posix()}" -m nbstripout' check_call(git_config + ['filter.nbstripout.clean', filepath]) check_call(git_config + ['filter.nbstripout.smudge', 'cat']) check_call(git_config + ['filter.nbstripout.required', 'true']) check_call(git_config + ['diff.ipynb.textconv', filepath + ' -t']) attrfile = _get_attrfile(git_config, install_location, attrfile) except FileNotFoundError: print('Installation failed: git is not on path!', file=sys.stderr) return 1 except CalledProcessError: print('Installation failed: not a git repository!', file=sys.stderr) return 1 # Check if there is already a filter for ipynb files filt_exists = False zeppelin_filt_exists = False diff_exists = False if path.exists(attrfile): with open(attrfile, 'r') as f: attrs = f.read() filt_exists = '*.ipynb filter' in attrs zeppelin_filt_exists = '*.zpln filter' in attrs diff_exists = '*.ipynb diff' in attrs if filt_exists and diff_exists: return try: with open(attrfile, 'a', newline='') as f: # If the file already exists, ensure it ends with a new line if f.tell(): f.write('\n') if not filt_exists: print('*.ipynb filter=nbstripout', file=f) if not zeppelin_filt_exists: print('*.zpln filter=nbstripout', file=f) if not diff_exists: print('*.ipynb diff=ipynb', file=f) except PermissionError: print(f'Installation failed: could not write to {attrfile}', file=sys.stderr) if install_location == INSTALL_LOCATION_GLOBAL: print('Did you forget to sudo?', file=sys.stderr) return 1 def uninstall(git_config, install_location=INSTALL_LOCATION_LOCAL, attrfile=None): """Uninstall the git filter and unset the git attributes.""" try: call(git_config + ['--unset', 'filter.nbstripout.clean'], stdout=open(devnull, 'w'), stderr=STDOUT) call(git_config + ['--unset', 'filter.nbstripout.smudge'], stdout=open(devnull, 'w'), stderr=STDOUT) call(git_config + ['--unset', 'filter.nbstripout.required'], stdout=open(devnull, 'w'), stderr=STDOUT) call(git_config + ['--remove-section', 'diff.ipynb'], stdout=open(devnull, 'w'), stderr=STDOUT) attrfile = _get_attrfile(git_config, install_location, attrfile) except FileNotFoundError: print('Uninstall failed: git is not on path!', file=sys.stderr) return 1 except CalledProcessError: print('Uninstall failed: not a git repository!', file=sys.stderr) return 1 # Check if there is a filter for ipynb files if path.exists(attrfile): with open(attrfile, 'r+') as f: patterns = ('*.ipynb filter', '*.zpln filter', '*.ipynb diff') lines = [line for line in f if not any(line.startswith(p) for p in patterns)] f.seek(0) f.write(''.join(lines)) f.truncate() def status(git_config, install_location=INSTALL_LOCATION_LOCAL, verbose=False): """Return 0 if nbstripout is installed in the current repo, 1 otherwise""" try: if install_location == INSTALL_LOCATION_SYSTEM: location = 'system-wide' elif install_location == INSTALL_LOCATION_GLOBAL: location = 'globally' else: git_dir = path.dirname(path.abspath(check_output(['git', 'rev-parse', '--git-dir'], universal_newlines=True).strip())) location = f"in repository '{git_dir}'" clean = check_output(git_config + ['filter.nbstripout.clean'], universal_newlines=True).strip() smudge = check_output(git_config + ['filter.nbstripout.smudge'], universal_newlines=True).strip() diff = check_output(git_config + ['diff.ipynb.textconv'], universal_newlines=True).strip() if install_location in {INSTALL_LOCATION_SYSTEM, INSTALL_LOCATION_GLOBAL}: attrfile = _get_attrfile(git_config, install_location) attributes = '' diff_attributes = '' if path.exists(attrfile): with open(attrfile, 'r') as f: attrs = f.readlines() attributes = ''.join(line for line in attrs if 'filter' in line).strip() diff_attributes = ''.join(line for line in attrs if 'diff' in line).strip() else: attributes = check_output(['git', 'check-attr', 'filter', '--', '*.ipynb'], universal_newlines=True).strip() diff_attributes = check_output(['git', 'check-attr', 'diff', '--', '*.ipynb'], universal_newlines=True).strip() try: extra_keys = check_output(git_config + ['filter.nbstripout.extrakeys'], universal_newlines=True).strip() except CalledProcessError: extra_keys = '' if attributes.endswith('unspecified'): if verbose: print('nbstripout is not installed', location) return 1 if verbose: print('nbstripout is installed', location) print('\nFilter:') print(' clean =', clean) print(' smudge =', smudge) print(' diff=', diff) print(' extrakeys=', extra_keys) print('\nAttributes:\n ', attributes) print('\nDiff Attributes:\n ', diff_attributes) return 0 except FileNotFoundError: print('Cannot determine status: git is not on path!', file=sys.stderr) return 1 except CalledProcessError: if verbose and 'location' in locals(): print('nbstripout is not installed', location) return 1 def process_jupyter_notebook(input_stream, output_stream, args, extra_keys, filename='input from stdin'): with warnings.catch_warnings(): warnings.simplefilter("ignore", category=UserWarning) nb = nbformat.read(input_stream, as_version=nbformat.NO_CONVERT) nb_orig = copy.deepcopy(nb) nb_stripped = strip_output(nb, args.keep_output, args.keep_count, args.keep_id, extra_keys, args.drop_empty_cells, args.drop_tagged_cells.split(), args.strip_init_cells, _parse_size(args.max_size)) any_change = nb_orig != nb_stripped if args.dry_run: if any_change: output_stream.write(f'Dry run: would have stripped {filename}\n') return any_change if output_stream.seekable(): output_stream.seek(0) output_stream.truncate() with warnings.catch_warnings(): warnings.simplefilter("ignore", category=UserWarning) nbformat.write(nb_stripped, output_stream) output_stream.flush() return any_change def process_zeppelin_notebook(input_stream, output_stream, args, extra_keys, filename='input from stdin'): nb = json.load(input_stream, object_pairs_hook=collections.OrderedDict) nb_orig = copy.deepcopy(nb) nb_stripped = strip_zeppelin_output(nb) any_change = nb_orig != nb_stripped if args.dry_run: if any_change: output_stream.write(f'Dry run: would have stripped {filename}\n') return any_change if output_stream.seekable(): output_stream.seek(0) output_stream.truncate() json.dump(nb_stripped, output_stream, indent=2) output_stream.write('\n') output_stream.flush() return any_change def main(): parser = ArgumentParser(epilog=__doc__, formatter_class=RawDescriptionHelpFormatter) task = parser.add_mutually_exclusive_group() task.add_argument('--dry-run', action='store_true', help='Print which notebooks would have been stripped') task.add_argument('--install', action='store_true', help='Install nbstripout in the current repository (set ' 'up the git filter and attributes)') task.add_argument('--uninstall', action='store_true', help='Uninstall nbstripout from the current repository ' '(remove the git filter and attributes)') task.add_argument('--is-installed', action='store_true', help='Check if nbstripout is installed in current repository') task.add_argument('--status', action='store_true', help='Print status of nbstripout installation in current ' 'repository and configuration summary if installed') task.add_argument('--version', action='store_true', help='Print version') parser.add_argument("--verify", action="store_true", help="Return a non-zero exit code if any files were changed, Implies --dry-run") parser.add_argument('--keep-count', action='store_true', help='Do not strip the execution count/prompt number') parser.add_argument('--keep-output', action='store_true', help='Do not strip output', default=None) parser.add_argument('--keep-id', action='store_true', help='Keep the randomly generated cell ids, ' 'which will be different after each execution.') parser.add_argument('--extra-keys', default='', help='Space separated list of extra keys to strip ' 'from metadata, e.g. metadata.foo cell.metadata.bar') parser.add_argument('--keep-metadata-keys', default='', help='Space separated list of metadata keys to keep' ', e.g. metadata.foo cell.metadata.bar') parser.add_argument('--drop-empty-cells', action='store_true', help='Remove cells where `source` is empty or contains only whitepace') parser.add_argument('--drop-tagged-cells', default='', help='Space separated list of cell-tags that remove an entire cell') parser.add_argument('--strip-init-cells', action='store_true', help='Remove cells with `init_cell: true` metadata (default: False)') parser.add_argument('--attributes', metavar='FILEPATH', help='Attributes file to add the filter to (in ' 'combination with --install/--uninstall), ' 'defaults to .git/info/attributes') location = parser.add_mutually_exclusive_group() location.add_argument('--global', dest='_global', action='store_true', help='Use global git config (default is local config)') location.add_argument('--system', dest='_system', action='store_true', help='Use system git config (default is local config)') location.add_argument('--python', dest='_python', metavar="PATH", help='Path to python executable to use when --install\'ing ' '(default is deduced from `sys.executable`)') parser.add_argument('--force', '-f', action='store_true', help='Strip output also from files with non ipynb extension') parser.add_argument('--max-size', metavar='SIZE', help='Keep outputs smaller than SIZE', default='0') parser.add_argument('--mode', '-m', default='jupyter', choices=['jupyter', 'zeppelin'], help='Specify mode between [jupyter (default) | zeppelin] (to be used in combination with -f)') parser.add_argument('--textconv', '-t', action='store_true', help='Prints stripped files to STDOUT') parser.add_argument('files', nargs='*', help='Files to strip output from') args = parser.parse_args() git_config = ['git', 'config'] if args.verify and not args.dry_run: args.dry_run = True if args._system: git_config.append('--system') install_location = INSTALL_LOCATION_SYSTEM elif args._global: git_config.append('--global') install_location = INSTALL_LOCATION_GLOBAL else: git_config.append('--local') install_location = INSTALL_LOCATION_LOCAL if args.install: raise SystemExit(install(git_config, install_location, python=args._python, attrfile=args.attributes)) if args.uninstall: raise SystemExit(uninstall(git_config, install_location, attrfile=args.attributes)) if args.is_installed: raise SystemExit(status(git_config, install_location, verbose=False)) if args.status: raise SystemExit(status(git_config, install_location, verbose=True)) if args.version: print(__version__) raise SystemExit(0) extra_keys = [ 'metadata.signature', 'metadata.widgets', 'cell.metadata.collapsed', 'cell.metadata.ExecuteTime', 'cell.metadata.execution', 'cell.metadata.heading_collapsed', 'cell.metadata.hidden', 'cell.metadata.scrolled', ] try: extra_keys.extend(check_output((git_config if args._system or args._global else ['git', 'config']) + ['filter.nbstripout.extrakeys'], universal_newlines=True).strip().split()) except (CalledProcessError, FileNotFoundError): pass extra_keys.extend(args.extra_keys.split()) try: keep_metadata_keys = check_output( (git_config if args._system or args._global else ['git', 'config']) + ['filter.nbstripout.keepmetadatakeys'], universal_newlines=True ).strip().split() except (CalledProcessError, FileNotFoundError): keep_metadata_keys = [] keep_metadata_keys.extend(args.keep_metadata_keys.split()) extra_keys = [i for i in extra_keys if i not in keep_metadata_keys] # Wrap input/output stream in UTF-8 encoded text wrapper # https://stackoverflow.com/a/16549381 input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8') if sys.stdin else None output_stream = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', newline='') process_notebook = {'jupyter': process_jupyter_notebook, 'zeppelin': process_zeppelin_notebook}[args.mode] any_change = False for filename in args.files: if not (args.force or filename.endswith('.ipynb') or filename.endswith('.zpln')): continue try: with io.open(filename, 'r+', encoding='utf8', newline='') as f: out = output_stream if args.textconv or args.dry_run else f if process_notebook(f, out, args, extra_keys, filename): any_change = True except nbformat.reader.NotJSONError: print(f"No valid notebook detected in '{filename}'", file=sys.stderr) raise SystemExit(1) except FileNotFoundError: print(f"Could not strip '{filename}': file not found", file=sys.stderr) raise SystemExit(1) except Exception: # Ignore exceptions for non-notebook files. print(f"Could not strip '{filename}'", file=sys.stderr) raise if not args.files and input_stream: try: if process_notebook(input_stream, output_stream, args, extra_keys): any_change = True except nbformat.reader.NotJSONError: print('No valid notebook detected on stdin', file=sys.stderr) raise SystemExit(1) if args.verify and any_change: raise SystemExit(1) python-nbstripout-0.8.1/nbstripout/_utils.py000066400000000000000000000126221475440000300213320ustar00rootroot00000000000000from collections import defaultdict import sys __all__ = ["pop_recursive", "strip_output", "strip_zeppelin_output", "MetadataError"] class MetadataError(Exception): pass def pop_recursive(d, key, default=None): """dict.pop(key) where `key` is a `.`-delimited list of nested keys. >>> d = {'a': {'b': 1, 'c': 2}} >>> pop_recursive(d, 'a.c') 2 >>> d {'a': {'b': 1}} """ if not isinstance(d, dict): return default if key in d: return d.pop(key, default) if '.' not in key: return default key_head, key_tail = key.split('.', maxsplit=1) if key_head in d: return pop_recursive(d[key_head], key_tail, default) return default def _cells(nb, conditionals): """Remove cells not satisfying any conditional in conditionals and yield all other cells.""" if hasattr(nb, 'nbformat') and nb.nbformat < 4: for ws in nb.worksheets: for conditional in conditionals: ws.cells = list(filter(conditional, ws.cells)) for cell in ws.cells: yield cell else: for conditional in conditionals: nb.cells = list(filter(conditional, nb.cells)) for cell in nb.cells: yield cell def get_size(item): """ Recursively sums length of all strings in `item` """ if isinstance(item, str): return len(item) elif isinstance(item, list): return sum(get_size(elem) for elem in item) elif isinstance(item, dict): return get_size(list(item.values())) else: return len(str(item)) def determine_keep_output(cell, default, strip_init_cells=False): """Given a cell, determine whether output should be kept Based on whether the metadata has "init_cell": true, "keep_output": true, or the tags contain "keep_output" """ if 'metadata' not in cell: return default if 'init_cell' in cell.metadata: return bool(cell.metadata.init_cell) and not strip_init_cells has_keep_output_metadata = 'keep_output' in cell.metadata keep_output_metadata = bool(cell.metadata.get('keep_output', False)) has_keep_output_tag = 'keep_output' in cell.metadata.get('tags', []) # keep_output between metadata and tags should not contradict each other if has_keep_output_metadata and has_keep_output_tag and not keep_output_metadata: raise MetadataError( 'cell metadata contradicts tags: `keep_output` is false, but `keep_output` in tags' ) if has_keep_output_metadata or has_keep_output_tag: return keep_output_metadata or has_keep_output_tag return default def _zeppelin_cells(nb): for pg in nb['paragraphs']: yield pg def strip_zeppelin_output(nb): for cell in _zeppelin_cells(nb): if 'results' in cell: cell['results'] = {} return nb def strip_output(nb, keep_output, keep_count, keep_id, extra_keys=[], drop_empty_cells=False, drop_tagged_cells=[], strip_init_cells=False, max_size=0): """ Strip the outputs, execution count/prompt number and miscellaneous metadata from a notebook object, unless specified to keep either the outputs or counts. `extra_keys` could be 'metadata.foo cell.metadata.bar metadata.baz' """ if keep_output is None and 'keep_output' in nb.metadata: keep_output = bool(nb.metadata['keep_output']) keys = defaultdict(list) for key in extra_keys: if '.' not in key or key.split('.')[0] not in ['cell', 'metadata']: sys.stderr.write(f'Ignoring invalid extra key `{key}`\n') else: namespace, subkey = key.split('.', maxsplit=1) keys[namespace].append(subkey) for field in keys['metadata']: pop_recursive(nb.metadata, field) conditionals = [] # Keep cells if they have any `source` line that contains non-whitespace if drop_empty_cells: conditionals.append(lambda c: any(line.strip() for line in c.get('source', []))) for tag_to_drop in drop_tagged_cells: conditionals.append(lambda c: tag_to_drop not in c.get("metadata", {}).get("tags", [])) for i, cell in enumerate(_cells(nb, conditionals)): keep_output_this_cell = determine_keep_output(cell, keep_output, strip_init_cells) # Remove the outputs, unless directed otherwise if 'outputs' in cell: # Default behavior (max_size == 0) strips all outputs. if not keep_output_this_cell: cell['outputs'] = [output for output in cell['outputs'] if get_size(output) <= max_size] # Strip the counts from the outputs that were kept if not keep_count. if not keep_count: for output in cell['outputs']: if 'execution_count' in output: output['execution_count'] = None # If keep_output_this_cell and keep_count, do nothing. # Remove the prompt_number/execution_count, unless directed otherwise if 'prompt_number' in cell and not keep_count: cell['prompt_number'] = None if 'execution_count' in cell and not keep_count: cell['execution_count'] = None # Replace the cell id with an incremental value that will be consistent across runs if 'id' in cell and not keep_id: cell['id'] = str(i) for field in keys['cell']: pop_recursive(cell, field) return nb python-nbstripout-0.8.1/pre-commit000066400000000000000000000012071475440000300172440ustar00rootroot00000000000000#!/bin/sh # # strip output of IPython Notebooks # add this as `.git/hooks/pre-commit` # to run every time you commit a notebook # # requires `nbstripout` to be available on your PATH # if git rev-parse --verify HEAD >/dev/null 2>&1; then against=HEAD else # Initial commit: diff against an empty tree object against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 fi # Find notebooks to be committed ( IFS=' ' NBS=$(git diff-index -z --cached $against --name-only | grep '.ipynb$' | uniq) for NB in $NBS ; do echo "Removing outputs from $NB" nbstripout "$NB" git add "$NB" done ) exec git diff-index --check --cached $against -- python-nbstripout-0.8.1/pytest.ini000066400000000000000000000000321475440000300172710ustar00rootroot00000000000000[pytest] addopts = --ruff python-nbstripout-0.8.1/setup.cfg000066400000000000000000000005071475440000300170700ustar00rootroot00000000000000[bumpversion] current_version = 0.8.1 commit = True tag = True tag_name = {new_version} tag_message = v{new_version} [bumpversion:file:CONTRIBUTING.md] [bumpversion:file:README.md] [bumpversion:file:setup.py] [bumpversion:file:nbstripout/_nbstripout.py] [bdist_wheel] universal = 1 [metadata] license_files = LICENSE.txt python-nbstripout-0.8.1/setup.py000066400000000000000000000026261475440000300167650ustar00rootroot00000000000000from setuptools import setup, find_packages with open('README.md') as f: long_description = f.read() install_requires = [ 'nbformat' ] setup(name='nbstripout', version='0.8.1', author='Florian Rathgeber', author_email='florian.rathgeber@gmail.com', url='https://github.com/kynan/nbstripout', license="License :: OSI Approved :: MIT License", description='Strips outputs from Jupyter and IPython notebooks', long_description=long_description, long_description_content_type='text/markdown', packages=find_packages(), provides=['nbstripout'], entry_points={ 'console_scripts': [ 'nbstripout = nbstripout._nbstripout:main' ], }, install_requires=install_requires, python_requires='>=3.8', classifiers=[ "Development Status :: 4 - Beta", "Environment :: Other Environment", "Framework :: IPython", "Intended Audience :: Developers", "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Topic :: Software Development :: Version Control", ]) python-nbstripout-0.8.1/tests/000077500000000000000000000000001475440000300164075ustar00rootroot00000000000000python-nbstripout-0.8.1/tests/conftest.py000066400000000000000000000000341475440000300206030ustar00rootroot00000000000000pytest_plugins = "pytester" python-nbstripout-0.8.1/tests/e2e_notebooks/000077500000000000000000000000001475440000300211455ustar00rootroot00000000000000python-nbstripout-0.8.1/tests/e2e_notebooks/test_drop_empty_cells.ipynb000066400000000000000000000022341475440000300266140ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This cell will always be kept\n" ] } ], "source": [ "print('This cell will always be kept')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# This cell will also be kept" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 4 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_drop_empty_cells.ipynb.expected000066400000000000000000000013141475440000300304120ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('This cell will always be kept')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# This cell will also be kept" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 4 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_drop_empty_cells_dontdrop.ipynb.expected000066400000000000000000000020341475440000300323230ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('This cell will always be kept')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# This cell will also be kept" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 4 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_drop_tagged_cells.ipynb000066400000000000000000000034201475440000300267070ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This cell will always be kept\n" ] } ], "source": [ "print('This cell will always be kept')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# This cell will also be kept" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [ "test2" ] }, "outputs": [], "source": [ "# This Code cell will stay" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "test" ] }, "outputs": [], "source": [ "# This is a Code Cell will be removed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": { "tags": [ "test" ] }, "source": [ "This is markdown and will be removed" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "test2" ] }, "source": [ "This is Markdown and will be kept" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.6" } }, "nbformat": 4, "nbformat_minor": 4 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_drop_tagged_cells.ipynb.expected000066400000000000000000000024651475440000300305170ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('This cell will always be kept')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# This cell will also be kept" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "test2" ] }, "outputs": [], "source": [ "# This Code cell will stay" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": { "tags": [ "test2" ] }, "source": [ "This is Markdown and will be kept" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.6" } }, "nbformat": 4, "nbformat_minor": 4 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_drop_tagged_cells_dontdrop.ipynb.expected000066400000000000000000000032261475440000300324240ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('This cell will always be kept')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# This cell will also be kept" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "test2" ] }, "outputs": [], "source": [ "# This Code cell will stay" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "test" ] }, "outputs": [], "source": [ "# This is a Code Cell will be removed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": { "tags": [ "test" ] }, "source": [ "This is markdown and will be removed" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "test2" ] }, "source": [ "This is Markdown and will be kept" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.6" } }, "nbformat": 4, "nbformat_minor": 4 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_empty_metadata.ipynb000066400000000000000000000003401475440000300262420ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "id": "5c42035d", "metadata": {}, "source": [ "This notebook is missing `metadata.kernelspec`." ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_empty_metadata.ipynb.expected000066400000000000000000000003311475440000300300420ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "This notebook is missing `metadata.kernelspec`." ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_execution_timing.ipynb000066400000000000000000000021611475440000300266210ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2020-05-28T20:57:07.733742Z", "iopub.status.busy": "2020-05-28T20:57:07.733470Z", "iopub.status.idle": "2020-05-28T20:57:07.788874Z", "shell.execute_reply": "2020-05-28T20:57:07.788239Z", "shell.execute_reply.started": "2020-05-28T20:57:07.733718Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print('Hello World')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 4 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_execution_timing.ipynb.expected000066400000000000000000000012741475440000300304250ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Hello World')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 4 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_invalid_json.ipynb000066400000000000000000000011171475440000300257260ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "id": "5c42035d", "metadata": {}, "source": [ "This notebook is not valid JSON." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 5, } python-nbstripout-0.8.1/tests/e2e_notebooks/test_keep_metadata_keys.ipynb000066400000000000000000000006311475440000300270660ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": { "scrolled": true }, "source": [ "This notebook tests that using \"--keep-metadata-keys\" works as expected." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "1+1" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_keep_metadata_keys.ipynb.expected000066400000000000000000000006311475440000300306660ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": { "scrolled": true }, "source": [ "This notebook tests that using \"--keep-metadata-keys\" works as expected." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "1+1" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_max_size.ipynb000066400000000000000000000040611475440000300250670ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "id": "336ba586", "metadata": {}, "source": [ "This notebook tests that outputs can be cleared based on size" ] }, { "cell_type": "code", "execution_count": 1, "id": "a014078d", "metadata": { "ExecuteTime": { "end_time": "2021-05-02T02:39:41.848031Z", "start_time": "2021-05-02T02:39:41.842433Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "aaaaaaaaaa\n" ] } ], "source": [ "print(\"a\"*10)" ] }, { "cell_type": "code", "execution_count": 2, "id": "89ff455b", "metadata": { "ExecuteTime": { "end_time": "2021-05-02T02:39:55.772449Z", "start_time": "2021-05-02T02:39:55.766400Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" ] } ], "source": [ "print(\"a\"*100)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.4" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 5 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_max_size.ipynb.expected000066400000000000000000000031551475440000300266720ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "id": "336ba586", "metadata": {}, "source": [ "This notebook tests that outputs can be cleared based on size" ] }, { "cell_type": "code", "execution_count": null, "id": "a014078d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "aaaaaaaaaa\n" ] } ], "source": [ "print(\"a\"*10)" ] }, { "cell_type": "code", "execution_count": null, "id": "89ff455b", "metadata": {}, "outputs": [], "source": [ "print(\"a\"*100)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.4" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 5 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_max_size.ipynb.expected_sequential_id000066400000000000000000000031301475440000300315710ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "This notebook tests that outputs can be cleared based on size" ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "aaaaaaaaaa\n" ] } ], "source": [ "print(\"a\"*10)" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "print(\"a\"*100)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.4" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 5 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_metadata.ipynb000066400000000000000000000033211475440000300250260ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that cells with either `\"keep_output\": true` or `\"init_cell\": true` are not stripped." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "init_cell": true }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+1 # This cell has `\"init_cell:\" true`" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "keep_output": true }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+2 # This cell has `\"keep_output:\" true`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3+3" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_metadata.ipynb.expected000066400000000000000000000027601475440000300266340ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that cells with either `\"keep_output\": true` or `\"init_cell\": true` are not stripped." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "init_cell": true }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+1 # This cell has `\"init_cell:\" true`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "keep_output": true }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+2 # This cell has `\"keep_output:\" true`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3+3" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_metadata_exception.ipynb000066400000000000000000000015711475440000300271110ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "gross-insertion", "metadata": { "keep_output": false, "tags": [ "keep_output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "this cell has a contradiction between tags\n" ] } ], "source": [ "print('this cell has a contradiction between tags')" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 5 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_metadata_extra_keys.ipynb.expected000066400000000000000000000022001475440000300310570ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that cells with either `\"keep_output\": true` or `\"init_cell\": true` are not stripped." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "init_cell": true }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+1 # This cell has `\"init_cell:\" true`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "keep_output": true }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+2 # This cell has `\"keep_output:\" true`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3+3" ] } ], "metadata": { "celltoolbar": "Edit Metadata" }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_metadata_keep_count.ipynb.expected000066400000000000000000000027411475440000300310470ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that cells with either `\"keep_output\": true` or `\"init_cell\": true` are not stripped." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "init_cell": true }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+1 # This cell has `\"init_cell:\" true`" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "keep_output": true }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+2 # This cell has `\"keep_output:\" true`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "3+3" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_metadata_keep_output.ipynb.expected000066400000000000000000000032301475440000300312510ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that cells with either `\"keep_output\": true` or `\"init_cell\": true` are not stripped." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "init_cell": true }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+1 # This cell has `\"init_cell:\" true`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "keep_output": true }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+2 # This cell has `\"keep_output:\" true`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3+3" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_metadata_keep_output_keep_count.ipynb.expected000066400000000000000000000032061475440000300334700ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that cells with either `\"keep_output\": true` or `\"init_cell\": true` are not stripped." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "init_cell": true }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+1 # This cell has `\"init_cell:\" true`" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "keep_output": true }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+2 # This cell has `\"keep_output:\" true`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3+3" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_metadata_notebook.ipynb000066400000000000000000000026321475440000300267320ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that when using `\"keep_out\": true` on notebook level, only cells with `\"keep_output\": false` are stripped." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+1 # This cell uses the notebook level `\"keep_output:\" true`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "keep_output": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+2 # This cell has `\"keep_output:\" false`" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "keep_output": true, "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_metadata_notebook.ipynb.expected000066400000000000000000000023621475440000300305320ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that when using `\"keep_out\": true` on notebook level, only cells with `\"keep_output\": false` are stripped." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+1 # This cell uses the notebook level `\"keep_output:\" true`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "keep_output": false }, "outputs": [], "source": [ "2+2 # This cell has `\"keep_output:\" false`" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "keep_output": true, "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_metadata_period.ipynb000066400000000000000000000035171475440000300263770ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "source": [ "This notebook tests that metadata keys with periods can be stripped." ], "metadata": { "application/vnd.databricks.v1+cell": { "title": "", "showTitle": false, "inputWidgets": {}, "nuid": "4006abf4-c940-4065-aa15-3772229cbe25" } } }, { "cell_type": "code", "source": [ "1+1" ], "metadata": { "application/vnd.databricks.v1+cell": { "title": "", "showTitle": false, "inputWidgets": {}, "nuid": "5069d88c-a72a-40b8-acf5-258ee9649736" } }, "outputs": [ { "output_type": "display_data", "metadata": { "application/vnd.databricks.v1+output": { "datasetInfos": [], "data": "
Out[1]: 2
", "removedWidgets": [], "addedWidgets": {}, "type": "html", "arguments": {} } }, "data": { "text/html": [ "\n
Out[1]: 2
" ] } } ], "execution_count": 0 } ], "metadata": { "application/vnd.databricks.v1+notebook": { "notebookName": "test_metadata_period", "dashboards": [], "language": "python", "widgets": {}, "notebookOrigID": 123456 } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_metadata_period.ipynb.expected000066400000000000000000000005401475440000300301700ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that metadata keys with periods can be stripped." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1+1" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_missing_nbformat.ipynb000066400000000000000000000011061475440000300266060ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "id": "5c42035d", "metadata": {}, "source": [ "This notebook is missing `nbformat` and `nbformat_minor` keys." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } } } python-nbstripout-0.8.1/tests/e2e_notebooks/test_missing_nbformat.ipynb.expected000066400000000000000000000014431475440000300304120ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "This notebook is missing `nbformat` and `nbformat_minor` keys." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } } } python-nbstripout-0.8.1/tests/e2e_notebooks/test_nbformat2.ipynb000066400000000000000000000026671475440000300251540ustar00rootroot00000000000000{ "metadata": { "name": "01_notebook_introduction" }, "nbformat": 2, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "\"This is the new Jupyter notebook\"" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 1, "text": [ "'This is the new Jupyter notebook'" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "ls" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "00_notebook_tour.ipynb formatting.ipynb sympy_quantum_computing.ipynb", "01_notebook_introduction.ipynb python-logo.svg trapezoid_rule.ipynb", "display_protocol.ipynb sympy.ipynb" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "def f(x):", " \"\"\"My function", " x : parameter\"\"\"", " ", " return x+1", "", "print \"f(3) = \", f(3)" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "f(3) = 4" ] } ], "prompt_number": 3 } ] } ] } python-nbstripout-0.8.1/tests/e2e_notebooks/test_nbformat2.ipynb.expected000066400000000000000000000014731475440000300267460ustar00rootroot00000000000000{ "metadata": { "name": "01_notebook_introduction" }, "nbformat": 2, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "\"This is the new Jupyter notebook\"" ], "language": "python", "outputs": [], "prompt_number": null }, { "cell_type": "code", "collapsed": false, "input": [ "ls" ], "language": "python", "outputs": [], "prompt_number": null }, { "cell_type": "code", "collapsed": false, "input": [ "def f(x):", " \"\"\"My function", " x : parameter\"\"\"", " ", " return x+1", "", "print \"f(3) = \", f(3)" ], "language": "python", "outputs": [], "prompt_number": null } ] } ] } python-nbstripout-0.8.1/tests/e2e_notebooks/test_nbformat45.ipynb000066400000000000000000000030651475440000300252340ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "5c42035d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This is the new Jupyter notebook'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"This is the new Jupyter notebook\"" ] }, { "cell_type": "code", "execution_count": 1, "id": "886205fa", "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "'text2'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"text2\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "a183d4e9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f(3) = 4\n" ] } ], "source": [ "def f(x):\n", " \"\"\"My function\n", " x : parameter\"\"\"\n", " \n", " return x+1\n", "\n", "print(\"f(3) = \", f(3))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 5 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_nbformat45.ipynb.expected000066400000000000000000000021041475440000300270250ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "5c42035d", "metadata": {}, "outputs": [], "source": [ "\"This is the new Jupyter notebook\"" ] }, { "cell_type": "code", "execution_count": null, "id": "886205fa", "metadata": {}, "outputs": [], "source": [ "\"text2\"" ] }, { "cell_type": "code", "execution_count": null, "id": "a183d4e9", "metadata": {}, "outputs": [], "source": [ "def f(x):\n", " \"\"\"My function\n", " x : parameter\"\"\"\n", " \n", " return x+1\n", "\n", "print(\"f(3) = \", f(3))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 5 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_nbformat45.ipynb.expected_sequential_id000066400000000000000000000020571475440000300317420ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "0", "metadata": {}, "outputs": [], "source": [ "\"This is the new Jupyter notebook\"" ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "\"text2\"" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "def f(x):\n", " \"\"\"My function\n", " x : parameter\"\"\"\n", " \n", " return x+1\n", "\n", "print(\"f(3) = \", f(3))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 5 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_nochange.ipynb000066400000000000000000000020351475440000300250310ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook should not be changed by nbstripout." ] }, { "cell_type": "code", "metadata": {}, "outputs": [], "source": [ "1+1 # This cell has `\"init_cell:\" true`" ] }, { "cell_type": "code", "metadata": { "keep_output": true }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+2 # This cell has `\"keep_output:\" true`" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_strip_init_cells.ipynb000066400000000000000000000020401475440000300266110ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that cells with `\"init_cell\": true` are stripped when the `--strip-init-cells` flag is passed in." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "init_cell": true }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+1 # This cell has `\"init_cell:\" true`" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_strip_init_cells.ipynb.expected000066400000000000000000000015461475440000300304230ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that cells with `\"init_cell\": true` are stripped when the `--strip-init-cells` flag is passed in." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "init_cell": true }, "outputs": [], "source": [ "1+1 # This cell has `\"init_cell:\" true`" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_unicode.ipynb000066400000000000000000000013141475440000300246740ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "äöü\n" ] } ], "source": [ "print u\"äöü\"" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_unicode.ipynb.expected000066400000000000000000000011101475440000300264660ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print u\"äöü\"" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_widgets.ipynb000066400000000000000000000021221475440000300247120ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ipywidgets import interact" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "@interact(foo=1)\n", "def f(foo):\n", " print(foo)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" }, "widgets": { "state": { "0da880c230634707bab4e9cd1fa24285": { "views": [ { "cell_index": 1 } ] } }, "version": "1.2.0" } }, "nbformat": 4, "nbformat_minor": 1 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_widgets.ipynb.expected000066400000000000000000000014171475440000300265200ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import interact" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@interact(foo=1)\n", "def f(foo):\n", " print(foo)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 } python-nbstripout-0.8.1/tests/e2e_notebooks/test_zeppelin.zpln000066400000000000000000000033431475440000300247420ustar00rootroot00000000000000{ "paragraphs": [ { "text": "%pyspark\nprint(\u0027hello\u0027)\n", "user": "anonymous", "dateUpdated": "2020-08-16 14:01:17.340", "config": { "colWidth": 12.0, "fontSize": 9.0, "enabled": true, "results": {}, "editorSetting": { "language": "python", "editOnDblClick": false, "completionKey": "TAB", "completionSupport": true }, "editorMode": "ace/mode/python" }, "settings": { "params": {}, "forms": {} }, "results": { "code": "SUCCESS", "msg": [ { "type": "TEXT", "data": "hello\n" } ] }, "apps": [], "progressUpdateIntervalMs": 500, "jobName": "paragraph_1594203870449_-297207159", "id": "paragraph_1594203870449_-297207159", "dateCreated": "2020-07-08 15:54:30.449", "dateStarted": "2020-08-16 14:01:17.351", "dateFinished": "2020-08-16 14:01:17.391", "status": "FINISHED" }, { "text": "%pyspark\n", "user": "anonymous", "dateUpdated": "2020-07-30 13:52:45.488", "config": {}, "settings": { "params": {}, "forms": {} }, "apps": [], "progressUpdateIntervalMs": 500, "jobName": "paragraph_1596097365488_-203822127", "id": "paragraph_1596097365488_-203822127", "dateCreated": "2020-07-30 13:52:45.488", "status": "READY" } ], "name": "test", "id": "2FCP5JWR7", "defaultInterpreterGroup": "spark", "version": "0.9.0-preview1", "noteParams": {}, "noteForms": {}, "angularObjects": {}, "config": { "isZeppelinNotebookCronEnable": false }, "info": {} }python-nbstripout-0.8.1/tests/e2e_notebooks/test_zeppelin.zpln.expected000066400000000000000000000031131475440000300265350ustar00rootroot00000000000000{ "paragraphs": [ { "text": "%pyspark\nprint('hello')\n", "user": "anonymous", "dateUpdated": "2020-08-16 14:01:17.340", "config": { "colWidth": 12.0, "fontSize": 9.0, "enabled": true, "results": {}, "editorSetting": { "language": "python", "editOnDblClick": false, "completionKey": "TAB", "completionSupport": true }, "editorMode": "ace/mode/python" }, "settings": { "params": {}, "forms": {} }, "results": {}, "apps": [], "progressUpdateIntervalMs": 500, "jobName": "paragraph_1594203870449_-297207159", "id": "paragraph_1594203870449_-297207159", "dateCreated": "2020-07-08 15:54:30.449", "dateStarted": "2020-08-16 14:01:17.351", "dateFinished": "2020-08-16 14:01:17.391", "status": "FINISHED" }, { "text": "%pyspark\n", "user": "anonymous", "dateUpdated": "2020-07-30 13:52:45.488", "config": {}, "settings": { "params": {}, "forms": {} }, "apps": [], "progressUpdateIntervalMs": 500, "jobName": "paragraph_1596097365488_-203822127", "id": "paragraph_1596097365488_-203822127", "dateCreated": "2020-07-30 13:52:45.488", "status": "READY" } ], "name": "test", "id": "2FCP5JWR7", "defaultInterpreterGroup": "spark", "version": "0.9.0-preview1", "noteParams": {}, "noteForms": {}, "angularObjects": {}, "config": { "isZeppelinNotebookCronEnable": false }, "info": {} } python-nbstripout-0.8.1/tests/requirements.txt000066400000000000000000000000231475440000300216660ustar00rootroot00000000000000pytest pytest-ruff python-nbstripout-0.8.1/tests/test_diff.ipynb000066400000000000000000000011631475440000300214220ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "print(\"aou\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/test_diff_different.ipynb000066400000000000000000000013561475440000300234540ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "aou now it is different\n" ] } ], "source": [ "print(\"aou now it is different\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/test_diff_different_extrakeys.ipynb000066400000000000000000000011031475440000300255410ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"aou now it is different\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/test_diff_from_bash_subshells.py000066400000000000000000000020711475440000300250340ustar00rootroot00000000000000from pathlib import Path import sys import pytest # fix this before pytester.chdir() happens NOTEBOOKS_FOLDER = Path("tests").absolute() def test_diff_with_process_substitution_nodiff(pytester: pytest.Pytester): if sys.platform.startswith("win"): pytest.skip("test requires proper bash shell") r = pytester.run( 'bash', '-c', f'diff <( nbstripout -t {NOTEBOOKS_FOLDER / "test_diff.ipynb"} ) <( nbstripout -t {NOTEBOOKS_FOLDER / "test_diff_output.ipynb"} )', ) assert not r.outlines assert r.ret == 0 def test_diff_with_process_substitution_diff(pytester: pytest.Pytester): if sys.platform.startswith("win"): pytest.skip("test requires proper bash shell") r = pytester.run( 'bash', '-c', f'diff <( nbstripout -t {NOTEBOOKS_FOLDER / "test_diff.ipynb"} ) <( nbstripout -t {NOTEBOOKS_FOLDER / "test_diff_different.ipynb"} )', ) r.stdout.re_match_lines(r"""(.*) < "print(\"aou\")" --- (.*\"print\(\\\"aou now it is different\\\"\)\") """.splitlines()) assert r.ret == 1 python-nbstripout-0.8.1/tests/test_diff_output.ipynb000066400000000000000000000013061475440000300230410ustar00rootroot00000000000000{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "aou\n" ] } ], "source": [ "print(\"aou\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 } python-nbstripout-0.8.1/tests/test_end_to_end.py000066400000000000000000000160451475440000300221240ustar00rootroot00000000000000import os from pathlib import Path import re from subprocess import run, PIPE # Note: typing.Pattern is deprecated, for removal in 3.13 in favour of re.Pattern introduced in 3.8 from typing import List, Union, Pattern import pytest NOTEBOOKS_FOLDER = Path("tests/e2e_notebooks") TEST_CASES = [ ("test_drop_empty_cells.ipynb", "test_drop_empty_cells_dontdrop.ipynb.expected", []), ("test_drop_empty_cells.ipynb", "test_drop_empty_cells.ipynb.expected", ["--drop-empty-cells"]), ("test_drop_tagged_cells.ipynb", "test_drop_tagged_cells_dontdrop.ipynb.expected", []), ("test_drop_tagged_cells.ipynb", "test_drop_tagged_cells.ipynb.expected", ['--drop-tagged-cells=test']), ("test_execution_timing.ipynb", "test_execution_timing.ipynb.expected", []), ("test_max_size.ipynb", "test_max_size.ipynb.expected", ["--max-size", "50", "--keep-id"]), ("test_max_size.ipynb", "test_max_size.ipynb.expected_sequential_id", ["--max-size", "50"]), ("test_empty_metadata.ipynb", "test_empty_metadata.ipynb.expected", []), ("test_metadata.ipynb", "test_metadata.ipynb.expected", []), ("test_metadata.ipynb", "test_metadata_extra_keys.ipynb.expected", ["--extra-keys", "metadata.kernelspec metadata.language_info"]), ("test_metadata.ipynb", "test_metadata_keep_count.ipynb.expected", ["--keep-count"]), ("test_metadata.ipynb", "test_metadata_keep_output.ipynb.expected", ["--keep-output"]), ("test_metadata.ipynb", "test_metadata_keep_output_keep_count.ipynb.expected", ["--keep-output", "--keep-count"]), ("test_metadata_notebook.ipynb", "test_metadata_notebook.ipynb.expected", []), ("test_keep_metadata_keys.ipynb", "test_keep_metadata_keys.ipynb.expected", ["--keep-metadata-keys", "cell.metadata.scrolled cell.metadata.collapsed metadata.a"]), ("test_metadata_period.ipynb", "test_metadata_period.ipynb.expected", ["--extra-keys", "cell.metadata.application/vnd.databricks.v1+cell metadata.application/vnd.databricks.v1+notebook"]), ("test_strip_init_cells.ipynb", "test_strip_init_cells.ipynb.expected", ["--strip-init-cells"]), ("test_nbformat2.ipynb", "test_nbformat2.ipynb.expected", []), ("test_nbformat45.ipynb", "test_nbformat45.ipynb.expected", ["--keep-id"]), ("test_nbformat45.ipynb", "test_nbformat45.ipynb.expected_sequential_id", []), ("test_missing_nbformat.ipynb", "test_missing_nbformat.ipynb.expected", []), ("test_unicode.ipynb", "test_unicode.ipynb.expected", []), ("test_widgets.ipynb", "test_widgets.ipynb.expected", []), ("test_zeppelin.zpln", "test_zeppelin.zpln.expected", ["--mode", "zeppelin"]), ] DRY_RUN_CASES = [ ("test_metadata.ipynb", [], True), ("test_zeppelin.zpln", ["--mode", "zeppelin"], True), ("test_nochange.ipynb", [], False), ] ERR_OUTPUT_CASES = [ ("test_metadata.ipynb", ["Ignoring invalid extra key `invalid`", "Ignoring invalid extra key `foo.invalid`"], ["--extra-keys", "invalid foo.invalid"]), ("test_metadata_exception.ipynb", [re.compile(".*MetadataError: cell metadata contradicts tags: `keep_output` is false, but `keep_output` in tags")], []), ("test_invalid_json.ipynb", ["No valid notebook detected on stdin"], []), ] def nbstripout_exe(): return os.environ.get("NBSTRIPOUT_EXE", "nbstripout") @pytest.mark.parametrize("input_file, expected_file, args", TEST_CASES) @pytest.mark.parametrize("verify", (True, False)) def test_end_to_end_stdin(input_file: str, expected_file: str, args: List[str], verify: bool): with open(NOTEBOOKS_FOLDER / expected_file, mode="r") as f: expected = f.read() with open(NOTEBOOKS_FOLDER / input_file, mode="r") as f: input_ = f.read() with open(NOTEBOOKS_FOLDER / input_file, mode="r") as f: args = [nbstripout_exe()] + args if verify: args.append("--verify") pc = run(args, stdin=f, stdout=PIPE, universal_newlines=True) output = pc.stdout if verify: # When using stin, the dry run flag is disregarded. assert pc.returncode == (1 if input_ != expected else 0) else: assert output == expected assert pc.returncode == 0 @pytest.mark.parametrize("input_file, expected_file, args", TEST_CASES) @pytest.mark.parametrize("verify", (True, False)) def test_end_to_end_file(input_file: str, expected_file: str, args: List[str], tmp_path, verify: bool): with open(NOTEBOOKS_FOLDER / expected_file, mode="r") as f: expected = f.read() p = tmp_path / input_file with open(NOTEBOOKS_FOLDER / input_file, mode="r") as f: p.write_text(f.read()) with open(NOTEBOOKS_FOLDER / input_file, mode="r") as f: input_ = f.read() args = [nbstripout_exe(), p] + args if verify: args.append("--verify") pc = run(args, stdout=PIPE, universal_newlines=True) output = pc.stdout.strip() if verify: if expected != input_: assert "Dry run: would have stripped" in output assert pc.returncode == 1 # Since verify implies --dry-run, we make sure the file is not modified with open(NOTEBOOKS_FOLDER / input_file, mode="r") as f: output_ = f.read() assert output_ == input_ else: assert pc.returncode == 0 assert not pc.stdout and p.read_text() == expected @pytest.mark.parametrize("input_file, extra_args, any_change", DRY_RUN_CASES) @pytest.mark.parametrize("verify", (True, False)) def test_dry_run_stdin(input_file: str, extra_args: List[str], any_change:bool, verify: bool): expected = "Dry run: would have stripped input from stdin\n" with open(NOTEBOOKS_FOLDER / input_file, mode="r") as f: args = [nbstripout_exe(), "--dry-run"] + extra_args if verify: args.append("--verify") pc = run(args, stdin=f, stdout=PIPE, universal_newlines=True) output = pc.stdout assert output == (expected if any_change else '') assert pc.returncode == (1 if verify and any_change else 0) @pytest.mark.parametrize("input_file, extra_args, any_change", DRY_RUN_CASES) @pytest.mark.parametrize("verify", (True, False)) def test_dry_run_args(input_file: str, extra_args: List[str], any_change: bool, verify: bool): expected_regex = re.compile(f"Dry run: would have stripped .*[/\\\\]{input_file}\n") args = [nbstripout_exe(), str(NOTEBOOKS_FOLDER / input_file), "--dry-run", ] + extra_args if verify: args.append("--verify") pc = run(args, stdout=PIPE, universal_newlines=True) output = pc.stdout assert expected_regex.match(output) if any_change else output == '' assert pc.returncode == (1 if verify and any_change else 0) @pytest.mark.parametrize("input_file, expected_errs, extra_args", ERR_OUTPUT_CASES) def test_make_errors(input_file: str, expected_errs: List[Union[str, Pattern]], extra_args: List[str]): with open(NOTEBOOKS_FOLDER / input_file, mode="r") as f: pc = run([nbstripout_exe(), "--dry-run"] + extra_args, stdin=f, stderr=PIPE, universal_newlines=True) err_output = pc.stderr for e in expected_errs: if isinstance(e, Pattern): assert e.search(err_output) else: assert e in err_output python-nbstripout-0.8.1/tests/test_git_integration.py000066400000000000000000000164231475440000300232140ustar00rootroot00000000000000from configparser import ConfigParser from pathlib import Path import re import sys import pytest # fix this before pytester.chdir() happens NOTEBOOKS_FOLDER = Path("tests").absolute() def test_install(pytester: pytest.Pytester): pytester.run('git', 'init') assert pytester.run('nbstripout', '--is-installed').ret == 1 pytester.run('nbstripout', '--install') assert pytester.run('nbstripout', '--is-installed').ret == 0 with open(".git/info/attributes", "r") as f: attr_lines = f.readlines() assert "*.ipynb filter=nbstripout\n" in attr_lines assert "*.zpln filter=nbstripout\n" in attr_lines assert "*.ipynb diff=ipynb\n" in attr_lines config = ConfigParser() config.read(".git/config") assert re.match(r".*python.* -m nbstripout", config['filter "nbstripout"']["clean"]) assert config['filter "nbstripout"']["required"] == "true" assert config['filter "nbstripout"']["smudge"] == "cat" assert re.match(r".*python.* -m nbstripout -t", config['diff "ipynb"']["textconv"]) def test_install_different_python(pytester: pytest.Pytester): pytester.run("git", "init") assert pytester.run("nbstripout", "--is-installed").ret == 1 pytester.run("nbstripout", "--install", "--python", "DIFFERENTPYTHON") assert pytester.run("nbstripout", "--is-installed").ret == 0 config = ConfigParser() config.read(".git/config") assert re.match( r".*DIFFERENTPYTHON.* -m nbstripout", config['filter "nbstripout"']["clean"] ) assert sys.executable not in config['filter "nbstripout"']["clean"] assert config['filter "nbstripout"']["required"] == "true" assert config['filter "nbstripout"']["smudge"] == "cat" assert re.match( r".*DIFFERENTPYTHON.* -m nbstripout -t", config['diff "ipynb"']["textconv"] ) assert sys.executable not in config['diff "ipynb"']["textconv"] def test_uninstall(pytester: pytest.Pytester): pytester.run('git', 'init') # add extra filter at the start, so we can check we don't remove it pytester.path.joinpath(".git/info/attributes").write_text("*.txt text") # do the install and verify pytester.run('nbstripout', '--install') assert pytester.run('nbstripout', '--is-installed').ret == 0 # uninstall and verify (the actual test) pytester.run('nbstripout', '--uninstall') assert pytester.run('nbstripout', '--is-installed').ret == 1 with open(".git/info/attributes", "r") as f: attr_lines = f.readlines() assert "*.txt text\n" in attr_lines # still there and not removed assert len(attr_lines) == 1 config = ConfigParser() config.read(".git/config") assert 'filter "nbstripout"' not in config assert 'diff "ipynb"'not in config def test_uninstall_leave_extrakeys(pytester: pytest.Pytester): pytester.run('git', 'init') # add extrakeys so we can check that we don't remove them pytester.run('git', 'config', 'filter.nbstripout.extrakeys', 'spam eggs') # check not installed assert pytester.run('nbstripout', '--is-installed').ret == 1 # do the install and verify pytester.run('nbstripout', '--install') assert pytester.run('nbstripout', '--is-installed').ret == 0 # uninstall and verify pytester.run('nbstripout', '--uninstall') assert pytester.run('nbstripout', '--is-installed').ret == 1 # check extrakeys still exist r = pytester.run('git', 'config', 'filter.nbstripout.extrakeys') assert r.stdout.str() == 'spam eggs' def test_status(pytester: pytest.Pytester): pytester.run('git', 'init') # status when not installed r = pytester.run('nbstripout', '--status') r.stdout.fnmatch_lines(['nbstripout is not installed in repository *']) assert r.ret == 1 # do the install and verify pytester.run('nbstripout', '--install') r = pytester.run('nbstripout', '--status') assert r.ret == 0 r.stdout.re_match_lines(r"""nbstripout is installed in repository .* \s* Filter: clean = .* -m nbstripout smudge = cat diff= .* -m nbstripout -t extrakeys=\s* \s* Attributes: \*.ipynb: filter: nbstripout \s* Diff Attributes: \*.ipynb: diff: ipynb """.splitlines()) # uninstall and verify pytester.run('nbstripout', '--uninstall') r = pytester.run('nbstripout', '--status') r.stdout.fnmatch_lines(['nbstripout is not installed in repository *']) assert r.ret == 1 def test_git_diff_nodiff(pytester: pytest.Pytester): pytester.run('git', 'init') pytester.run('git', 'config', '--local', 'filter.nbstripout.extrakeys', ' ') pytester.run('nbstripout', '--install') r = pytester.run('git', 'diff', '--no-index', '--no-ext-diff', '--unified=0', '--exit-code', '-a', '--no-prefix', NOTEBOOKS_FOLDER / "test_diff.ipynb", NOTEBOOKS_FOLDER / "test_diff_output.ipynb") assert r.ret == 0 assert not r.outlines def test_git_diff_diff(pytester: pytest.Pytester): pytester.run('git', 'init') pytester.run('git', 'config', '--local', 'filter.nbstripout.extrakeys', ' ') pytester.run('nbstripout', '--install') r = pytester.run('git', 'diff', '--no-index', NOTEBOOKS_FOLDER / "test_diff.ipynb", NOTEBOOKS_FOLDER / "test_diff_different_extrakeys.ipynb") assert r.ret == 1 r.stdout.fnmatch_lines(r"""index* --- *test_diff.ipynb* +++ *test_diff_different_extrakeys.ipynb* @@ -6,15 +6,14 @@ "metadata": {}, "outputs": [], "source": [ - "print(\"aou\")" + "print(\"aou now it is different\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", - "language": "python", - "name": "python2" + "language": "python" }, "language_info": { "codemirror_mode": { """.splitlines()) assert len(r.outlines) == 22 # 21 lines + new line at end def test_git_diff_extrakeys(pytester: pytest.Pytester): pytester.run('git', 'init') pytester.run('git', 'config', '--local', 'filter.nbstripout.extrakeys', 'cell.metadata.collapsed metadata.kernelspec.name') pytester.run('nbstripout', '--install') r = pytester.run('git', 'diff', '--no-index', NOTEBOOKS_FOLDER / "test_diff.ipynb", NOTEBOOKS_FOLDER / "test_diff_different_extrakeys.ipynb") assert r.ret == 1 r.stdout.fnmatch_lines(r"""index* --- *test_diff.ipynb* +++ *test_diff_different_extrakeys.ipynb* @@ -6,7 +6,7 @@ "metadata": {}, "outputs": [], "source": [ - "print(\"aou\")" + "print(\"aou now it is different\")" ] } ], """.splitlines()) assert len(r.outlines) == 13 # 12 lines + new line at end def test_git_diff_keepmetadatakeys(pytester: pytest.Pytester): pytester.run('git', 'init') pytester.run('git', 'config', '--local', 'filter.nbstripout.keepmetadatakeys', 'cell.metadata.scrolled metadata.foo.bar') pytester.run('nbstripout', '--install') r = pytester.run('git', 'diff', '--no-index', NOTEBOOKS_FOLDER / "test_diff.ipynb", NOTEBOOKS_FOLDER / "test_diff_different_extrakeys.ipynb") assert r.ret == 1 r.stdout.fnmatch_lines(r"""index* --- *test_diff.ipynb* +++ *test_diff_different_extrakeys.ipynb* @@ -3,20 +3,17 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "scrolled": true - }, + "metadata": {}, "outputs": [], "source": [ - "print(\"aou\")" + "print(\"aou now it is different\")" ] } ], """.splitlines()) assert len(r.outlines) == 28 # 12 lines + new line at end python-nbstripout-0.8.1/tests/test_keep_output_tags.ipynb000066400000000000000000000044231475440000300240760ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that output is stripped unless the cell has a `keep_output` tag in its metadata" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-03-15T03:18:30.324432Z", "start_time": "2020-03-15T03:18:30.313427Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This cell should have no output, because it does not have keep_output in its metadata (top level) or its tags\n" ] } ], "source": [ "# no_output\n", "print('''This cell should have no output, because it does not have keep_output in its metadata (top level) or its tags''')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-03-15T03:18:30.381986Z", "start_time": "2020-03-15T03:18:30.329424Z" }, "tags": [ "keep_output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This cell has keep_output in its tags metadata\n" ] } ], "source": [ "# output\n", "print('''This cell has keep_output in its tags metadata''')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-03-15T03:18:30.393917Z", "start_time": "2020-03-15T03:18:30.386910Z" }, "keep_output": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This cell has \"keep_output\": true in it's metadata but not in its tags\n" ] } ], "source": [ "# output\n", "print('''This cell has \"keep_output\": true in it's metadata but not in its tags''')" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "local", "language": "python", "name": "local" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 2 } python-nbstripout-0.8.1/tests/test_keep_output_tags.py000066400000000000000000000025121475440000300234020ustar00rootroot00000000000000from copy import deepcopy import os import re import nbformat import pytest from nbstripout import strip_output, MetadataError directory = os.path.dirname(__file__) @pytest.fixture def orig_nb(): fname = 'test_keep_output_tags.ipynb' return nbformat.read(os.path.join(directory, fname), nbformat.NO_CONVERT) @pytest.fixture def nb_with_exception(): fname = 'test_keep_output_tags_exception.ipynb' return nbformat.read(os.path.join(directory, fname), nbformat.NO_CONVERT) def test_cells(orig_nb): nb_stripped = deepcopy(orig_nb) nb_stripped = strip_output(nb_stripped, keep_output=None, keep_count=None, keep_id=None) for i, cell in enumerate(nb_stripped.cells): if cell.cell_type == 'code' and cell.source: match = re.match(r"\s*#\s*(output|no_output)", cell.source) if match: # original cell should have had output. # If not, there's a problem with the test fixture assert orig_nb.cells[i].outputs if match.group(1) == 'output': assert len(cell.outputs) > 0 else: assert len(cell.outputs) == 0 def test_exception(nb_with_exception): with pytest.raises(MetadataError): strip_output(nb_with_exception, keep_output=None, keep_count=None, keep_id=None) python-nbstripout-0.8.1/tests/test_keep_output_tags_exception.ipynb000066400000000000000000000024261475440000300261550ustar00rootroot00000000000000{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook tests that output is stripped unless the cell has a `keep_output` tag in its metadata" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-03-15T03:18:30.406033Z", "start_time": "2020-03-15T03:18:30.398910Z" }, "keep_output": false, "tags": [ "keep_output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This cell should raise an exception, because the metadata and tags conflict in terms of keep_output\n" ] } ], "source": [ "# exception\n", "print('''This cell should raise an exception, because the metadata and tags conflict in terms of keep_output''')" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "local", "language": "python", "name": "local" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 2 } python-nbstripout-0.8.1/tests/test_utils.py000066400000000000000000000020741475440000300211630ustar00rootroot00000000000000import pytest from nbstripout._utils import pop_recursive def make_dict(): return {'a': {'b': 1, 'c': 2, 'd.e': 3, 'f': {'g': 4}}} def make_data(default=None): return [ ('a.c', 2, {'a': {'b': 1, 'd.e': 3, 'f': {'g': 4}}}), ('a.d.e', 3, {'a': {'b': 1, 'c': 2, 'f': {'g': 4}}}), ('a.f', {'g': 4}, {'a': {'b': 1, 'c': 2, 'd.e': 3}}), ('a.f.g', 4, {'a': {'b': 1, 'c': 2, 'd.e': 3, 'f': {}}}), ('a', {'b': 1, 'c': 2, 'd.e': 3, 'f': {'g': 4}}, {}), ('notfound', default, make_dict()), ('a.notfound', default, make_dict()), ('a.b.notfound', default, make_dict()), ] @pytest.fixture def d(): return make_dict() @pytest.mark.parametrize(('key', 'res', 'remainder'), make_data()) def test_pop_recursive(d, key, res, remainder): assert pop_recursive(d, key) == res assert d == remainder @pytest.mark.parametrize(('key', 'res', 'remainder'), make_data(default=0)) def test_pop_recursive_default(d, key, res, remainder): assert pop_recursive(d, key, default=0) == res assert d == remainder