Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DirectoryTree.path no longer reactive #4208

Closed
juftin opened this issue Feb 26, 2024 · 4 comments · Fixed by #4210
Closed

DirectoryTree.path no longer reactive #4208

juftin opened this issue Feb 26, 2024 · 4 comments · Fixed by #4210
Assignees
Labels
bug Something isn't working Task

Comments

@juftin
Copy link
Contributor

juftin commented Feb 26, 2024

The DirectoryTree.path variable changing values no longer reloads the widget when it's changed. This behavior changed on the 0.49.0 release.

I expected the path attribute to be reactive, according to the watch_path docs:

If the path is changed the directory tree will be repopulated using the new value as the root.

In the below examples I set a binding on the directory tree, . - this action should trigger the directory tree going refreshing to the current root's parent directory:

Example App

import pathlib
from typing import Any, ClassVar, List

from rich.syntax import Syntax
from textual import on
from textual.app import App, ComposeResult
from textual.binding import BindingType
from textual.containers import Horizontal, VerticalScroll
from textual.widgets import DirectoryTree, Footer, Header, Static


class DirectoryTreeApp(App):
    """
    Directory Tree + Code Viewer App
    """

    BINDINGS: ClassVar[List[BindingType]] = [
        ("q", "quit", "Quit"),
        (".", "parent_dir", "Parent Directory"),
    ]

    def __init__(self, path: str, *args: Any, **kwargs: Any):
        super().__init__(*args, **kwargs)
        self.path = pathlib.Path(path).resolve()
        self.file_content = Static()
        self.directory_tree = DirectoryTree(path=self.path)

    def compose(self) -> ComposeResult:
        yield Header()
        yield Horizontal(self.directory_tree, VerticalScroll(self.file_content))
        yield Footer()

    @on(DirectoryTree.FileSelected)
    def handle_file_selected(self, message: DirectoryTree.FileSelected) -> None:
        file_content = message.path.read_text(errors="ignore")
        lexer = Syntax.guess_lexer(path=str(message.path))
        code = Syntax(code=file_content, lexer=lexer)
        self.file_content.update(code)

    def action_parent_dir(self) -> None:
        """
        Go to the parent directory
        """
        if self.directory_tree.path != self.directory_tree.path.parent:
            self.directory_tree.path = self.directory_tree.path.parent
            self.notify(message=f"Directory Changed: {self.directory_tree.path}")


app = DirectoryTreeApp(".")

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

textual==0.48.2 screen recording

Screen.Recording.2024-02-25.at.8.22.04.PM.mov

textual==0.49.0 screen recording

Screen.Recording.2024-02-25.at.8.20.38.PM.mov

@Textualize Textualize deleted a comment from github-actions bot Feb 26, 2024
@davep davep added bug Something isn't working Task labels Feb 26, 2024
@davep
Copy link
Contributor

davep commented Feb 26, 2024

Confirmed with this:

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

class DirTreePathIssueApp(App[None]):

    def compose(self) -> ComposeResult:
        yield Input()
        yield DirectoryTree(".")

    @on(Input.Submitted)
    def new_path(self, event: Input.Submitted) -> None:
        self.notify(f"Swapping to {event.value}")
        self.query_one(DirectoryTree).path = event.value

if __name__ == "__main__":
    DirTreePathIssueApp().run()

Most likely an unintended consequence of #4123.

Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@juftin
Copy link
Contributor Author

juftin commented Feb 26, 2024

Thank you Dave!

@davep
Copy link
Contributor

davep commented Feb 27, 2024

Most welcome! And thanks for the heads-up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants