Directory Tree Visualization

Generate tree visualizations for nbdev project structure

Basic Tree Generation

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


source

generate_tree_lines

 generate_tree_lines (path:pathlib.Path, prefix:str='', is_last:bool=True,
                      show_notebooks_only:bool=False,
                      max_depth:Optional[int]=None, current_depth:int=0,
                      exclude_index:bool=True, exclude_empty:bool=True)

Generate tree visualization lines for a directory

Type Default Details
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 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
Returns List Lines of tree output

source

generate_tree

 generate_tree (path:pathlib.Path=None, show_notebooks_only:bool=False,
                max_depth:Optional[int]=None, exclude_index:bool=True,
                exclude_empty:bool=True)

Generate a tree visualization for a directory

Type Default Details
path Path None Directory to visualize (defaults to nbs_path)
show_notebooks_only bool False Only show notebooks, not directories
max_depth Optional None Maximum depth to traverse
exclude_index bool True Exclude index.ipynb from tree
exclude_empty bool True Exclude empty directories
Returns str Tree visualization as string

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:


source

extract_notebook_info

 extract_notebook_info (path:pathlib.Path)

Extract title and description from a notebook

Type Details
path Path Path to notebook file
Returns NotebookInfo Notebook information
# 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:


source

generate_tree_with_descriptions

 generate_tree_with_descriptions (path:pathlib.Path=None,
                                  show_counts:bool=True,
                                  max_depth:Optional[int]=None,
                                  exclude_index:bool=True,
                                  exclude_empty:bool=True)

Generate tree visualization with descriptions from notebooks

Type Default Details
path Path None Directory to visualize
show_counts bool True Show notebook counts for directories
max_depth Optional None Maximum depth to traverse
exclude_index bool True Exclude index.ipynb from tree
exclude_empty bool True Exclude empty directories
Returns str Tree with descriptions

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:


source

generate_subdirectory_tree

 generate_subdirectory_tree (subdir_path:pathlib.Path,
                             show_descriptions:bool=True,
                             exclude_empty:bool=True,
                             exclude_index:bool=True)

Generate tree visualization for a specific subdirectory showing all notebooks

Type Default Details
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
Returns str Tree visualization

Summary Statistics

Let’s add a function to generate summary statistics:


source

get_tree_summary

 get_tree_summary (path:pathlib.Path=None)

Get summary statistics for notebooks in directory tree

Type Default Details
path Path None Directory to analyze
Returns str Summary string
# Test summary
print(get_tree_summary())
Total: 7 notebooks