File size: 3,406 Bytes
5ef7afe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""The Files page for the Trackio UI."""

import os
from pathlib import Path

import gradio as gr

try:
    import trackio.utils as utils
    from trackio.ui import fns
except ImportError:
    import utils
    from ui import fns


def get_files_path(project: str | None) -> str | None:
    """Get the files directory path for a project. If the directory does not exist, returns None."""
    if not project:
        return None
    files_dir = utils.MEDIA_DIR / project / "files"
    if not files_dir.exists():
        return None
    return str(files_dir)


def update_file_explorer(project: str | None):
    """Update the file explorer based on the selected project."""
    files_path = get_files_path(project)
    if files_path:
        return gr.FileExplorer(root_dir=files_path, visible=True)
    else:
        return gr.FileExplorer(visible=False)


def extract_files(project: str, files_or_diectories: list[str | Path]):
    """Extract files from a list of files or directories."""
    files = []
    root_dir = Path(get_files_path(project))
    for file_or_directory in files_or_diectories:
        if os.path.isfile(file_or_directory):
            files.append(str(root_dir / file_or_directory))
    return files


with gr.Blocks() as files_page:
    with gr.Sidebar() as sidebar:
        logo_urls = utils.get_logo_urls()
        logo = gr.Markdown(
            f"""
                <img src='{logo_urls["light"]}' width='80%' class='logo-light'>
                <img src='{logo_urls["dark"]}' width='80%' class='logo-dark'>            
            """
        )
        project_dd = gr.Dropdown(label="Project", allow_custom_value=True)

    navbar = gr.Navbar(
        value=[
            ("Metrics", ""),
            ("Media & Tables", "/media"),
            ("Runs", "/runs"),
            ("Files", "/files"),
        ],
        main_page_name=False,
    )
    timer = gr.Timer(value=1)

    gr.Markdown("## Files")
    file_explorer = gr.FileExplorer(label="Uploaded Files", visible=False)
    file_downloader = gr.Files(label="Download Selected Files")

    gr.on(
        [timer.tick],
        fn=lambda: gr.Dropdown(info=fns.get_project_info()),
        outputs=[project_dd],
        show_progress="hidden",
        api_visibility="private",
    )

    gr.on(
        [files_page.load],
        fn=fns.get_projects,
        outputs=project_dd,
        show_progress="hidden",
        queue=False,
        api_visibility="private",
    ).then(
        fn=update_file_explorer,
        inputs=[project_dd],
        outputs=[file_explorer],
        show_progress="hidden",
        api_visibility="private",
        queue=False,
    ).then(
        fns.update_navbar_value,
        inputs=[project_dd],
        outputs=[navbar],
        show_progress="hidden",
        api_visibility="private",
        queue=False,
    )

    gr.on(
        [project_dd.change],
        fn=update_file_explorer,
        inputs=[project_dd],
        outputs=[file_explorer],
        show_progress="hidden",
        api_visibility="private",
        queue=False,
    ).then(
        fns.update_navbar_value,
        inputs=[project_dd],
        outputs=[navbar],
        show_progress="hidden",
        api_visibility="private",
        queue=False,
    )

    gr.on(
        [file_explorer.change],
        fn=extract_files,
        inputs=[project_dd, file_explorer],
        outputs=[file_downloader],
    )