georad commited on
Commit
f5fb270
·
verified ·
1 Parent(s): 3999ab4

Update pages/type_text.py

Browse files
Files changed (1) hide show
  1. pages/type_text.py +31 -27
pages/type_text.py CHANGED
@@ -47,40 +47,46 @@ def convert_json(df:pd.DataFrame):
47
  #st.json(json_string, expanded=True)
48
  return json_string
49
 
50
- def auto_scroll_to_bottom():
51
- # JavaScript to scroll to bottom
52
  js_code = """
53
  <script>
54
- // Wait for the page to fully render
55
- const scrollToBottom = () => {
56
- // Get the main Streamlit iframe
57
- const streamlitDoc = window.parent.document;
58
- // Get the app container
59
- const appContainer = streamlitDoc.querySelector('.main');
60
- if (appContainer) {
61
- // Scroll the app container to the bottom
62
- appContainer.scrollTop = appContainer.scrollHeight;
63
- } else {
64
- // Fallback to scrolling the entire page
65
- window.parent.scrollTo(0, streamlitDoc.body.scrollHeight);
66
  }
67
- };
68
 
69
- // Try immediately
70
- scrollToBottom();
 
 
71
 
72
- // Also try after a short delay to ensure content is rendered
73
- setTimeout(scrollToBottom, 200);
 
 
 
 
74
 
75
- // And after a longer delay just to be safe
76
- setTimeout(scrollToBottom, 500);
 
 
 
 
 
77
  </script>
78
  """
79
 
80
- # Render the JavaScript code
81
- html = f'<div style="display:none">{js_code}</div>'
82
- st.components.v1.html(html, height=0)
83
-
84
 
85
  #df_chapters = pd.read_csv("SBS_V2_0/Chapter_Index_Rows.csv")
86
  df_chapters = pd.read_csv("SBS_V2_0/Chapter_Index_Rows_with_total.csv")
@@ -141,8 +147,6 @@ dfALL = pd.DataFrame.from_dict(dictA)
141
 
142
  #INTdesc_input = st.text_input("Type internal description", key="user_input")
143
  INTdesc_input = st.text_input(r"$\textsf{\Large Type internal description}$", key="user_input")
144
- auto_scroll_to_bottom()
145
-
146
  placeholder, centerL_column, centerR_column, right_column = st.columns(4)
147
  #placeholder_clicked = placeholder.button("Perform some action", key="user_placeholder")
148
  right_column.button("Reset description", on_click=on_click)
 
47
  #st.json(json_string, expanded=True)
48
  return json_string
49
 
50
+ def scroll_to_bottom():
51
+ # This script uses Streamlit's message system to ensure proper timing
52
  js_code = """
53
  <script>
54
+ // Function to scroll to bottom
55
+ function scrollToBottom() {
56
+ // This targets Streamlit's specific structure
57
+ const mainContainer = window.parent.document.querySelector('.stApp');
58
+ if (mainContainer) {
59
+ mainContainer.scrollTo({
60
+ top: mainContainer.scrollHeight,
61
+ behavior: 'smooth'
62
+ });
 
 
 
63
  }
64
+ }
65
 
66
+ // Use MutationObserver to detect when content is added
67
+ const observer = new MutationObserver((mutations) => {
68
+ scrollToBottom();
69
+ });
70
 
71
+ // Start observing the document body for changes
72
+ const streamlitDoc = window.parent.document;
73
+ observer.observe(streamlitDoc.body, {
74
+ childList: true,
75
+ subtree: true
76
+ });
77
 
78
+ // Initial scroll and disconnect after some time
79
+ scrollToBottom();
80
+ setTimeout(() => {
81
+ observer.disconnect();
82
+ // One final scroll
83
+ scrollToBottom();
84
+ }, 1000);
85
  </script>
86
  """
87
 
88
+ # Use unsafe_allow_html to inject JavaScript
89
+ st.markdown(js_code, unsafe_allow_html=True)
 
 
90
 
91
  #df_chapters = pd.read_csv("SBS_V2_0/Chapter_Index_Rows.csv")
92
  df_chapters = pd.read_csv("SBS_V2_0/Chapter_Index_Rows_with_total.csv")
 
147
 
148
  #INTdesc_input = st.text_input("Type internal description", key="user_input")
149
  INTdesc_input = st.text_input(r"$\textsf{\Large Type internal description}$", key="user_input")
 
 
150
  placeholder, centerL_column, centerR_column, right_column = st.columns(4)
151
  #placeholder_clicked = placeholder.button("Perform some action", key="user_placeholder")
152
  right_column.button("Reset description", on_click=on_click)