AbhijitClemson commited on
Commit
beb9b0c
·
verified ·
1 Parent(s): 1254b68

Update page_files/Categorized_Search.py

Browse files
Files changed (1) hide show
  1. page_files/Categorized_Search.py +62 -53
page_files/Categorized_Search.py CHANGED
@@ -10,21 +10,14 @@ from streamlit_scroll_to_top import scroll_to_here
10
 
11
  from data_loader import get_all_sections, load_material_data
12
  import streamlit.components.v1 as components
 
 
13
  def clear_search():
14
  """Clear the search box and search term. Called as a callback from other widgets."""
15
  st.session_state._search_term = None
16
  st.session_state["_pending_clear_search"] = True
17
 
18
  def switch_tab(index, clear=True):
19
- count = st.session_state.get('_tab_switch_count', 0) + 1
20
- st.session_state['_tab_switch_count'] = count
21
- components.html(f"""
22
- <script>
23
- var _unique = {count};
24
- var tabs = window.parent.document.querySelectorAll('[data-testid="stTabs"] button[role="tab"]');
25
- if (tabs.length > {index}) tabs[{index}].click();
26
- </script>
27
- """, height=0)
28
  st.session_state.current_tab = index
29
  if clear:
30
  clear_search()
@@ -352,6 +345,15 @@ def load_all_data() -> pd.DataFrame:
352
  return pd.concat(frames, ignore_index=True)
353
 
354
 
 
 
 
 
 
 
 
 
 
355
  def extract_matrix_fiber(abbr: str):
356
  if not isinstance(abbr, str):
357
  return None, None
@@ -435,6 +437,7 @@ defaults = {
435
  "inspect_property": None,
436
  "_reset_prop_checks": False,
437
  "_scroll_to_results": False,
 
438
  }
439
 
440
  for key, value in defaults.items():
@@ -481,8 +484,8 @@ meta["Matrix"] = matrix_fiber.apply(
481
  meta["Fiber"] = matrix_fiber.apply(
482
  lambda pair: pair[1] if isinstance(pair, tuple) and len(pair) >= 2 else None
483
  )
484
-
485
- all_sections = get_all_sections()
486
 
487
  if st.session_state._reset_prop_checks:
488
  selected = set(st.session_state.selected_props)
@@ -641,6 +644,39 @@ with left_col:
641
  # st.session_state.current_page = 0
642
  # st.rerun()
643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
644
  if st.button(
645
  "🔎 Inspect",
646
  key="inspect_btn",
@@ -648,7 +684,6 @@ with left_col:
648
  type="primary",
649
  disabled=st.session_state.selected_row is None,
650
  ):
651
- st.info("Open the Inspect tab on the right panel.")
652
  st.session_state["_switch_to_tab"] = 1
653
  if st.session_state.selected_row:
654
  selected_abbr, selected_name = st.session_state.selected_row
@@ -656,6 +691,7 @@ with left_col:
656
  f"<div class='aim-selected'><b>Selected</b><br>{selected_name}<br><span style='font-family:monospace'>{selected_abbr}</span></div>",
657
  unsafe_allow_html=True,
658
  )
 
659
  if st.session_state.pop("_pending_clear_search", False):
660
  st.session_state.top_search_input = ""
661
  if st.session_state._search_term != st.session_state.get("_previous_search_term"):
@@ -711,21 +747,28 @@ with right_col:
711
  st.markdown(
712
  """
713
  <div style='padding:14px 18px 0;'>
714
- <div class='aim-breadcrumb'>INVENTORY / <span>MATERIALS DATABASE</span></div>
715
  <div class='aim-title'>Materials Database</div>
716
  </div>
717
  """,
718
  unsafe_allow_html=True,
719
  )
720
 
721
- tab_materials, tab_inspect = st.tabs(
722
- ["All Materials", "Inspect"]
723
- )
 
 
 
 
 
 
724
  if st.session_state.get("_switch_to_tab") is not None:
725
  tab_index = st.session_state["_switch_to_tab"]
726
  switch_tab(tab_index, clear=(tab_index != 1))
727
  del st.session_state["_switch_to_tab"]
728
- with tab_materials:
 
729
  filter_label = (
730
  ", ".join(st.session_state.active_classes)
731
  if st.session_state.active_classes
@@ -924,7 +967,7 @@ with right_col:
924
 
925
  st.rerun()
926
 
927
- with tab_inspect:
928
  if not st.session_state.selected_row:
929
  st.warning("Select a material in All Materials first.")
930
  else:
@@ -1095,38 +1138,4 @@ with right_col:
1095
 
1096
  if st.session_state._scroll_to_results:
1097
  scroll_to_here(0, key="inspect_results_scroll")
1098
- st.session_state._scroll_to_results = False
1099
-
1100
-
1101
-
1102
-
1103
-
1104
-
1105
-
1106
-
1107
-
1108
-
1109
-
1110
-
1111
-
1112
-
1113
-
1114
-
1115
-
1116
-
1117
-
1118
-
1119
-
1120
-
1121
-
1122
-
1123
-
1124
-
1125
-
1126
-
1127
-
1128
-
1129
-
1130
-
1131
-
1132
-
 
10
 
11
  from data_loader import get_all_sections, load_material_data
12
  import streamlit.components.v1 as components
13
+ import time
14
+ t0 = time.time()
15
  def clear_search():
16
  """Clear the search box and search term. Called as a callback from other widgets."""
17
  st.session_state._search_term = None
18
  st.session_state["_pending_clear_search"] = True
19
 
20
  def switch_tab(index, clear=True):
 
 
 
 
 
 
 
 
 
21
  st.session_state.current_tab = index
22
  if clear:
23
  clear_search()
 
345
  return pd.concat(frames, ignore_index=True)
346
 
347
 
348
+ @st.cache_data
349
+ def get_all_sections_cached() -> list:
350
+ # get_all_sections() re-queries the DB directly (bypassing load_data's
351
+ # cache) every time it's called. Wrapping it here means the 3 DB round
352
+ # trips only happen once per session instead of on every single rerun
353
+ # (every row click, class pill click, page nav, etc).
354
+ return get_all_sections()
355
+
356
+
357
  def extract_matrix_fiber(abbr: str):
358
  if not isinstance(abbr, str):
359
  return None, None
 
437
  "inspect_property": None,
438
  "_reset_prop_checks": False,
439
  "_scroll_to_results": False,
440
+ "current_tab": 0,
441
  }
