File size: 2,266 Bytes
87fe461
 
6684d9c
a467828
87fe461
6684d9c
87fe461
6e61d26
 
 
87fe461
 
bcb8af1
6e61d26
 
 
87fe461
 
 
 
6e61d26
87fe461
6e61d26
 
 
 
 
bcb8af1
 
6e61d26
0c196bb
a467828
9826c29
 
 
0c196bb
 
 
 
 
 
 
 
 
a467828
 
52a0611
6e61d26
 
 
 
 
 
87fe461
 
 
bcb8af1
 
6e61d26
ab818d6
 
 
 
 
 
bcb8af1
 
 
87fe461
 
6e61d26
 
 
52a0611
6e61d26
a467828
6e61d26
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
import streamlit as st

# TODO: rename and refactor everything


def select_int_interval(param_name: str, limits_list: list, defaults, **kwargs):
    st.sidebar.subheader(param_name)
    min_max_interval = st.sidebar.slider(
        "", limits_list[0], limits_list[1], defaults, key=hash(param_name)
    )
    return min_max_interval


def select_several_ints(
    param_name, subparam_names, limits_list, defaults_list, **kwargs
):
    st.sidebar.subheader(param_name)
    result = []
    assert len(limits_list) == len(defaults_list)
    assert len(subparam_names) == len(defaults_list)

    for name, limits, defaults in zip(subparam_names, limits_list, defaults_list):
        result.append(
            st.sidebar.slider(
                name, limits[0], limits[1], defaults, key=hash(param_name + name)
            )
        )
    return tuple(result)


def select_min_max(param_name, limits_list, defaults_list, min_diff=0, **kwargs):
    assert len(param_name) == 2
    result = list(
        select_int_interval(" & ".join(param_name), limits_list, defaults_list)
    )
    if result[1] - result[0] < min_diff:
        diff = min_diff - result[1] + result[0]
        if result[1] + diff <= limits_list[1]:
            result[1] = result[1] + diff
        elif result[0] - diff >= limits_list[0]:
            result[0] = result[0] - diff
        else:
            result = limits_list
    return tuple(result)


def select_RGB(param_name, **kwargs):
    result = select_several_ints(
        param_name,
        subparam_names=["Red", "Green", "Blue"],
        limits_list=[[0, 255], [0, 255], [0, 255]],
        defaults_list=[0, 0, 0],
    )
    return tuple(result)


def select_radio(param_name, options_list, **kwargs):
    st.sidebar.subheader(param_name)
    result = st.sidebar.radio("", options_list)
    return result


def select_checkbox(param_name, defaults, **kwargs):
    st.sidebar.subheader(param_name)
    result = st.sidebar.checkbox("True", defaults)
    return result


# dict from param name to function showing this param
param2func = {
    "int_interval": select_int_interval,
    "several_ints": select_several_ints,
    "radio": select_radio,
    "rgb": select_RGB,
    "checkbox": select_checkbox,
    "min_max": select_min_max,
}