georad commited on
Commit
8b5014a
·
verified ·
1 Parent(s): 556e09d

Update pages/type_text.py

Browse files
Files changed (1) hide show
  1. pages/type_text.py +74 -71
pages/type_text.py CHANGED
@@ -26,79 +26,82 @@ def make_spinner(text = "In progress..."):
26
 
27
 
28
  def scrollToBottom():
29
- # JavaScript to scroll to bottom
30
- JS_code = """
31
- <script>
32
- // Function to scroll to the bottom of a specific container
33
- function scrollToBottom() {
34
- console.log("Attempting to scroll container to bottom."); // Log when scroll function is called
35
-
36
- // --- Identify the scrollable container ---
37
- // Streamlit often uses a main container div. We'll try to find it.
38
- // You might need to inspect your deployed app's HTML to find the correct selector
39
- // for the element that actually has the scrollbar.
40
- // Common candidates:
41
- // - Elements with classes like 'stAppViewContainer', 'main', 'block-container'
42
- // - A div directly containing your dynamic content
43
- // Once you've found the correct container div,
44
- // note its class names (e.g., class="some-streamlit-class another-class") or its ID (e.g., id="some-id").
45
- // If you found a class name like st-emotion-cache-abcxyz,
46
- // you'd change the line to: const scrollableContainer = document.querySelector('.st-emotion-cache-abcxyz');
47
- // If you found multiple class names, you might use one that seems most specific, or combine them if necessary (e.g., .class1.class2).
48
- // If you found an ID like my-streamlit-container,
49
- // you'd change the line to: const scrollableContainer = document.getElementById('my-streamlit-container'); (and use getElementById instead of querySelector)
50
- // *** REPLACE THE SELECTOR BELOW with the correct one you found by inspecting your app's HTML ***
51
- const scrollableContainer = document.querySelector('.flex flex-col min-h-dvh bg-white dark:bg-gray-950 text-black SpacePage') // <--- REPLACE THIS SELECTOR
52
- #const scrollableContainer = document.getElementById('your-container-id')); // <--- REPLACE THIS SELECTOR
53
-
54
- if (scrollableContainer) {
55
- console.log("Scrollable container found. Scrolling...");
56
- // Scroll the found container to its bottom
57
- scrollableContainer.scrollTo({
58
- top: scrollableContainer.scrollHeight,
59
- behavior: 'smooth'
60
- });
61
- } else {
62
- console.warn("Scrollable container not found. Falling back to document.body scroll.");
63
- // Fallback to body scroll if the specific container isn't found
64
- window.scrollTo({
65
- top: document.body.scrollHeight,
66
- behavior: 'smooth'
67
- });
 
 
68
  }
69
- }
70
-
71
- // --- Logic for handling dynamically added content ---
72
-
73
- // Create a MutationObserver instance.
74
- // This observer will watch for changes in the DOM.
75
- const observer = new MutationObserver(function(mutations) {
76
- console.log("MutationObserver detected changes."); // Log when observer fires
77
- // Use requestAnimationFrame to ensure the scroll happens after the browser
78
- // has potentially rendered the new content.
79
- requestAnimationFrame(scrollToBottom);
80
- });
81
-
82
- // Configure the observer to watch for changes to the child list of the body element.
83
- // We keep observing the body because Streamlit adds content there,
84
- // even if a child element is the one that scrolls.
85
- const config = { childList: true, subtree: true };
86
-
87
- console.log("Starting MutationObserver on document.body."); // Log when observer starts
88
- // Start observing the document body for the configured mutations.
89
- observer.observe(document.body, config);
90
-
91
- // --- Initial scroll on page load ---
92
- window.addEventListener('load', function() {
93
- console.log("Window loaded. Initial scroll attempt."); // Log on window load
94
- requestAnimationFrame(scrollToBottom);
95
- });
96
 
97
- </script>
98
- """
99
- # Render the JavaScript code
100
- html = f'<div style="display:none">{JS_code}</div>'
101
- st.components.v1.html(html, height=0, width=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
 
104
  def auto_scroll_to_bottom():
 
26
 
27
 
28
  def scrollToBottom():
29
+ # JavaScript to scroll to bottom
30
+ JS_code = """
31
+ <script>
32
+ // Wrap the code in an Immediately Invoked Function Expression (IIFE)
33
+ // to create a private scope and potentially avoid conflicts.
34
+ (function() {
35
+ // Function to scroll to the bottom of a specific container
36
+ function scrollToBottom() {
37
+ console.log("Attempting to scroll container to bottom."); // Log when scroll function is called
38
+
39
+ // --- Identify the scrollable container ---
40
+ // Streamlit often uses a main container div. We'll try to find it.
41
+ // YOU NEED TO INSPECT YOUR DEPLOYED APP'S HTML TO FIND THE CORRECT SELECTOR
42
+ // FOR THE ELEMENT THAT ACTUALLY HAS THE SCROLLBAR.
43
+ //
44
+ // Common candidates based on Streamlit versions and structure:
45
+ // - '.stAppViewContainer'
46
+ // - '.main'
47
+ // - '.block-container'
48
+ // - Look for divs with class names starting with 'st-emotion-cache-' followed by random characters.
49
+ // - A div directly containing your dynamic content (check its class or ID)
50
+
51
+ // *** REPLACE THE SELECTOR BELOW with the correct one you found by inspecting your app's HTML ***
52
+ // Use document.querySelector('.your-class-name') for a class, or document.getElementById('your-id') for an ID.
53
+ const scrollableContainer = document.querySelector('.flex min-h-dvh flex-col'); // <--- REPLACE THIS ENTIRE LINE IF USING getElementById
54
+
55
+ if (scrollableContainer) {
56
+ console.log("Scrollable container found. Scrolling...");
57
+ // Scroll the found container to its bottom
58
+ scrollableContainer.scrollTo({
59
+ top: scrollableContainer.scrollHeight,
60
+ behavior: 'smooth'
61
+ });
62
+ } else {
63
+ console.warn("Scrollable container not found using the specified selector. Please inspect your app's HTML to find the correct class or ID.");
64
+ // Fallback to body scroll (less likely to work for Streamlit's main content area)
65
+ window.scrollTo({
66
+ top: document.body.scrollHeight,
67
+ behavior: 'smooth'
68
+ });
69
+ }
70
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
+ // --- Logic for handling dynamically added content ---
73
+
74
+ // Create a MutationObserver instance.
75
+ // This observer will watch for changes in the DOM.
76
+ const observer = new MutationObserver(function(mutations) {
77
+ console.log("MutationObserver detected changes."); // Log when observer fires
78
+ // Use requestAnimationFrame to ensure the scroll happens after the browser
79
+ // has potentially rendered the new content.
80
+ requestAnimationFrame(scrollToBottom);
81
+ });
82
+
83
+ // Configure the observer to watch for changes to the child list of the body element.
84
+ // We keep observing the body because Streamlit adds content there,
85
+ // even if a child element is the one that scrolls.
86
+ const config = { childList: true, subtree: true };
87
+
88
+ console.log("Starting MutationObserver on document.body."); // Log when observer starts
89
+ // Start observing the document body for the configured mutations.
90
+ observer.observe(document.body, config);
91
+
92
+ // --- Initial scroll on page load ---
93
+ window.addEventListener('load', function() {
94
+ console.log("Window loaded. Initial scroll attempt."); // Log on window load
95
+ requestAnimationFrame(scrollToBottom);
96
+ });
97
+
98
+ })(); // End of IIFE
99
+
100
+ </script>
101
+ """
102
+ # Render the JavaScript code
103
+ html = f'<div style="display:none">{JS_code}</div>'
104
+ st.components.v1.html(html, height=0, width=0)
105
 
106
 
107
  def auto_scroll_to_bottom():