442
 
443
  for key, value in defaults.items():
 
484
  meta["Fiber"] = matrix_fiber.apply(
485
  lambda pair: pair[1] if isinstance(pair, tuple) and len(pair) >= 2 else None
486
  )
487
+ st.sidebar.write(f"meta build: {time.time() - t0:.2f}s")
488
+ all_sections = get_all_sections_cached()
489
 
490
  if st.session_state._reset_prop_checks:
491
  selected = set(st.session_state.selected_props)
 
644
  # st.session_state.current_page = 0
645
  # st.rerun()
646
 
647
+ # if st.button(
648
+ # "🔎 Inspect",
649
+ # key="inspect_btn",
650
+ # use_container_width=True,
651
+ # type="primary",
652
+ # disabled=st.session_state.selected_row is None,
653
+ # ):
654
+ # st.info("Open the Inspect tab on the right panel.")
655
+ # st.session_state["_switch_to_tab"] = 1
656
+ # if st.session_state.selected_row:
657
+ # selected_abbr, selected_name = st.session_state.selected_row
658
+ # st.markdown(
659
+ # f"<div class='aim-selected'><b>Selected</b><br>{selected_name}<br><span style='font-family:monospace'>{selected_abbr}</span></div>",
660
+ # unsafe_allow_html=True,
661
+ # )
662
+
663
+ #st.markdown("<div class='aim-sec'>📋 Property Types</div>", unsafe_allow_html=True)
664
+ #selected_props = []
665
+ #with st.container(height=480):
666
+ # for index, section in enumerate(all_sections):
667
+ # key = f"prop_check_{index}"
668
+ # if key not in st.session_state:
669
+ # st.session_state[key] = section in st.session_state.selected_props
670
+
671
+ # if st.checkbox(section, key=key):
672
+ # selected_props.append(section)
673
+
674
+ #if selected_props != st.session_state.selected_props:
675
+ # st.session_state.selected_props = selected_props
676
+ # st.session_state.current_page = 0
677
+ # st.rerun()
678
+
679
+ with st.container(border=True):
680
  if st.button(
681
  "🔎 Inspect",
682
  key="inspect_btn",
 
684
  type="primary",
685
  disabled=st.session_state.selected_row is None,
686
  ):
 
687
  st.session_state["_switch_to_tab"] = 1
688
  if st.session_state.selected_row:
689
  selected_abbr, selected_name = st.session_state.selected_row
 
691
  f"<div class='aim-selected'><b>Selected</b><br>{selected_name}<br><span style='font-family:monospace'>{selected_abbr}</span></div>",
692
  unsafe_allow_html=True,
693
  )
694
+
695
  if st.session_state.pop("_pending_clear_search", False):
696
  st.session_state.top_search_input = ""
697
  if st.session_state._search_term != st.session_state.get("_previous_search_term"):
 
747
  st.markdown(
748
  """
749
  <div style='padding:14px 18px 0;'>
750
+ <!-- <div class='aim-breadcrumb'>INVENTORY / <span>MATERIALS DATABASE</span></div> -->
751
  <div class='aim-title'>Materials Database</div>
752
  </div>
753
  """,
754
  unsafe_allow_html=True,
755
  )
756
 
757
+ if st.button("Clear Selection", key="clear_selection_btn", type="primary"):
758
+ st.session_state.selected_row = None
759
+ st.session_state["last_synced_abbr"] = None
760
+ df_key = f"materials_df_{st.session_state.current_page}"
761
+ if df_key in st.session_state:
762
+ st.session_state[df_key] = {"selection": {"rows": [], "columns": [], "cells": []}}
763
+ st.session_state["_switch_to_tab"] = 0
764
+ st.rerun()
765
+
766
  if st.session_state.get("_switch_to_tab") is not None:
767
  tab_index = st.session_state["_switch_to_tab"]
768
  switch_tab(tab_index, clear=(tab_index != 1))
769
  del st.session_state["_switch_to_tab"]
770
+
771
+ if st.session_state.current_tab == 0:
772
  filter_label = (
773
  ", ".join(st.session_state.active_classes)
774
  if st.session_state.active_classes
 
967
 
968
  st.rerun()
969
 
970
+ else:
971
  if not st.session_state.selected_row:
972
  st.warning("Select a material in All Materials first.")
973
  else:
 
1138
 
1139
  if st.session_state._scroll_to_results:
1140
  scroll_to_here(0, key="inspect_results_scroll")
1141
+ st.session_state._scroll_to_results = False