Skip to content

DirectoryTree

A tree control to navigate the contents of your filesystem.

  • Focusable
  • Container

Example

The example below creates a simple tree to navigate the current working directory.

from textual.app import App, ComposeResult
from textual.widgets import DirectoryTree


class DirectoryTreeApp(App):
    def compose(self) -> ComposeResult:
        yield DirectoryTree("./")


if __name__ == "__main__":
    app = DirectoryTreeApp()
    app.run()

Messages

FileSelected

Bases: Message

Posted when a file is selected.

Can be handled using on_directory_tree_file_selected in a subclass of DirectoryTree or in a parent widget in the DOM.

Attributes:

Name Type Description
path str

The path of the file that was selected.

Reactive Attributes

Name Type Default Description
show_root bool True Show the root node.
show_guides bool True Show guide lines between levels.
guide_depth int 4 Amount of indentation between parent and child.

Component Classes

The directory tree widget provides the following component classes:

Class Description
directory-tree--extension Target the extension of a file name.
directory-tree--file Target files in the directory structure.
directory-tree--folder Target folders in the directory structure.
directory-tree--hidden Target hidden items in the directory structure.

See also the component classes for Tree.

See Also