CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-nbdev

Create delightful software with Jupyter Notebooks

Pending
Overview
Eval results
Files

export.mddocs/

Notebook Export

Convert Jupyter notebook cells to Python modules with proper imports, documentation, and structure. The export system processes notebook cells marked with special directives and creates well-structured Python packages.

Capabilities

Main Export Function

Export notebooks to Python modules based on project configuration.

def nb_export():
    """
    Export notebooks to Python modules.
    
    Processes all notebooks in the project's nbs_path, extracting cells marked
    with #|export directives and converting them to proper Python modules
    in the lib_path directory. Handles imports, documentation, and code structure.
    """

Usage Example:

from nbdev.export import nb_export

# Export all notebooks in project to Python modules
nb_export()

Export Processor

Core processor class for handling notebook-to-module conversion.

class ExportModuleProc:
    """
    A processor which exports code to a module.
    
    Processes notebook cells and organizes them into Python modules
    based on export directives and default export destinations.
    """
    
    def begin(self):
        """Initialize module collections and __all__ tracking."""
    
    def _default_exp_(self, cell, exp_to: str):
        """Set default export destination for subsequent cells."""
    
    def _export_(self, cell, exp_to: str = None):
        """Export cell to module and include in __all__."""
    
    def _exporti_(self, cell, exp_to: str = None):
        """Export cell to module without including in __all__."""
    
    def __call__(self, cell):
        """Process a single notebook cell."""

Usage Example:

from nbdev.export import ExportModuleProc
from nbdev.process import NBProcessor

# Create processor for a specific notebook
proc = ExportModuleProc()
processor = NBProcessor('example.ipynb', procs=[proc])
processor.process()

# Access exported modules
for module_name, cells in proc.modules.items():
    print(f"Module {module_name}: {len(cells)} cells")

Code Formatting

Format exported code using black for consistent style.

def black_format(cell, force: bool = False):
    """
    Processor to format code with black.
    
    Args:
        cell: Notebook cell to format
        force: Turn black formatting on regardless of settings.ini
        
    Formats cell source code using black if black_formatting is enabled
    in configuration or force is True. Only processes code cells.
    
    Raises:
        ImportError: If black is not installed but formatting is requested
    """

Usage Example:

from nbdev.export import black_format
from execnb.nbio import NbCell

# Create a code cell
cell = NbCell('def hello():\n  print("world")', 'code')

# Format with black
black_format(cell, force=True)
print(cell.source)  # Properly formatted code

Magic Command Processing

Remove or process IPython magic commands from exported code.

def scrub_magics(cell):
    """
    Remove IPython magics from cell source.
    
    Args:
        cell: Notebook cell to process
        
    Removes or converts IPython magic commands (lines starting with % or %%)
    that shouldn't appear in exported Python modules.
    """

Optional Processors

Get list of optional processors for export pipeline.

def optional_procs():
    """
    Get list of optional processors for export.
    
    Returns:
        List of processor classes that can be optionally included
        in the export pipeline based on configuration settings.
        
    Optional processors may include:
    - black_format: Code formatting
    - Additional custom processors
    """

Export Directives

nbdev uses special cell directives to control export behavior:

Basic Export Directives

  • #|default_exp module_name: Set default export destination
  • #|export: Export cell to default module and include in all
  • #|exporti: Export cell to default module but don't include in all
  • #|exports: Alias for #|export

Targeted Export

  • #|export module_name: Export to specific module
  • #|exporti module_name: Export to specific module without all

Usage in Notebooks:

# In notebook cell 1:
#|default_exp core

# In notebook cell 2:  
#|export
def public_function():
    """This will be in __all__"""
    pass

# In notebook cell 3:
#|exporti  
def _private_function():
    """This won't be in __all__"""
    pass

# In notebook cell 4:
#|export utils
def utility_function():
    """This goes to utils module"""
    pass

Export Process

The export process follows these steps:

  1. Discovery: Find all notebooks in nbs_path
  2. Processing: Process each notebook through the export pipeline
  3. Cell Analysis: Extract cells with export directives
  4. Module Generation: Group cells by destination module
  5. Code Generation: Create Python files with proper structure
  6. Import Handling: Add necessary imports and all declarations
  7. Formatting: Apply black formatting if enabled
  8. File Writing: Write modules to lib_path directory

Complete Example:

from nbdev.export import nb_export, ExportModuleProc
from nbdev.config import get_config

# Check configuration
config = get_config()
print(f"Notebooks: {config.nbs_path}")
print(f"Library: {config.lib_path}")

# Export all notebooks
nb_export()

# The export creates:
# - Python modules in lib_path
# - Proper __init__.py files  
# - __all__ declarations
# - Import statements
# - Documentation strings

Integration with nbdev Pipeline

The export system integrates with other nbdev components:

  • Configuration: Uses settings from get_config()
  • Processing: Leverages NBProcessor for cell handling
  • Documentation: Exports docstrings for show_doc
  • Testing: Exported modules can be tested with nbdev_test
  • Cleaning: Works with nbdev_clean for notebook maintenance

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