Directory Tree Visualization

Generate tree visualizations for nbdev project structure

Basic Tree Generation

First, let’s implement the basic tree generation without descriptions:


generate_tree_lines


def generate_tree_lines(
    path:Path, # Directory to visualize
    prefix:str='', # Line prefix for tree structure
    is_last:bool=True, # Is this the last item in parent
    show_notebooks_only:bool=False, # Only show notebooks, not directories
    max_depth:Optional[int]=None, # Maximum depth to traverse
    current_depth:int=0, # Current depth in traversal
    exclude_index:bool=True, # Exclude index.ipynb from tree
    exclude_empty:bool=True, # Exclude empty directories
)->List[str]: # Lines of tree output

Generate tree visualization lines for a directory


generate_tree


def generate_tree(
    path:Path=None, # Directory to visualize (defaults to nbs_path)
    show_notebooks_only:bool=False, # Only show notebooks, not directories
    max_depth:Optional[int]=None, # Maximum depth to traverse
    exclude_index:bool=True, # Exclude index.ipynb from tree
    exclude_empty:bool=True, # Exclude empty directories
)->str: # Tree visualization as string

Generate a tree visualization for a directory

Testing Basic Tree

Let’s test the basic tree generation on our project:

# Test basic tree generation
print(generate_tree(show_notebooks_only=True))
nbs/
├── api_docs.ipynb
├── cli.ipynb
├── core.ipynb
├── dependencies.ipynb
├── generators.ipynb
├── parsers.ipynb
└── tree.ipynb

Parsing Notebook Information

Now let’s add functions to extract descriptions from notebooks:


extract_notebook_info


def extract_notebook_info(
    path:Path, # Path to notebook file
)->NotebookInfo: # Notebook information

Extract title and description from a notebook

# Test extracting notebook info
nb_info = extract_notebook_info(Path("core.ipynb"))
print(f"Title: {nb_info.title}")
print(f"Description: {nb_info.description}")
print(f"Export module: {nb_info.export_module}")
Title: Core Utilities
Description: Core utilities and data models for nbdev project overview generation
Export module: core

Tree Generation with Descriptions

Now let’s create the enhanced tree generation that includes descriptions:


generate_tree_with_descriptions


def generate_tree_with_descriptions(
    path:Path=None, # Directory to visualize
    show_counts:bool=True, # Show notebook counts for directories
    max_depth:Optional[int]=None, # Maximum depth to traverse
    exclude_index:bool=True, # Exclude index.ipynb from tree
    exclude_empty:bool=True, # Exclude empty directories
)->str: # Tree with descriptions

Generate tree visualization with descriptions from notebooks

Testing Tree with Descriptions

Let’s test the enhanced tree on our project:

Subdirectory Tree Visualization

Let’s also add a function to visualize a specific subdirectory with its notebooks:


generate_subdirectory_tree


def generate_subdirectory_tree(
    subdir_path:Path, # Path to subdirectory
    show_descriptions:bool=True, # Include notebook descriptions
    exclude_empty:bool=True, # Exclude empty directories
    exclude_index:bool=True, # Exclude index.ipynb
)->str: # Tree visualization

Generate tree visualization for a specific subdirectory showing all notebooks

Summary Statistics

Let’s add a function to generate summary statistics:


get_tree_summary


def get_tree_summary(
    path:Path=None, # Directory to analyze
)->str: # Summary string

Get summary statistics for notebooks in directory tree

# Test summary
print(get_tree_summary())
Total: 7 notebooks