Skip to content

Instantly share code, notes, and snippets.

@iforvard
Created July 27, 2022 22:27
Show Gist options
  • Save iforvard/43aad622c6aa4948991eb53c902d6b90 to your computer and use it in GitHub Desktop.
Save iforvard/43aad622c6aa4948991eb53c902d6b90 to your computer and use it in GitHub Desktop.
File manager concept on flat
import os
from pathlib import Path
from flet import icons, flet, Page, Row, ElevatedButton, GridView, TextButton, TextField
def setup_page(page: Page):
page.theme_mode = "dark"
def get_dir_elements(path="/"):
for element in os.listdir(path=path):
if os.path.isdir(f"{path}/{element}"):
yield element, icons.FOLDER
else:
yield element, icons.FILE_OPEN
def main(page: Page):
def click_element(event):
path, dir_screen = event.control.data
text_label = fs_address_one if dir_screen.data == 1 else fs_address_two
text_label.value = path
text_label.label = path
change_dir(dir_screen, text_label)
def update_dir_screen(dir_screen, path):
dir_screen.controls.clear()
for element in get_dir_elements(path):
path = "" if path == "/" else path
dir_screen.controls.append(
TextButton(
icon=element[1],
icon_color="blue400",
text=element[0],
data=(f"{path}/{element[0]}", dir_screen),
on_click=click_element,
)
)
page.update()
def button_clicked_one(event):
if event.control.data and event.control.data == "back":
fs_address_one.value = Path(fs_address_one.value).parent.absolute()
change_dir(fs_dir_one, fs_address_one)
def button_clicked_two(event):
if event.control.data and event.control.data == "back":
fs_address_two.value = Path(fs_address_two.value).parent.absolute()
change_dir(fs_dir_two, fs_address_two)
def change_dir(dir_screen, text_label):
if os.path.isdir(text_label.value):
text_label.label = text_label.value
update_dir_screen(dir_screen, text_label.label)
page.update()
setup_page(page)
path_btn_one = ElevatedButton(icon=icons.FIND_IN_PAGE, on_click=button_clicked_one)
path_btn_two = ElevatedButton(icon=icons.FIND_IN_PAGE, on_click=button_clicked_two)
back_btn_one = ElevatedButton(
icon=icons.ARROW_BACK, on_click=button_clicked_one, data="back"
)
back_btn_two = ElevatedButton(
icon=icons.ARROW_BACK, on_click=button_clicked_two, data="back"
)
fs_address_one = TextField(label="", expand=1)
fs_address_two = TextField(label="", expand=1)
fs_dir_one = GridView(
expand=1,
runs_count=5,
max_extent=150,
child_aspect_ratio=1.0,
spacing=5,
run_spacing=5,
# index in page TextField
data=1,
)
fs_dir_two = GridView(
expand=1,
runs_count=5,
max_extent=150,
child_aspect_ratio=1.0,
spacing=5,
run_spacing=5,
# index in page TextField
data=2,
)
page.add(Row([fs_address_one, path_btn_one, back_btn_one]))
page.add(fs_dir_one)
page.add(Row([fs_address_two, path_btn_two, back_btn_two]))
page.add(fs_dir_two)
update_dir_screen(fs_dir_one, "/")
update_dir_screen(fs_dir_two, "/")
page.update()
if __name__ == "__main__":
flet.app(target=main, port=9000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment