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

How to refresh DirectoryTree to see new files/folders? #4777

Closed
mayermakes opened this issue Jul 18, 2024 Discussed in #1428 · 10 comments
Closed

How to refresh DirectoryTree to see new files/folders? #4777

mayermakes opened this issue Jul 18, 2024 Discussed in #1428 · 10 comments

Comments

@mayermakes
Copy link

Pushing this discussion to issue as it seems critical for a lot of apps based on textual.
A method to reload/refresh the DirectoryTree Widget would be highly appreciated.
self.query(Input).refresh(layout=False, recompose=True) --> or any Widget I tried works.

self.query(DirectoryTree).refresh(layout=False, recompose=True) -> does not, but would be expected.

Discussed in #1428

Originally posted by toiletsandpaper December 22, 2022
This does't work at all:

def on_button_pressed(self, event: Button.Pressed):
    if event.button.id == 'button-refresh-tree':
        logger.info('Refresh Tree button pressed')
        tree: DirectoryTree = self.query_one('#tree-view', DirectoryTree)
        tree.refresh()

And this is working, but seems very inefficient:

def on_button_pressed(self, event: Button.Pressed):
    if event.button.id == 'button-refresh-tree':
        logger.info('Refresh Tree button pressed')
        app.pop_screen()
        app.uninstall_screen('file-picker')
        app.install_screen(FilePickerScreen(), name='file-picker')
        app.push_screen('file-picker')
```</div>
Copy link

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@TomJGooding
Copy link
Contributor

DirectoryTree.reload was added back in Textual v0.23.0 and later also DirectoryTree.reload_nodein v0.33.0.

@mayermakes
Copy link
Author

thanks, I missed that in the docs /tutorials

@TomJGooding
Copy link
Contributor

No problem. Would you mind closing this if that resolves your issue?

@mayermakes
Copy link
Author

Closing the issue but want to remark that reload only works( at least for me) with
query_one
like this:
self.query_one(DirectoryTree).reload()
while other widgets can be refreshed all at once and this one has to betrageted once there are more than one .
like this:
self.query_one('#loading-tree').reload()

and the docs did not indicate that to me.

Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@TomJGooding
Copy link
Contributor

Sorry I'm not sure I understand your last remark.

self.query_one('#loading-tree').reload() would a) only target a single widget and b) reload is only a DirectoryTree method?

@mayermakes
Copy link
Author

self.query(DirectoryTree).reload() does not work.
-> AttributeError: 'DOMQuery' object has no attribute 'reload'
self.query_one(DirectoryTree).reload() does work.

Now once there is a second DirectoryTree active.
--> TooManyMatches: Call to only_one resulted in more than one matched node

but if we target one specific DirectoryTree instance --> the one with id "loading-tree":
self.query_one('#loading-tree').reload()

To reload all all Directory trees in the app we would now need to do a query_one on each of the DirectoryTree Widgets individually.
In contrast we can refresh other Inputs with self.query(Input),refresh(layout=False, recompose=True).
-> So I would have expected that refreshing would work with all widgets the same way.

@TomJGooding
Copy link
Contributor

In contrast we can refresh other Inputs with self.query(Input),refresh(layout=False, recompose=True).

I don't think refresh does what you think it does. You shouldn't even have any need to call refresh explictly, see the docs: https://textual.textualize.io/api/widget/#textual.widget.Widget.refresh

@mayermakes
Copy link
Author

mayermakes commented Jul 18, 2024

For my particular application reactivity did not work, but refreshing all the widgets does.
the app is basically just reading in a file displaying its content ins many editable widgets and can save it back to a file/load in new files or convert to other formats.
all contents are based on a yaml file thats read in on launch , which can be exchanged during operation by loading in a new yaml. Reactivity did not work for me, but refreshing after loading in the yaml does.
I was just expecting DirectoryTree to be refreshable like the other widgets are. but the method turned out to be different.
I should mention that the data is all stored in one dictionary generated from a yaml loaded in on launch that needs do be accessible to all functions of the App.
So that might be why reactivity did not work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants