Skip to main content

Automatically save matplotlib figures to files and link them in Jupyter notebooks instead of embedding them.

Project description

한국어 README (Korean README)


dietnb

PyPI version License: MIT Buy Me A Coffee

dietnb addresses the issue of large .ipynb file sizes caused by matplotlib figures being embedded as Base64 data. By saving plots as external PNG files and embedding only image links, dietnb keeps your notebooks lightweight and improves manageability.


Key Features

  • Minimized Notebook Size: Significantly reduces .ipynb file bulk by storing matplotlib figures as external PNG files.
  • One-Click Image Copy: Every plot includes an automatically added 📋 copy button for instant clipboard access.
  • Automatic Image Folder Management: Creates and manages image storage directories (e.g., [NotebookFileName]_dietnb_imgs) relative to the notebook's location. When you move the notebook and the folder together, the <img> tags continue to work because only relative paths are stored. If detection fails, the default dietnb_imgs folder in the working directory is used.
  • Automatic Image Updates: Registers per-directory/per-cell execution counts to replace old PNGs when a cell reruns, preventing stale images from piling up.
  • Image Cleanup Function: The dietnb.clean_unused() function consults the execution registry to remove unreferenced image files from the active session.
  • Simple Auto-Activation: The dietnb install command configures dietnb to activate automatically when IPython and Jupyter environments start.

Installation and Activation

1. Install the dietnb package

Execute the following command in your terminal:

pip install dietnb

2. Choose an Activation Method

A. Automatic Activation (Recommended) Run the following command in your terminal once:

dietnb install

This creates a startup script (00-dietnb.py) in your IPython profile directory. After restarting your Jupyter kernel, dietnb will be activated automatically. Images will be saved to a folder based on the notebook's path or to the default dietnb_imgs directory.

To disable automatic activation later, run:

dietnb uninstall

This removes the startup script.

B. Manual Activation (Per Notebook) If you prefer to use dietnb only for specific notebooks or do not want automatic activation, add the following code at the top of your notebook to activate it manually:

import dietnb
dietnb.activate()

Example Usage

With dietnb active, use your matplotlib code as usual.

import matplotlib.pyplot as plt
import numpy as np

# Create a plot
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x), label='sin(x)')
plt.plot(x, np.cos(x), label='cos(x)')
plt.title("Trigonometric Functions")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()

plt.show() # On show(), the image is saved to a file, and a link is displayed in the notebook.

Generated images can be found in the [NotebookFileName]_dietnb_imgs folder alongside your notebook (with relative links), or in the dietnb_imgs folder.


Cleaning Unused Image Files

To remove image files that are no longer in use, execute the following function in a notebook cell:

import dietnb
dietnb.clean_unused()

License

MIT License. See LICENSE for details.

Project details


Download files

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

Source Distribution

dietnb-0.2.2.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

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

dietnb-0.2.2-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file dietnb-0.2.2.tar.gz.

File metadata

  • Download URL: dietnb-0.2.2.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for dietnb-0.2.2.tar.gz
Algorithm Hash digest
SHA256 7a4e7b3c9a37b254df81b2f81586390b346f9d634d884e1b8fe95694061621f2
MD5 484ce2c9e1d8735e6a5f31c59d46cbc5
BLAKE2b-256 3152a93ce629e643f0752a27aa0b446a8b2e7d84af22cb8be00f51e76f9d1a4e

See more details on using hashes here.

File details

Details for the file dietnb-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: dietnb-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for dietnb-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5fd6e4c50c2ef458f4d15821fbfbcfd8c5053e4a48d2133d3af98430b74bfcf6
MD5 bad81d4aaf900cfe8c7d9ce06dfef5c2
BLAKE2b-256 65c206b7cfc2b58b33a1959da4e7e200f2d1f1f7decd252a4860bc753f20cea8

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