Skip to content

File Browser #39

Closed
Closed
@Isaac-Flath

Description

@Isaac-Flath

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions