Create delightful software with Jupyter Notebooks
—
Synchronize between notebooks and plain Python source code for IDE compatibility. The synchronization system enables two-way sync between notebook cells and Python modules, allowing developers to use IDEs while maintaining notebook-driven development.
Update notebooks from changes made to Python source files.
def nbdev_update():
"""
Update notebooks from source code changes.
Synchronizes notebooks with changes made to Python modules,
updating notebook cells with modified functions, classes,
and other code elements that were changed in the exported
Python files.
"""Usage Example:
from nbdev.sync import nbdev_update
# After editing Python files in your IDE
nbdev_update()
# This updates the corresponding notebook cellsConvert between different import styles for better IDE support.
def absolute_import(name: str, fname: str, level: int = 0):
"""
Convert to absolute imports.
Args:
name: Import name to convert
fname: Source file name
level: Relative import level
Returns:
Absolute import statement suitable for IDE tools
Converts relative imports to absolute imports for better
IDE support and static analysis tools.
"""Usage Example:
from nbdev.sync import absolute_import
# Convert relative import to absolute
abs_import = absolute_import('core', 'mylib/utils.py', level=1)
print(abs_import) # 'mylib.core' instead of '.core'from nbdev.export import nb_export
from nbdev.sync import nbdev_update
# Step 1: Export notebooks to Python
nb_export()
# Step 2: Edit Python files in your IDE
# ... make changes to .py files ...
# Step 3: Sync changes back to notebooks
nbdev_update()The sync system handles:
VS Code Integration:
# Install Python extension for VS Code
# Edit .py files with full IntelliSense
# Use nbdev_update() to sync back to notebooksPyCharm Integration:
# Open exported Python modules in PyCharm
# Use refactoring tools, debugger, etc.
# Sync changes back with nbdev_update()Vim/Emacs Integration:
# Edit Python files with preferred editor
# Use language servers for Python support
# Sync back to notebooks when doneThe sync system handles complex import scenarios:
from nbdev.sync import absolute_import
# Handles relative imports
from .core import function_name
# Converts to absolute for IDE support
from mypackage.core import function_name
# Maintains compatibility with notebook environmentSynchronization respects project configuration:
# In settings.ini
lib_path = mylib
nbs_path = notebooks
# Sync will update notebooks in nbs_path based on
# changes to Python files in lib_pathfrom nbdev.sync import nbdev_update
from pathlib import Path
# Update specific notebook
# (implementation varies - this shows the concept)
specific_nb = Path('notebooks/01_core.ipynb')
nbdev_update() # Updates all notebooks including specific_nbfrom nbdev.sync import nbdev_update
from nbdev.export import nb_export
from nbdev.test import nbdev_test
def development_cycle():
"""Complete development cycle with sync."""
# 1. Export notebooks to Python
print("Exporting notebooks...")
nb_export()
# 2. Now edit Python files in your IDE
print("Edit Python files in your IDE, then press Enter...")
input()
# 3. Sync changes back to notebooks
print("Syncing changes back to notebooks...")
nbdev_update()
# 4. Run tests to verify everything works
print("Running tests...")
nbdev_test()
print("Development cycle complete!")
development_cycle()When synchronization encounters conflicts:
# If sync detects conflicts, manual resolution may be needed
# The system will typically:
# 1. Warn about conflicts
# 2. Preserve notebook structure
# 3. Update code content where possible
# 4. Leave markers for manual reviewnb_export() regularlynbdev_update() to bring changes backnbdev_test() after syncingproject/
├── notebooks/ # Source notebooks
│ ├── 01_core.ipynb
│ └── 02_utils.ipynb
├── mylib/ # Exported Python modules
│ ├── core.py # From 01_core.ipynb
│ └── utils.py # From 02_utils.ipynb
└── settings.ini # Configuration# Team workflow with sync
from nbdev.sync import nbdev_update
from nbdev.export import nb_export
# Before starting work
nbdev_update() # Get latest notebook updates
# After making changes in IDE
nb_export() # Export to Python for others
# Commit both notebooks and Python files
# After pulling from team
nbdev_update() # Update notebooks with team changes# Git hooks can automate sync
# Pre-commit hook:
nbdev_export
# Post-merge hook:
nbdev_updatefrom nbdev.sync import nbdev_update
from nbdev.test import nbdev_test
# After syncing, run tests
nbdev_update()
nbdev_test() # Verify sync didn't break anythingfrom nbdev.sync import nbdev_update
from nbdev.doclinks import nbdev_export
# Sync and regenerate docs
nbdev_update()
nbdev_export() # Update documentationfrom nbdev.sync import nbdev_update
from nbdev.clean import nbdev_clean
# Clean notebooks after sync
nbdev_update()
nbdev_clean() # Clean up metadataComplete Sync Example:
from nbdev.sync import nbdev_update, absolute_import
from nbdev.export import nb_export
from nbdev.config import get_config
# Check configuration
config = get_config()
print(f"Syncing between {config.nbs_path} and {config.lib_path}")
# Export notebooks to enable IDE editing
nb_export()
print("Python modules ready for IDE editing")
# After editing in IDE, sync back
print("After editing Python files, sync back to notebooks:")
nbdev_update()
print("Notebooks updated with IDE changes")Install with Tessl CLI
npx tessl i tessl/pypi-nbdev