Mahdi Naser Moghadasi commited on
Commit
7cd64bd
·
1 Parent(s): a081fee

Add aggressive auto-scroll: Multiple attempts and frequent checks for reliable scrolling

Browse files
Files changed (1) hide show
  1. app.py +51 -11
app.py CHANGED
@@ -1513,8 +1513,25 @@ with gr.Blocks(
1513
 
1514
  // Also try to scroll after any Gradio update
1515
  window.addEventListener('load', function() {
1516
- setInterval(scrollChatToBottom, 1000);
1517
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1518
  </script>
1519
  """
1520
  ) as demo:
@@ -1709,19 +1726,42 @@ with gr.Blocks(
1709
  </div>
1710
  """
1711
 
1712
- # Add auto-scroll trigger
1713
  scroll_script = """
1714
  <script>
1715
- setTimeout(function() {
1716
- const chatContainer = document.querySelector('#albert_chat .chatbot') ||
1717
- document.querySelector('#albert_chat .overflow-y-auto') ||
1718
- document.querySelector('.chatbot .overflow-y-auto');
1719
- if (chatContainer) {
1720
- chatContainer.scrollTop = chatContainer.scrollHeight;
1721
- } else {
1722
- window.scrollTo(0, document.body.scrollHeight);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1723
  }
1724
- }, 100);
 
 
 
 
 
 
1725
  </script>
1726
  """
1727
 
 
1513
 
1514
  // Also try to scroll after any Gradio update
1515
  window.addEventListener('load', function() {
1516
+ setInterval(scrollChatToBottom, 500);
1517
  });
1518
+
1519
+ // More aggressive auto-scroll for chat updates
1520
+ function aggressiveScroll() {
1521
+ const chatElement = document.querySelector('#albert_chat');
1522
+ if (chatElement) {
1523
+ // Look for any scrollable container within the chat
1524
+ const scrollableElements = chatElement.querySelectorAll('*');
1525
+ scrollableElements.forEach(el => {
1526
+ if (el.scrollHeight > el.clientHeight) {
1527
+ el.scrollTop = el.scrollHeight;
1528
+ }
1529
+ });
1530
+ }
1531
+ }
1532
+
1533
+ // Run aggressive scroll more frequently
1534
+ setInterval(aggressiveScroll, 200);
1535
  </script>
1536
  """
1537
  ) as demo:
 
1726
  </div>
1727
  """
1728
 
1729
+ # Add auto-scroll trigger with multiple attempts
1730
  scroll_script = """
1731
  <script>
1732
+ function forceScrollToBottom() {
1733
+ // Try multiple selectors and methods
1734
+ const selectors = [
1735
+ '#albert_chat .chatbot',
1736
+ '#albert_chat .overflow-y-auto',
1737
+ '#albert_chat .h-full',
1738
+ '.chatbot .overflow-y-auto',
1739
+ '.chatbot .h-full',
1740
+ '[data-testid="chatbot"]',
1741
+ '.gradio-chatbot'
1742
+ ];
1743
+
1744
+ let scrolled = false;
1745
+ for (const selector of selectors) {
1746
+ const element = document.querySelector(selector);
1747
+ if (element) {
1748
+ element.scrollTop = element.scrollHeight;
1749
+ scrolled = true;
1750
+ break;
1751
+ }
1752
+ }
1753
+
1754
+ // If no chat container found, scroll the whole page
1755
+ if (!scrolled) {
1756
+ window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
1757
  }
1758
+ }
1759
+
1760
+ // Try scrolling multiple times to ensure it works
1761
+ setTimeout(forceScrollToBottom, 50);
1762
+ setTimeout(forceScrollToBottom, 200);
1763
+ setTimeout(forceScrollToBottom, 500);
1764
+ setTimeout(forceScrollToBottom, 1000);
1765
  </script>
1766
  """
1767