Extract Command Examples¶
Practical examples for using nbctl extract to save notebook outputs.
Basic Usage¶
Extract All Outputs (Interactive)¶
Prompts:
What would you like to extract?
1. Both data and images
2. Only data outputs
3. Only image outputs
4. All outputs without prompting
Enter choice (1-4):
Extract Everything (Non-Interactive)¶
Result:
outputs/
├── data/
│ └── cell_0_output_0_data_0.json
└── images/
├── cell_2_output_0_img_0.png
└── cell_5_output_0_img_1.svg
Extract Only Images¶
Perfect for saving plots and visualizations.
Extract Only Data¶
Perfect for saving DataFrames and JSON outputs.
Custom Output Directory¶
Save to Custom Location¶
Organized Extraction¶
# Extract images to figures directory
nbctl extract analysis.ipynb --images -o ./report/figures/
# Extract data to data directory
nbctl extract analysis.ipynb --data -o ./report/data/
Workflow Examples¶
Create Report Package¶
Extract outputs for a report:
#!/bin/bash
# create-report-package.sh
notebook="analysis.ipynb"
report_dir="report-$(date +%Y%m%d)"
mkdir -p "$report_dir"
# Extract all outputs
nbctl extract "$notebook" --all -o "$report_dir/outputs/"
# Export to HTML
nbctl export "$notebook" -f html --output-dir "$report_dir/"
# Create archive
tar -czf "$report_dir.tar.gz" "$report_dir"
echo "Report package: $report_dir.tar.gz"
Extract for Publication¶
Save figures for academic paper:
# Create publication-ready figures directory
mkdir -p paper/figures
# Extract all plots
nbctl extract ml_results.ipynb --images -o paper/figures/
# Rename for paper
cd paper/figures
mv cell_3_output_0_img_0.png figure1_accuracy.png
mv cell_7_output_0_img_1.png figure2_confusion_matrix.png
Batch Extraction¶
Extract from multiple notebooks:
#!/bin/bash
# Extract outputs from all analysis notebooks
for nb in analysis_*.ipynb; do
name=$(basename "$nb" .ipynb)
nbctl extract "$nb" --all -o "outputs/$name/"
done
echo "Extracted outputs from all notebooks"
Data Extraction Examples¶
Save DataFrames¶
Extract pandas DataFrame HTML tables:
Result: DataFrames saved as HTML tables.
Extract JSON Results¶
Save model evaluation metrics:
Extract CSV Data¶
Image Extraction Examples¶
Save All Plots¶
Extract matplotlib/seaborn plots:
Save Vector Graphics¶
Extract SVG for scalable figures:
Extract for Presentation¶
# Extract plots for slides
nbctl extract analysis.ipynb --images -o presentation/images/
# Use in PowerPoint, Google Slides, etc.
Advanced Examples¶
Conditional Extraction¶
Extract only if outputs exist:
if nbctl info notebook.ipynb | grep -q "outputs"; then
nbctl extract notebook.ipynb --all
echo "Outputs extracted"
else
echo "ℹ No outputs to extract"
fi
Extract with Timestamp¶
Extract and Archive¶
# Extract outputs
nbctl extract notebook.ipynb --all -o temp-outputs/
# Create archive
tar -czf outputs-$(date +%Y%m%d).tar.gz temp-outputs/
# Cleanup
rm -rf temp-outputs/
echo "Outputs archived"
Automated Workflows¶
Daily Plot Extraction¶
#!/bin/bash
# daily-plots.sh - Extract plots daily
date=$(date +%Y-%m-%d)
plot_dir="plots/$date"
mkdir -p "$plot_dir"
for nb in dashboards/*.ipynb; do
# Run notebook to generate latest plots
nbctl run "$nb"
# Extract plots
name=$(basename "$nb" .ipynb)
nbctl extract "$nb" --images -o "$plot_dir/$name/"
done
echo "Daily plots extracted to $plot_dir"
CI/CD Integration¶
.github/workflows/extract-outputs.yml:
name: Extract Outputs
on:
push:
paths:
- '**.ipynb'
jobs:
extract:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
- name: Install nbctl
run: pip install nbctl
- name: Extract outputs
run: |
mkdir -p artifacts
for nb in *.ipynb; do
nbctl extract "$nb" --all -o "artifacts/${nb%.ipynb}/"
done
- name: Upload artifacts
uses: actions/upload-artifact@v2
with:
name: notebook-outputs
path: artifacts/
Organization Patterns¶
By Notebook¶
# Structure: outputs/{notebook_name}/data/ and /images/
for nb in *.ipynb; do
name=$(basename "$nb" .ipynb)
nbctl extract "$nb" --all -o "outputs/$name/"
done
Result:
By Date¶
By Type¶
# All images in one place
nbctl extract *.ipynb --images -o all-images/
# All data in one place
nbctl extract *.ipynb --data -o all-data/
Tips & Best Practices¶
1. Run Before Extract¶
2. Organize Extraction¶
# Create clear structure
project/
├── notebooks/
├── outputs/
│ ├── figures/
│ └── data/
└── reports/
3. Version Outputs¶
# Keep versions
nbctl extract notebook.ipynb --all -o "outputs/v1/"
# ... make changes ...
nbctl extract notebook.ipynb --all -o "outputs/v2/"
4. Document Outputs¶
Create outputs/README.md:
# Outputs
Generated from: analysis.ipynb
Date: 2025-11-12
## Images
- cell_3_output_0_img_0.png: Model accuracy plot
- cell_7_output_0_img_1.png: Confusion matrix
## Data
- cell_1_output_0_data_0.json: Model metrics
Related Examples¶
- Export Examples - Export entire notebook
- Run Examples - Execute to generate outputs
- ML-Split Examples - Extract code structure