Skip to content

fix: FileBrowser bug when navigating to path root#707

Merged
maartenbreddels merged 2 commits intowidgetti:masterfrom
Jhsmit:issue_filebrowser
Jul 10, 2024
Merged

fix: FileBrowser bug when navigating to path root#707
maartenbreddels merged 2 commits intowidgetti:masterfrom
Jhsmit:issue_filebrowser

Conversation

@Jhsmit
Copy link
Copy Markdown
Contributor

@Jhsmit Jhsmit commented Jul 8, 2024

Fixes #688

The PR changes the type of paths used internally in FileBrowser from str to pathlib.Path such that the correct parent is always returned.

Also fixed optional kwargs typing

@Jhsmit
Copy link
Copy Markdown
Contributor Author

Jhsmit commented Jul 8, 2024

I have a use case where I want to run an app on a server and show files/folders on the server in a given directory.

However, the user should not be able to navigate to parent directories above the starting directory.

I've currently implemented it as:

@solara.component
def Page():
    my_dir = Path.cwd()

    def browser_filter(path: Path) -> bool:
        if path.is_dir():
            return path not in my_dir.parents
        return True

    MyFileBrowser(directory=my_dir, directory_first=True, filter=browser_filter)

Which then requires an additional check on new_dir in FileBrowser

            new_dir = current_dir.value.parent
            if not filter(new_dir):
                return

I can add this to this PR or make another one if you think it would be more generally useful.

@maartenbreddels
Copy link
Copy Markdown
Contributor

beautifull fix!
Regarding your last comment, does this solve your problem:
#149
(why is there no pycafe example? :))

@Jhsmit
Copy link
Copy Markdown
Contributor Author

Jhsmit commented Jul 10, 2024

here is the link to pycafe

the fix in the issue you mention does indeed work.

However, given that the function of filter is to filter out files and/or folders that should not be shown, wouldn't it make sense to pass the new parent folder through filter first before navigating there?
ie you could consider it to be a bug that filter is not respected when navigating up

@maartenbreddels
Copy link
Copy Markdown
Contributor

I don't think I fully understand the problem, can you show an example that does not work?

@Jhsmit
Copy link
Copy Markdown
Contributor Author

Jhsmit commented Jul 10, 2024

import solara
from pathlib import Path

BASE_PATH = Path.cwd()

@solara.component
def Page():
    directory = solara.use_reactive(BASE_PATH)
    def protect_filter(path: Path) -> bool:
        if path.is_dir():
            return path not in BASE_PATH.parents
        return True

    solara.FileBrowser(directory, filter=protect_filter)

pycafe

In this example, based on how you interpret the docstring of filter, you might expect this filter to make it impossible to navigate up above BASE_PATH. But you can access it if you click '..', you just cannot go back. This would be 'fixed' when adding the early return when filter returns False (if not filter(new_dir):)

@maartenbreddels maartenbreddels merged commit 64ffb28 into widgetti:master Jul 10, 2024
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

Successfully merging this pull request may close these issues.

FileBrowser bug when navigating to path root

2 participants