kambris commited on
Commit
7e0131c
·
verified ·
1 Parent(s): 8eaf0c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -30
app.py CHANGED
@@ -354,43 +354,34 @@ if uploaded_file is not None:
354
  plt.xticks(rotation=45)
355
  st.pyplot(fig2)
356
 
357
-
358
  st.subheader("Word Sequence Viewer")
359
 
360
- # Initialize session state
361
  if 'word_index' not in st.session_state:
362
  st.session_state.word_index = 0
363
 
364
- # Pre-process data for faster access
365
  available_folios = sorted(set(line_data['folio'] for line_data in word_positions))
366
  selected_folio = st.selectbox("Select Folio:", [''] + available_folios, key='folio_select')
367
 
368
- # Add jump-to input
369
- col1, col2 = st.columns([2, 1])
370
- with col1:
371
- selected_folio = st.selectbox("Select Folio:", [''] + available_folios, key='folio_select')
372
- with col2:
373
- jump_to = st.number_input("Jump to word:", min_value=1, max_value=len(filtered_words) if filtered_words else 1)
374
- if jump_to != st.session_state.word_index + 1:
375
- st.session_state.word_index = jump_to - 1
376
-
377
- # Filter words more efficiently
378
  filtered_words = []
 
 
 
379
  if selected_folio:
380
- filtered_words = [(word, chars)
381
- for line_data in word_positions
382
- if line_data['folio'] == selected_folio
383
- for word, _, chars in line_data['words']]
 
 
384
  else:
385
- filtered_words = list(zip(words, chars_list))
386
 
387
  if filtered_words:
388
- current_word, current_chars = filtered_words[st.session_state.word_index]
389
 
390
- # Display word info
391
  st.write(f"Word {st.session_state.word_index + 1} of {len(filtered_words)}")
392
 
393
- # Display character grid
394
  cols = st.columns(12)
395
  for i in range(12):
396
  with cols[i]:
@@ -411,29 +402,39 @@ if uploaded_file is not None:
411
  </div>
412
  """, unsafe_allow_html=True)
413
 
414
- # Navigation with page size options
 
 
 
 
 
 
 
 
 
 
 
415
  col1, col2, col3, col4 = st.columns([1,1,1,2])
416
  with col1:
417
- if st.button("◀ -10", disabled=st.session_state.word_index < 10):
418
- st.session_state.word_index = max(0, st.session_state.word_index - 10)
419
  st.rerun()
420
 
421
  with col2:
422
- if st.button("Previous", disabled=st.session_state.word_index <= 0):
423
  st.session_state.word_index -= 1
424
  st.rerun()
425
 
426
  with col3:
427
- if st.button("Next ", disabled=st.session_state.word_index >= len(filtered_words) - 1):
428
  st.session_state.word_index += 1
429
  st.rerun()
430
 
431
- if st.button("+10", disabled=st.session_state.word_index >= len(filtered_words) - 10):
432
- st.session_state.word_index = min(len(filtered_words) - 1, st.session_state.word_index + 10)
433
  st.rerun()
434
 
435
  with col4:
436
- line_info = next((line_data for line_data in word_positions
437
- if any(word == current_word for word, _, _ in line_data['words'])), None)
438
  if line_info:
439
  st.write(f"Folio: {line_info['folio']}, Par: {line_info['par']}, Line: {line_info['line']}")
 
 
354
  plt.xticks(rotation=45)
355
  st.pyplot(fig2)
356
 
 
357
  st.subheader("Word Sequence Viewer")
358
 
 
359
  if 'word_index' not in st.session_state:
360
  st.session_state.word_index = 0
361
 
 
362
  available_folios = sorted(set(line_data['folio'] for line_data in word_positions))
363
  selected_folio = st.selectbox("Select Folio:", [''] + available_folios, key='folio_select')
364
 
365
+ # Pre-process data with line information
 
 
 
 
 
 
 
 
 
366
  filtered_words = []
367
+ line_boundaries = [] # Store indices where new lines start
368
+ current_line = None
369
+
370
  if selected_folio:
371
+ for line_data in word_positions:
372
+ if line_data['folio'] == selected_folio:
373
+ if current_line != (line_data['par'], line_data['line']):
374
+ line_boundaries.append(len(filtered_words))
375
+ current_line = (line_data['par'], line_data['line'])
376
+ filtered_words.extend([(word, chars, line_data) for word, _, chars in line_data['words']])
377
  else:
378
+ filtered_words = [(word, chars, None) for word, chars in zip(words, chars_list)]
379
 
380
  if filtered_words:
381
+ current_word, current_chars, line_info = filtered_words[st.session_state.word_index]
382
 
 
383
  st.write(f"Word {st.session_state.word_index + 1} of {len(filtered_words)}")
384
 
 
385
  cols = st.columns(12)
386
  for i in range(12):
387
  with cols[i]:
 
402
  </div>
403
  """, unsafe_allow_html=True)
404
 
405
+ def get_next_line_index():
406
+ for boundary in line_boundaries:
407
+ if boundary > st.session_state.word_index:
408
+ return boundary
409
+ return len(filtered_words) - 1
410
+
411
+ def get_prev_line_index():
412
+ for boundary in reversed(line_boundaries):
413
+ if boundary < st.session_state.word_index:
414
+ return boundary
415
+ return 0
416
+
417
  col1, col2, col3, col4 = st.columns([1,1,1,2])
418
  with col1:
419
+ if st.button("◀ Previous Line", disabled=st.session_state.word_index <= 0):
420
+ st.session_state.word_index = get_prev_line_index()
421
  st.rerun()
422
 
423
  with col2:
424
+ if st.button("Previous Word", disabled=st.session_state.word_index <= 0):
425
  st.session_state.word_index -= 1
426
  st.rerun()
427
 
428
  with col3:
429
+ if st.button("Next Word", disabled=st.session_state.word_index >= len(filtered_words) - 1):
430
  st.session_state.word_index += 1
431
  st.rerun()
432
 
433
+ if st.button("Next Line ▶", disabled=st.session_state.word_index >= len(filtered_words) - 1):
434
+ st.session_state.word_index = get_next_line_index()
435
  st.rerun()
436
 
437
  with col4:
 
 
438
  if line_info:
439
  st.write(f"Folio: {line_info['folio']}, Par: {line_info['par']}, Line: {line_info['line']}")
440
+