Automatically save matplotlib figures to files and link them in Jupyter notebooks instead of embedding them.
Project description
dietnb
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
.ipynbfile bulk by storingmatplotlibfigures as external PNG files. - Automatic Image Folder Management: Creates and manages image storage directories (e.g.,
[NotebookFileName]_dietnb_imgs) relative to the notebook's location. (Applies when path detection is successful, e.g., in VS Code; defaults todietnb_imgsin the current working directory if detection fails.) - Automatic Image Updates: When a notebook cell is re-executed, image files generated from its previous run are automatically deleted, ensuring only the latest output is retained.
- Image Cleanup Function: The
dietnb.clean_unused()function allows easy removal of unreferenced image files from the current session. - Simple Auto-Activation: The
dietnb installcommand configuresdietnbto 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, 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dietnb-0.1.7.tar.gz.
File metadata
- Download URL: dietnb-0.1.7.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd11f0b6d755295f073050d3a9adc285c4a8eb5bdb2db523a0c4b67ba72632b5
|
|
| MD5 |
7058b870adf38178fe900ca0bf4e58ab
|
|
| BLAKE2b-256 |
c9aa23b72f395af9e2e7247ec5c1b4011eb7bf3b7b273eca300b48ad6c98657b
|
File details
Details for the file dietnb-0.1.7-py3-none-any.whl.
File metadata
- Download URL: dietnb-0.1.7-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a7bffdeb877c0ebf339b45eaae06d2ece069838095998ae5e80fa860efa6883
|
|
| MD5 |
3598ee2f73b5262b0c02c4f99df604eb
|
|
| BLAKE2b-256 |
e74765177a9fb913d35069265ce414852b40ee3a911109eef80024344f103006
|