Create delightful software with Jupyter Notebooks
—
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.
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()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")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 codeRemove 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.
"""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
"""nbdev uses special cell directives to control export behavior:
#|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#|export module_name: Export to specific module#|exporti module_name: Export to specific module without allUsage 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"""
passThe export process follows these steps:
nbs_pathlib_path directoryComplete 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 stringsThe export system integrates with other nbdev components:
get_config()NBProcessor for cell handlingshow_docnbdev_testnbdev_clean for notebook maintenanceInstall with Tessl CLI
npx tessl i tessl/pypi-nbdev