File: 21_file_browser.py

package info (click to toggle)
python-trame 3.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 101,620 kB
  • sloc: python: 13,515; sh: 183; javascript: 93; makefile: 7
file content (69 lines) | stat: -rw-r--r-- 1,863 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from tkinter import Tk, filedialog

from trame.app import get_server
from trame.ui.vuetify import SinglePageLayout
from trame.widgets import vuetify

# -----------------------------------------------------------------------------
# Trame setup
# -----------------------------------------------------------------------------

server = get_server(client_type="vue2")
state, ctrl = server.state, server.controller

# Keep track of the currently selected directory
state.selected_dir = "None"

root = Tk()

# Ensure the tkinter main window is hidden
root.withdraw()

# Ensure that the file browser will appear in front on Windows
root.wm_attributes("-topmost", 1)


def open_directory():
    kwargs = {
        "title": "Select Directory",
    }
    dirpath = filedialog.askdirectory(**kwargs)
    if not dirpath:
        # User canceled.
        print("Canceled")
        return

    print("Selected directory:", dirpath)
    state.selected_dir = dirpath


# The controller would let us set up callback logic in a separate file
ctrl.open_directory = open_directory

# -----------------------------------------------------------------------------
# UI setup
# -----------------------------------------------------------------------------

layout = SinglePageLayout(server)

with layout:
    # Toolbar
    with layout.toolbar as toolbar:
        toolbar.clear()

        vuetify.VSpacer()

        vuetify.VBtn("Select Directory", click=ctrl.open_directory)

        vuetify.VSpacer()

        with layout.content:
            with vuetify.VContainer() as container:
                container.add_child("Selected Directory: {{ selected_dir }}")

# -----------------------------------------------------------------------------
# start server
# -----------------------------------------------------------------------------

if __name__ == "__main__":
    server.start()