Spaces:
Sleeping
Sleeping
rmm
commited on
Commit
·
3eaf0a5
1
Parent(s):
fd18838
fix: checking input requires testing for empty strings/lists
Browse files- testing for the presence of the key in session_state is no longer
sufficient with current implementation (we explicitly set the keys
so we can handle >1 file)
- src/input/input_handling.py +23 -48
- src/main.py +1 -1
src/input/input_handling.py
CHANGED
@@ -34,10 +34,6 @@ spoof_metadata = {
|
|
34 |
}
|
35 |
|
36 |
def check_inputs_are_set(empty_ok:bool=False, debug:bool=False) -> bool:
|
37 |
-
return check_inputs_are_set_by_hash(empty_ok=empty_ok, debug=debug)
|
38 |
-
|
39 |
-
|
40 |
-
def check_inputs_are_set_by_hash(empty_ok:bool=False, debug:bool=False) -> bool:
|
41 |
"""
|
42 |
Checks if all expected inputs have been entered
|
43 |
|
@@ -53,16 +49,36 @@ def check_inputs_are_set_by_hash(empty_ok:bool=False, debug:bool=False) -> bool:
|
|
53 |
if len(image_hashes) == 0:
|
54 |
return empty_ok
|
55 |
|
|
|
|
|
56 |
|
57 |
-
exp_input_key_stubs = ["input_latitude", "input_longitude"]
|
58 |
-
#exp_input_key_stubs = ["input_latitude", "input_longitude", "input_author_email", "input_date", "input_time", "input_image_selector"]
|
59 |
vals = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
for image_hash in image_hashes:
|
61 |
for stub in exp_input_key_stubs:
|
62 |
key = f"{stub}_{image_hash}"
|
63 |
val = None
|
64 |
if key in st.session_state:
|
65 |
val = st.session_state[key]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
vals.append(val)
|
67 |
if debug:
|
68 |
msg = f"{key:15}, {(val is not None):8}, {val}"
|
@@ -74,42 +90,6 @@ def check_inputs_are_set_by_hash(empty_ok:bool=False, debug:bool=False) -> bool:
|
|
74 |
return all([v is not None for v in vals])
|
75 |
|
76 |
|
77 |
-
def check_inputs_are_set_by_fname(empty_ok:bool=False, debug:bool=False) -> bool:
|
78 |
-
"""
|
79 |
-
Checks if all expected inputs have been entered
|
80 |
-
|
81 |
-
Implementation: via the Streamlit session state.
|
82 |
-
|
83 |
-
Args:
|
84 |
-
empty_ok (bool): If True, returns True if no inputs are set. Default is False.
|
85 |
-
debug (bool): If True, prints and logs the status of each expected input key. Default is False.
|
86 |
-
Returns:
|
87 |
-
bool: True if all expected input keys are set, False otherwise.
|
88 |
-
"""
|
89 |
-
filenames = st.session_state.image_filenames
|
90 |
-
if len(filenames) == 0:
|
91 |
-
return empty_ok
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
exp_input_key_stubs = ["input_latitude", "input_longitude"]
|
96 |
-
#exp_input_key_stubs = ["input_latitude", "input_longitude", "input_author_email", "input_date", "input_time", "input_image_selector"]
|
97 |
-
vals = []
|
98 |
-
for image_filename in filenames:
|
99 |
-
for stub in exp_input_key_stubs:
|
100 |
-
key = f"{stub}_{image_filename}"
|
101 |
-
val = None
|
102 |
-
if key in st.session_state:
|
103 |
-
val = st.session_state[key]
|
104 |
-
vals.append(val)
|
105 |
-
if debug:
|
106 |
-
msg = f"{key:15}, {(val is not None):8}, {val}"
|
107 |
-
m_logger.debug(msg)
|
108 |
-
print(msg)
|
109 |
-
|
110 |
-
return all([v is not None for v in vals])
|
111 |
-
|
112 |
-
|
113 |
def process_one_file(file:UploadedFile) -> Tuple[np.ndarray, str, str, InputObservation]:
|
114 |
# do all the non-UI calcs
|
115 |
# add the UI elements
|
@@ -138,7 +118,7 @@ def process_one_file(file:UploadedFile) -> Tuple[np.ndarray, str, str, InputObse
|
|
138 |
# 3. Latitude Entry Box
|
139 |
latitude = viewcontainer.text_input(
|
140 |
"Latitude for " + filename,
|
141 |
-
spoof_metadata.get('latitude', ""),
|
142 |
key=f"input_latitude_{ukey}")
|
143 |
if latitude and not is_valid_number(latitude):
|
144 |
viewcontainer.error("Please enter a valid latitude (numerical only).")
|
@@ -260,11 +240,6 @@ def setup_input(
|
|
260 |
_setup_oneoff_inputs()
|
261 |
# amazingly we just have to add the uploader and its callback, and the rest is dynamic.
|
262 |
|
263 |
-
# # check if the inputs are set
|
264 |
-
# if check_inputs_are_set(empty_ok=True):
|
265 |
-
# st.sidebar.success("All inputs are set.")
|
266 |
-
# else:
|
267 |
-
# st.sidebar.warning("Please fill in all the required inputs.")
|
268 |
|
269 |
def setup_input_monolithic(
|
270 |
viewcontainer: DeltaGenerator=None,
|
|
|
34 |
}
|
35 |
|
36 |
def check_inputs_are_set(empty_ok:bool=False, debug:bool=False) -> bool:
|
|
|
|
|
|
|
|
|
37 |
"""
|
38 |
Checks if all expected inputs have been entered
|
39 |
|
|
|
49 |
if len(image_hashes) == 0:
|
50 |
return empty_ok
|
51 |
|
52 |
+
exp_input_key_stubs = ["input_latitude", "input_longitude", "input_date", "input_time"]
|
53 |
+
#exp_input_key_stubs = ["input_latitude", "input_longitude", "input_author_email", "input_date", "input_time",
|
54 |
|
|
|
|
|
55 |
vals = []
|
56 |
+
# the author_email is global/one-off - no hash extension.
|
57 |
+
if "input_author_email" in st.session_state:
|
58 |
+
val = st.session_state["input_author_email"]
|
59 |
+
vals.append(val)
|
60 |
+
if debug:
|
61 |
+
msg = f"{'input_author_email':15}, {(val is not None):8}, {val}"
|
62 |
+
m_logger.debug(msg)
|
63 |
+
print(msg)
|
64 |
+
|
65 |
+
|
66 |
for image_hash in image_hashes:
|
67 |
for stub in exp_input_key_stubs:
|
68 |
key = f"{stub}_{image_hash}"
|
69 |
val = None
|
70 |
if key in st.session_state:
|
71 |
val = st.session_state[key]
|
72 |
+
|
73 |
+
# handle cases where it is defined but empty
|
74 |
+
# if val is a string and empty, set to None
|
75 |
+
if isinstance(val, str) and not val:
|
76 |
+
val = None
|
77 |
+
# if val is a list and empty, set to None (not sure what UI elements would return a list?)
|
78 |
+
if isinstance(val, list) and not val:
|
79 |
+
val = None
|
80 |
+
# number 0 is ok - possibly. could be on the equator, e.g.
|
81 |
+
|
82 |
vals.append(val)
|
83 |
if debug:
|
84 |
msg = f"{key:15}, {(val is not None):8}, {val}"
|
|
|
90 |
return all([v is not None for v in vals])
|
91 |
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
def process_one_file(file:UploadedFile) -> Tuple[np.ndarray, str, str, InputObservation]:
|
94 |
# do all the non-UI calcs
|
95 |
# add the UI elements
|
|
|
118 |
# 3. Latitude Entry Box
|
119 |
latitude = viewcontainer.text_input(
|
120 |
"Latitude for " + filename,
|
121 |
+
#spoof_metadata.get('latitude', ""),
|
122 |
key=f"input_latitude_{ukey}")
|
123 |
if latitude and not is_valid_number(latitude):
|
124 |
viewcontainer.error("Please enter a valid latitude (numerical only).")
|
|
|
240 |
_setup_oneoff_inputs()
|
241 |
# amazingly we just have to add the uploader and its callback, and the rest is dynamic.
|
242 |
|
|
|
|
|
|
|
|
|
|
|
243 |
|
244 |
def setup_input_monolithic(
|
245 |
viewcontainer: DeltaGenerator=None,
|
src/main.py
CHANGED
@@ -227,7 +227,7 @@ def main() -> None:
|
|
227 |
|
228 |
if st.session_state.workflow_fsm.is_in_state('doing_data_entry'):
|
229 |
# can we advance state? - only when all inputs are set for all uploaded files
|
230 |
-
all_inputs_set = check_inputs_are_set(debug=True)
|
231 |
if all_inputs_set:
|
232 |
st.session_state.workflow_fsm.complete_current_state()
|
233 |
# -> data_entry_complete
|
|
|
227 |
|
228 |
if st.session_state.workflow_fsm.is_in_state('doing_data_entry'):
|
229 |
# can we advance state? - only when all inputs are set for all uploaded files
|
230 |
+
all_inputs_set = check_inputs_are_set(debug=True, empty_ok=False)
|
231 |
if all_inputs_set:
|
232 |
st.session_state.workflow_fsm.complete_current_state()
|
233 |
# -> data_entry_complete
|