Core Utilities

Core utilities and data models for nbdev project overview generation

Data Models

First, let’s define the data models we’ll use throughout the project:


NotebookInfo


def NotebookInfo(
    path:Path, name:str, title:Optional[str]=None, description:Optional[str]=None, export_module:Optional[str]=None
)->None:

Information about a single notebook


DirectoryInfo


def DirectoryInfo(
    path:Path, name:str, notebook_count:int=0, description:Optional[str]=None, subdirs:List[DirectoryInfo]=<factory>,
    notebooks:List[NotebookInfo]=<factory>
)->None:

Information about a directory in the nbs folder

Path Utilities


get_notebook_files


def get_notebook_files(
    path:Path=None, # Directory to search (defaults to nbs_path)
    recursive:bool=True, # Search subdirectories
)->List[Path]: # List of notebook paths

Get all notebook files in a directory


get_subdirectories


def get_subdirectories(
    path:Path=None, # Directory to search (defaults to nbs_path)
    recursive:bool=False, # Include all nested subdirectories
)->List[Path]: # List of directory paths

Get subdirectories in a directory

Notebook Reading Utilities


read_notebook


def read_notebook(
    path:Path, # Path to notebook file
)->Dict[str, Any]: # Notebook content as dict

Read a notebook file and return its content


get_cell_source


def get_cell_source(
    cell:Dict[str, Any], # Notebook cell
)->str: # Cell source as string

Get source from a notebook cell

Testing

Let’s test our utilities on the current project:

# Test getting notebook files
notebooks = get_notebook_files()
print(f"Found {len(notebooks)} notebooks:")
for nb in notebooks[:5]:  # Show first 5
    print(f"  - {nb.name}")
Found 8 notebooks:
  - 00_core.ipynb
  - 01_parsers.ipynb
  - 02_tree.ipynb
  - 03_api_docs.ipynb
  - 04_dependencies.ipynb