emvecchi commited on
Commit
8a76709
1 Parent(s): 6c882b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -42
app.py CHANGED
@@ -311,38 +311,41 @@ def navigate(index_change):
311
 
312
  # Function to navigate to specific rows
313
  def navigate_to_row(index):
314
- st.session_state.current_index = index - 1
315
- # only works consistently if done before rerun
316
- js = '''
317
- <script>
318
- var body = window.parent.document.querySelector(".main");
319
-
320
- body.scrollTop = 0;
321
- window.scrollY = 0;
322
- </script>
323
- '''
324
- st.components.v1.html(js, height=0)
325
- # https://discuss.streamlit.io/t/click-twice-on-button-for-changing-state/45633/2
326
-
327
- # disable text input enter to submit
328
- # https://discuss.streamlit.io/t/text-input-how-to-disable-press-enter-to-apply/14457/6
329
- components.html(
330
- """
331
  <script>
332
- const inputs = window.parent.document.querySelectorAll('input');
333
- inputs.forEach(input => {
334
- input.addEventListener('keydown', function(event) {
335
- if (event.key === 'Enter') {
336
- event.preventDefault();
337
- }
338
- });
339
- });
340
  </script>
341
- """,
342
- height=0
343
- )
344
-
345
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
 
347
 
348
  def show_field(f: Field, index: int, data_collected):
@@ -569,14 +572,6 @@ elif st.session_state.current_index == -2:
569
  st.session_state.user_id = st.text_input('User ID', value=user_id_from_url)
570
  add_validated_submit([st.session_state.user_id], "Please enter a valid user ID")
571
 
572
- #elif st.session_state.current_index == -2:
573
- # if st.session_state.get('user_id'):
574
- # st.session_state.user_id = get_param_from_url("user_id")
575
- # navigate(1)
576
- # else:
577
- # with st.form("data_form"):
578
- # st.session_state.user_id = st.text_input('User ID', value=user_id_from_url)
579
- # add_validated_submit([st.session_state.user_id], "Please enter a valid user ID")
580
 
581
  elif st.session_state.current_index == -1:
582
  add_annotation_guidelines()
@@ -603,10 +598,15 @@ if 0 <= st.session_state.current_index < len(st.session_state.data):
603
  st.write(f"Page {st.session_state.current_index + 1} out of {len(st.session_state.data)}")
604
 
605
  def navigate_to_index():
606
- if st.session_state.go_to-1 in range(len(st.session_state.data)):
607
- navigate_to_row(st.session_state.go_to)
608
- else:
609
- st.info(f"{st.session_state.go_to} not in range of data.")
 
 
 
 
 
610
 
611
  with st.form(key="nav_form"):
612
  st.number_input("Go to Page: ", key="go_to")
 
311
 
312
  # Function to navigate to specific rows
313
  def navigate_to_row(index):
314
+ if 0 <= index - 1 < len(st.session_state.data):
315
+ st.session_state.current_index = index - 1
316
+ # only works consistently if done before rerun
317
+ js = '''
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  <script>
319
+ var body = window.parent.document.querySelector(".main");
320
+
321
+ body.scrollTop = 0;
322
+ window.scrollY = 0;
 
 
 
 
323
  </script>
324
+ '''
325
+ st.components.v1.html(js, height=0)
326
+ # https://discuss.streamlit.io/t/click-twice-on-button-for-changing-state/45633/2
327
+
328
+ # disable text input enter to submit
329
+ # https://discuss.streamlit.io/t/text-input-how-to-disable-press-enter-to-apply/14457/6
330
+ components.html(
331
+ """
332
+ <script>
333
+ const inputs = window.parent.document.querySelectorAll('input');
334
+ inputs.forEach(input => {
335
+ input.addEventListener('keydown', function(event) {
336
+ if (event.key === 'Enter') {
337
+ event.preventDefault();
338
+ }
339
+ });
340
+ });
341
+ </script>
342
+ """,
343
+ height=0
344
+ )
345
+
346
+ st.rerun()
347
+ else:
348
+ st.error("Index out of range.")
349
 
350
 
351
  def show_field(f: Field, index: int, data_collected):
 
572
  st.session_state.user_id = st.text_input('User ID', value=user_id_from_url)
573
  add_validated_submit([st.session_state.user_id], "Please enter a valid user ID")
574
 
 
 
 
 
 
 
 
 
575
 
576
  elif st.session_state.current_index == -1:
577
  add_annotation_guidelines()
 
598
  st.write(f"Page {st.session_state.current_index + 1} out of {len(st.session_state.data)}")
599
 
600
  def navigate_to_index():
601
+ try:
602
+ go_to_index = int(st.session_state.go_to)
603
+ # Check if the index is within the valid range
604
+ if 0 <= go_to_index < len(st.session_state.data):
605
+ navigate_to_row(go_to_index)
606
+ else:
607
+ st.info(f"{go_to_index} is not in the range of data.")
608
+ except ValueError:
609
+ st.error("The page number must be an integer.")
610
 
611
  with st.form(key="nav_form"):
612
  st.number_input("Go to Page: ", key="go_to")