Auto-generate folder_name.ipynb notebooks for nbdev project organization
Notebook Generation
Folder Notebook Generation
generate_folder_notebook
def generate_folder_notebook( folder_path:Path, # Path to folder title:Optional[str]=None, # Custom title description:Optional[str]=None, # Custom description overwrite:bool=False, # Overwrite existing)->Path: # Path to created notebook
Generate a folder_name.ipynb notebook for a folder
generate_all_folder_notebooks
def generate_all_folder_notebooks( 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)->List[Path]: # Created notebook paths
Generate folder notebooks for all folders that don’t have them
Interactive Folder Notebook Generation
interactive_folder_notebook_generator
def interactive_folder_notebook_generator( base_path:Path=None, # Base path)->List[Path]: # Created notebooks
Interactively generate folder notebooks with custom titles and descriptions
Testing
Let’s test the generator functionality:
# First, let's check if there are any folders in our projectsubdirs = 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 functionalitytest_folder = Path("test_folder")ifnot 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 foldertry: 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 inenumerate(nb.cells):print(f"\nCell {i+1} ({cell.cell_type}):")print(cell.source)exceptExceptionas 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 folderimport shutilif test_folder.exists(): shutil.rmtree(test_folder)print(f"Cleaned up test folder: {test_folder}")