File size: 1,678 Bytes
cb5b71d
 
 
 
 
 
 
 
dc92053
cb5b71d
 
 
 
 
 
dc92053
 
 
cb5b71d
 
 
 
 
 
 
 
dc92053
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb5b71d
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
import datetime

from etils import epath
import streamlit as st

from core.constants import PROJECT_FOLDER_PATTERN
from core.past_projects import load_past_projects_paths
from core.past_projects import open_project
from core.query_params import set_project
from core.state import CurrentProject
from core.state import Metadata


def _load_croissant(metadata: Metadata, path: epath.Path) -> None:
    st.session_state[Metadata] = metadata
    project = CurrentProject(path)
    st.session_state[CurrentProject] = project
    set_project(project)


def _remove_croissant(path: epath.Path) -> None:
    path.rmtree(missing_ok=True)


def render_previous_files():
    paths = load_past_projects_paths()
    has_no_project = True
    for index, path in enumerate(paths):
        try:
            metadata = open_project(path)
            timestamp = datetime.datetime.strptime(
                path.name, PROJECT_FOLDER_PATTERN
            ).strftime("%Y/%m/%d %H:%M")
            label = f"{metadata.name or 'Unnamed dataset'} - {timestamp}"
            col1, col2 = st.columns([10, 1])
            col1.button(
                label,
                key=f"splash-{index}-load",
                on_click=_load_croissant,
                args=(metadata, path),
            )
            col2.button(
                "✖️",
                help="Warning: this will delete the project.",
                key=f"splash-{index}-remove",
                on_click=_remove_croissant,
                args=(path,),
            )
            has_no_project = False
        except:
            pass
    if has_no_project:
        st.write("No past project to load. Create one on the left!")