File size: 5,601 Bytes
a62bbf7
161d75f
86d2f2e
bbe71e4
 
3011029
b493bab
 
 
 
bbe71e4
 
 
 
 
a62bbf7
baec762
161d75f
3011029
59d7f29
bbe71e4
baec762
bbe71e4
baec762
b493bab
86d2f2e
baec762
a62bbf7
baec762
b493bab
86d2f2e
baec762
a62bbf7
baec762
b493bab
86d2f2e
baec762
a62bbf7
baec762
b493bab
86d2f2e
baec762
bbe71e4
 
 
 
 
3ae828c
bbe71e4
 
 
 
161d75f
bbe71e4
 
 
 
3ae828c
 
 
bbe71e4
 
 
 
 
 
 
 
 
3ae828c
ce3810b
 
baec762
3ae828c
86d2f2e
a725af0
3ae828c
 
 
 
baec762
bbe71e4
 
 
 
 
 
 
 
 
 
 
 
 
 
baec762
ce3810b
baec762
86d2f2e
3ae828c
baec762
 
ce3810b
baec762
86d2f2e
3ae828c
baec762
 
18350b3
baec762
18350b3
86d2f2e
18350b3
baec762
 
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
import gradio as gr
from utils.utils_config import get_custom_config_dropdowns
from validation_submission.utils_individual import add_data_to_individual

#--------------------------------------------------------- LEVEL 1 DROPDOWNS
def retrieve_config_options(label, dropdown_config):
    options = list(dropdown_config[label].keys())
    options = [option.title() for option in options]
    return options

def reinitialise_level2(): 
    dropdown_level2 = gr.Dropdown(choices=[], visible=False)
    openfield_level2 = gr.Textbox(visible=False)
    dropdown_extra_level2 = gr.Dropdown(choices=[], visible=False)
    return dropdown_level2, openfield_level2, dropdown_extra_level2

def create_dropdown_level1(label, individual): 
    dropdown_config = get_custom_config_dropdowns("config_dropdown_circumstances.json")
    options = retrieve_config_options(label, dropdown_config)
    dropdown = gr.Dropdown(choices=options, label=label, interactive=True, visible=True)
    dropdown_level2, openfield_level2, dropdown_extra_level2 = reinitialise_level2()
    return dropdown, dropdown_level2, openfield_level2, dropdown_extra_level2, individual
    
def dropdown_collision(individual):
    label = "Collision with a means of transport"
    individual = add_data_to_individual("wounded_dead", "circumstance", label.lower(), individual)
    return create_dropdown_level1(label, individual)

def dropdown_deliberate_destruction(individual):
    label = "Destruction / Deliberatly removed"
    individual = add_data_to_individual("wounded_dead", "circumstance", label.lower(), individual)
    return create_dropdown_level1(label, individual)     

def dropdown_indirect_destruction(individual): 
    label = "Indirect destruction"
    individual = add_data_to_individual("wounded_dead", "circumstance", label.lower(), individual)
    return create_dropdown_level1(label, individual) 

def dropdown_natural_cause(individual): 
    label = "Natural cause"
    individual = add_data_to_individual("wounded_dead", "circumstance", label.lower(), individual)
    return create_dropdown_level1(label, individual)         


#--------------------------------------------------------- LEVEL 2 DROPDOWNS
def get_options(value):
        value = value.lower()
        options_label = None
        options_dropdown= None
        open_field = None
        extras = None
        extras_label = None
        dropdown_config = get_custom_config_dropdowns("config_dropdown_circumstances.json")
        for _, sub_dict in dropdown_config.items():
            nested_dict = sub_dict.get(value)
            if nested_dict is not None: 
                if "Options" in nested_dict.keys():
                    options_dict = nested_dict["Options"]
                    options_label = list(options_dict.keys())[0]
                    options_dropdown = list(options_dict.values())[0]
                    options_dropdown = [option.title() for option in options_dropdown]
                if "Open" in nested_dict.keys():
                    open_field = nested_dict["Open"]
                    open_field = open_field.title()
                if "Extra" in nested_dict.keys():
                    for key, val in nested_dict["Extra"].items():
                        extras_label = key
                        extras = val
                        extras = [extra.title() for extra in extras]
        return options_label, options_dropdown, open_field, extras, extras_label

       
def on_select(evt: gr.SelectData, individual):  # SelectData is a subclass of EventData
    options_label, options_dropdown, open_field, extras, extras_label = get_options(evt.value)
    individual = add_data_to_individual("wounded_dead", 
                    "circumstance_type", 
                    {"type": (evt.value).lower(),
                     "option_dropdown_label" : options_label.lower() if options_label is not None else 'NA',
                     "open_field_label" : open_field.lower() if open_field is not None else 'NA',
                     "extra_label": extras_label.lower() if extras_label is not None else 'NA'
                    }, individual)
    if options_dropdown is not None:
        dropdown_level2 = gr.Dropdown(choices=options_dropdown, label=evt.value, interactive=True, visible=True)
    else: 
        dropdown_level2 = gr.Dropdown(choices=[], visible=False)

    if open_field is not None:
        openfield_level2 = gr.Textbox(label=open_field, interactive=True, visible=True)
    else: 
        openfield_level2 = gr.Textbox(visible=False)

    if extras is not None: 
        dropdown_extra_level2 = gr.Dropdown(choices=extras, label=extras_label, interactive=True, visible=True)
    else: 
        dropdown_extra_level2 = gr.Dropdown(choices=[], visible=False)
    return dropdown_level2, openfield_level2, dropdown_extra_level2, individual

def on_select_dropdown_level2(evt: gr.SelectData, individual): 
    individual = add_data_to_individual("wounded_dead", 
                "circumstance_option_dropdown", 
                evt.value.lower(), individual)
    return individual

def on_select_dropdown_extra_level2(evt: gr.SelectData, individual):  
    individual = add_data_to_individual("wounded_dead", 
                 "circumstance_extra", 
                 evt.value.lower(), individual)
    return individual
    
def on_change_openfield_level2(openfield_level2_dead, individual): 
    print("Saving open field")
    individual = add_data_to_individual("wounded_dead", 
                "circumstance_open_field", 
                str(openfield_level2_dead).lower(), individual)
    return individual