Auto-generation Utilities

Auto-generate folder_name.ipynb notebooks for nbdev project organization

Notebook Generation

Folder Notebook Generation


source

generate_folder_notebook

 generate_folder_notebook (folder_path:pathlib.Path,
                           title:Optional[str]=None,
                           description:Optional[str]=None,
                           overwrite:bool=False)

Generate a folder_name.ipynb notebook for a folder

Type Default Details
folder_path Path Path to folder
title Optional None Custom title
description Optional None Custom description
overwrite bool False Overwrite existing
Returns Path Path to created notebook

source

generate_all_folder_notebooks

 generate_all_folder_notebooks (base_path:pathlib.Path=None,
                                recursive:bool=True, overwrite:bool=False,
                                dry_run:bool=False)

Generate folder notebooks for all folders that don’t have them

Type Default Details
base_path Path None Base path (defaults to nbs)
recursive bool True Include nested folders
overwrite bool False Overwrite existing
dry_run bool False Just show what would be created
Returns List Created notebook paths

Interactive Folder Notebook Generation


source

interactive_folder_notebook_generator

 interactive_folder_notebook_generator (base_path:pathlib.Path=None)

Interactively generate folder notebooks with custom titles and descriptions

Type Default Details
base_path Path None Base path
Returns List Created notebooks

Testing

Let’s test the generator functionality:

# First, let's check if there are any folders in our project
subdirs = get_subdirectories()
print(f"Found {len(subdirs)} subdirectories in nbs/")
for d in subdirs:
    nb_path = d / f"{d.name}.ipynb"
    exists = "✓" if nb_path.exists() else "✗"
    print(f"{exists} {d.name}/")
Found 0 subdirectories in nbs/
# Create a test folder to demonstrate the functionality
test_folder = Path("test_folder")
if not test_folder.exists():
    test_folder.mkdir()
    print(f"Created test folder: {test_folder}")
    
    # Create a sample notebook in the folder
    sample_nb = test_folder / "sample.ipynb"
    cells = [
        mk_cell("# Sample Notebook\n\n> A sample notebook for testing", cell_type='markdown'),
        mk_cell("print('Hello from sample notebook!')", cell_type='code')
    ]
    nb = new_nb(cells)
    write_nb(nb, sample_nb)
    print(f"Created sample notebook: {sample_nb}")
else:
    print(f"Test folder already exists: {test_folder}")
Created test folder: test_folder
Created sample notebook: test_folder/sample.ipynb
# Generate a folder notebook for the test folder
try:
    nb_path = generate_folder_notebook(
        test_folder, 
        title="Test Folder", 
        description="Testing folder notebook generation"
    )
    print(f"\nGenerated notebook at: {nb_path}")
    
    # Read and display the generated notebook content
    nb = read_nb(nb_path)
    print("\nGenerated notebook contents:")
    print("-" * 50)
    for i, cell in enumerate(nb.cells):
        print(f"\nCell {i+1} ({cell.cell_type}):")
        print(cell.source)
except Exception as e:
    print(f"Error: {e}")
Created notebook: /mnt/SN850X_8TB_EXT4/Projects/GitHub/cj-mills/cjm-nbdev-overview/nbs/test_folder/test_folder.ipynb

Generated notebook at: /mnt/SN850X_8TB_EXT4/Projects/GitHub/cj-mills/cjm-nbdev-overview/nbs/test_folder/test_folder.ipynb

Generated notebook contents:
--------------------------------------------------

Cell 1 (markdown):
# Test Folder

> Testing folder notebook generation

Cell 2 (code):


Cell 3 (code):
#| export
# This module serves as the package root for this folder

Cell 4 (markdown):
## Overview

This folder contains notebooks related to testing folder notebook generation.

Cell 5 (markdown):
## Notebooks in this folder:

- `sample.ipynb`: A sample notebook for testing

Cell 6 (code):
#| hide
import nbdev; nbdev.nbdev_export()
# Clean up test folder
import shutil
if test_folder.exists():
    shutil.rmtree(test_folder)
    print(f"Cleaned up test folder: {test_folder}")
Cleaned up test folder: test_folder