CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-nbdev

Create delightful software with Jupyter Notebooks

Pending
Overview
Eval results
Files

synchronization.mddocs/

Code Synchronization

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.

Capabilities

Main Synchronization Function

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 cells

Import Management

Convert 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'

Synchronization Process

Two-Way Sync Workflow

  1. Notebook → Python: Export notebook cells to Python modules
  2. Edit in IDE: Modify Python files using IDE features
  3. Python → Notebook: Update notebooks with IDE changes
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()

What Gets Synchronized

The sync system handles:

  • Function definitions: Updates function code and signatures
  • Class definitions: Syncs class methods and attributes
  • Documentation: Updates docstrings and comments
  • Imports: Maintains import statements
  • Variable assignments: Syncs module-level variables

What Doesn't Get Synchronized

  • Cell outputs: Execution results remain in notebooks
  • Markdown cells: Text content stays in notebooks
  • Cell metadata: Notebook-specific metadata preserved
  • Cell order: Notebook structure maintained

IDE Integration

Supported Workflows

VS Code Integration:

# Install Python extension for VS Code
# Edit .py files with full IntelliSense
# Use nbdev_update() to sync back to notebooks

PyCharm 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 done

Import Resolution

The 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 environment

Configuration

Synchronization 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_path

Advanced Usage

Selective Synchronization

from 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_nb

Integration with Development Workflow

from 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()

Handling Conflicts

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 review

Best Practices

Recommended Workflow

  1. Start with notebooks: Write and test code in notebooks
  2. Export frequently: Use nb_export() regularly
  3. IDE editing: Make larger refactors in IDE
  4. Sync back: Use nbdev_update() to bring changes back
  5. Test: Always run nbdev_test() after syncing

File Organization

project/
├── 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 Collaboration

# 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

Automated Synchronization

# Git hooks can automate sync
# Pre-commit hook:
nbdev_export

# Post-merge hook:
nbdev_update

Integration with Other nbdev Features

With Testing

from nbdev.sync import nbdev_update
from nbdev.test import nbdev_test

# After syncing, run tests
nbdev_update()
nbdev_test()  # Verify sync didn't break anything

With Documentation

from nbdev.sync import nbdev_update
from nbdev.doclinks import nbdev_export

# Sync and regenerate docs
nbdev_update()
nbdev_export()  # Update documentation

With Cleaning

from nbdev.sync import nbdev_update
from nbdev.clean import nbdev_clean

# Clean notebooks after sync
nbdev_update()
nbdev_clean()  # Clean up metadata

Complete 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

docs

cleaning.md

configuration.md

development-tools.md

documentation.md

export.md

git-integration.md

index.md

release-management.md

synchronization.md

testing.md

tile.json