Skip to main content

IPython extension to mask sensitive data

Project description

IPython extension to mask sensitive data

Simple ipython extension to mask sensitive data like credentials from notebook cell outputs. This extensions may be useful to avoid inadvertently displaying sensitive data in public notebooks. The extension modifies the ipython display system to try and mask any occurence of any of the given strings that appear in textual outputs from printing, logging and the native display system.

Usage

To load the extension use the magic command %load_ext nbmask. The extension will automatically mask the notebook display textual outputs without any further magic commands. When first loaded the extension will mask just the username.

import os
from pathlib import Path

%load_ext nbmask

username = os.getenv('USER')
print("My name is {username}!")
# >>> My name is ...!

documents = Path(f"~/Documents").expanduser()
documents
# >>> PosixPath('/Users/.../Documents')

You can add more secrets with the %nbmask magic line command and a string parameter using python builtin variable expansion syntax.

TOKEN = my_secret_token()

%nbmask "$TOKEN"

credentials = dict(user=username, token=TOKEN)

credentials
# >>> {'user': '...', 'token': '...'}

The extension modifies standard output, standard error and also the default logging handlers.

import logging

logging.basicConfig(level="DEBUG")

logging.debug("Token is %s", TOKEN)
# >>> DEBUG:root:Token is ...

Example

See nbmask-tests.ipynb in extras

Installation

You can install the current version of this package with pip

pip install nbmask

Changelog

0.05

  • Now using uv_build backend
  • Added testing with pytest and nbmake
  • Requires python >= 3.10

0.0.4

  • Cell magic %%masked is no longer needed. Will be removed

0.0.3

  • Masking pattern is cached

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nbmask-0.0.5-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file nbmask-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: nbmask-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for nbmask-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4694da079fa099cc727557492c8357ff240521574c55ff8383a309be33a26bc5
MD5 00bdff7e9c35c32b1aa718f0199dfc31
BLAKE2b-256 7fadec553f37bd55ad3f4c913e60760aa1afe09623c9a79051299aa180221265

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page