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

File Browser #39

Open
Isaac-Flath opened this issue Sep 9, 2024 · 1 comment
Open

File Browser #39

Isaac-Flath opened this issue Sep 9, 2024 · 1 comment

Comments

@Isaac-Flath
Copy link
Collaborator

FYI @davidbrochart - Feel free to do a PR, otherwise I will add it when I get time if you're ok with it

Add File browser to gallery as widget.

from pathlib import Path

from fasthtml.common import *


FILE = "📄"
FOLDER = "📁"
SPACE = "\u2800"

app, rt = fast_app()

@rt("/{path:path}")
def get(path: str):
    p1 = Path(path)
    content1 = {p2.name: p2.is_dir() for p2 in p1.iterdir()}
    directories = [name for name, is_dir in content1.items() if is_dir]
    files = [name for name, is_dir in content1.items() if not is_dir]
    directories.sort()
    files.sort()
    content2 = []
    if p1.parts:
        content2.append(P(SPACE * (len(p1.parts) - 1) + "⌄" + FOLDER + p1.name))
    spaces = SPACE * len(p1.parts)
    for name in directories:
        content2.append(P(spaces + ">" + FOLDER + name, hx_get=f"{path}/{name}", hx_swap="outerHTML"))
    for name in files:
        content2.append(P(spaces + SPACE + FILE + name))
    return Div(*content2)

serve()
@davidbrochart
Copy link

otherwise I will add it when I get time if you're ok with it

Sure, thanks!

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