georad commited on
Commit
abd1079
·
verified ·
1 Parent(s): 95a2cba

Update pages/type_text.py

Browse files
Files changed (1) hide show
  1. pages/type_text.py +46 -20
pages/type_text.py CHANGED
@@ -28,32 +28,58 @@ def scrollToBottom():
28
  # JavaScript to scroll to bottom
29
  JS_code = """
30
  <script>
31
- function scrollToBottom() {
32
- window.scrollTo({
33
- top: document.body.scrollHeight,
34
- behavior: 'smooth'
35
- });
36
- }
37
-
38
- const observer = new MutationObserver(function(mutations) {
39
- requestAnimationFrame(scrollToBottom);
40
- });
41
-
42
- const config = { childList: true, subtree: true };
43
- observer.observe(document.body, config);
44
-
45
- window.addEventListener('load', function() {
46
- requestAnimationFrame(scrollToBottom);
47
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  </script>
49
- """
50
-
51
  # Render the JavaScript code
52
  html = f'<div style="display:none">{JS_code}</div>'
53
  st.components.v1.html(html, height=0, width=0)
54
 
55
 
56
-
57
  def auto_scroll_to_bottom():
58
  # JavaScript to scroll to bottom
59
  js_code = """
 
28
  # JavaScript to scroll to bottom
29
  JS_code = """
30
  <script>
31
+ // Function to scroll to the bottom of the page
32
+ function scrollToBottom() {
33
+ console.log("Scrolling to bottom."); // Log when scroll function is called
34
+ // Use window.scrollTo to scroll to the bottom smoothly.
35
+ // The first argument is the x-coordinate (0 for horizontal scroll).
36
+ // The second argument is the y-coordinate, document.body.scrollHeight gives the total height of the page.
37
+ // The 'behavior: "smooth"' option provides a smooth scrolling animation.
38
+ window.scrollTo({
39
+ top: document.body.scrollHeight,
40
+ behavior: 'smooth'
41
+ });
42
+ }
43
+
44
+ // --- Logic for handling dynamically added content ---
45
+
46
+ // Create a MutationObserver instance.
47
+ // This observer will watch for changes in the DOM.
48
+ const observer = new MutationObserver(function(mutations) {
49
+ console.log("MutationObserver detected changes."); // Log when observer fires
50
+ // We don't need to inspect the mutations array in detail for this case.
51
+ // Any significant change in the body's content (like new elements being added by Streamlit)
52
+ // should trigger a scroll to the bottom.
53
+
54
+ // Use requestAnimationFrame to ensure the scroll happens after the browser
55
+ // has potentially rendered the new content. This helps prevent choppy scrolling.
56
+ requestAnimationFrame(scrollToBottom);
57
+ });
58
+
59
+ // Configure the observer to watch for changes to the child list of the body element.
60
+ // This means it will trigger when elements are added or removed directly from the body.
61
+ const config = { childList: true, subtree: true }; // subtree: true to watch descendants as well
62
+
63
+ console.log("Starting MutationObserver on document.body."); // Log when observer starts
64
+ // Start observing the document body for the configured mutations.
65
+ // This will start the auto-scrolling whenever Streamlit adds new content to the page.
66
+ observer.observe(document.body, config);
67
+
68
+ // --- Initial scroll on page load (optional, but good for initial state) ---
69
+ // You might still want an initial scroll when the page first loads,
70
+ // in case there's content already present.
71
+ window.addEventListener('load', function() {
72
+ console.log("Window loaded. Initial scroll attempt."); // Log on window load
73
+ requestAnimationFrame(scrollToBottom);
74
+ });
75
+
76
  </script>
77
+ """
 
78
  # Render the JavaScript code
79
  html = f'<div style="display:none">{JS_code}</div>'
80
  st.components.v1.html(html, height=0, width=0)
81
 
82
 
 
83
  def auto_scroll_to_bottom():
84
  # JavaScript to scroll to bottom
85
  js_code = """