Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/msalvaris/nbtoolz

Set of tools for Jupyter notebooks
https://github.com/msalvaris/nbtoolz

git jupyter

Last synced: 3 months ago
JSON representation

Set of tools for Jupyter notebooks

Awesome Lists containing this project

README

        

# Introduction
This app offers methods to run on notebooks to ensure no sensitive information is contained in the cell or sensitive
output is removed.

It is built using Python Fire so the interface is a little peculiar >:)

# Installation
To install you can simply run the following
```bash
pip install git+https://github.com/msalvaris/nbtoolz.git
```

It has been tested on Ubuntu but should run on Windows.

# How to Use
The app offer 4 methods, 2 that alter the context and 2 that are used as the final output. *strip_output* and *replace*
alter the contents of the notebook. *print* prints the output of the processing to the terminal and *write*
writes the result of the transformations to the file specified. Additionally, you can *overwrite*, which simply writes the results back to the input file.

In the example below we will strip out cells tagged with strip_out tag and replace Team B with and print
out the results to the terminal.
```bash
nbtoolz read test.ipynb - strip_output strip_out replace "Team B" "" print
```

The default tag for strip_output is strip_out so the following does the same.
```bash
nbtoolz read test.ipynb - strip_output - replace "Team B" "" print
```

To tag a cell simply select tags in the cell output menu
![Tag menu Jupyter](images/tag_menu.PNG "Tag Menu")

and add the tag in the text box above the cell
![Tag Jupyter](images/tag.PNG "Tag")

We can also chain more methods
```bash
nbtoolz read test.ipynb - strip_output - replace "Team B" "" replace "TEAM A" "" print
```

To write the output to the file simply run write at the end
```bash
nbtoolz read test.ipynb - strip_output - replace "Team B" "" replace "TEAM A" "" write out.ipynb
```

It also accepts a list of files
```bash
nbtoolz read test.ipynb test2.iynb - strip_output - replace "Team B" "" replace "TEAM A" "" write out.ipynb out2.ipynb
```

or wildcards
```bash
nbtoolz read *.ipynb - strip_output - replace "Team B" "" replace "TEAM A" "" write
```

You can also overwrite your input file
```bash
nbtoolz read test.ipynb - strip_output - replace "Team B" "" replace "TEAM A" "" overwrite
```

# Configure git filter
You can configure git filter to use nbtoolz to ensure that you never commit any sensitive information. To do so once you
have installed nbtoolz

Simply create a shell script called nbfilter that accepts a single argument and add it to your path or .bashrc
```bash
#!/usr/bin/env bash

nbtoolz read-stdin strip_output - replace "Team" "" replace "Team2" "" print

```

Then you can either edit the .git/config file and add the following
```text
[filter "nbclean"]
clean = nbfilter
smudge = cat
```

or run the following commands

```bash
git config filter.nbclean.clean nbfilter
git config filter.nbclean.smudge cat
git config filter.nbclean.required true
```

then create a .gitattributes file in your project directory and add the following
```text
*.ipynb filter=nbclean
```

# Contribute
Please post issues, PRs welcome :)