Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import undetected_chromedriver as uc | |
| from time import perf_counter, sleep | |
| import threading | |
| import time | |
| def refresh_page(url, refresh_interval, stop_event): | |
| # Configure undetected Chrome driver | |
| options = uc.ChromeOptions() | |
| options.add_argument("--no-sandbox") | |
| options.add_argument("--disable-dev-shm-usage") | |
| options.add_argument("--disable-blink-features=AutomationControlled") | |
| options.add_argument("--start-maximized") | |
| # Initialize the browser | |
| driver = uc.Chrome(options=options) | |
| try: | |
| # Open the link | |
| driver.get(url) | |
| st.write("Loaded the page successfully.") | |
| # Refresh loop | |
| while not stop_event.is_set(): | |
| start_time = perf_counter() | |
| driver.refresh() | |
| elapsed_time = perf_counter() - start_time | |
| st.write(f"Page refreshed. Time taken: {elapsed_time:.6f} seconds") | |
| time.sleep(refresh_interval) | |
| except Exception as e: | |
| st.error(f"An error occurred: {e}") | |
| finally: | |
| driver.quit() | |
| def main(): | |
| st.title("Web Page Auto-Refresh") | |
| # URL input | |
| url = st.text_input("Enter URL to refresh", | |
| "https://solscan.io/account/FWH9cXncf2C7fmbRhweeNGLNKKZ23qiyXyBk4ex8ic3y") | |
| # Refresh interval input | |
| refresh_interval = st.slider("Refresh Interval (seconds)", | |
| min_value=0.5, | |
| max_value=10.0, | |
| value=4.0, | |
| step=0.5) | |
| # Stop event for thread control | |
| stop_event = threading.Event() | |
| # Start and Stop buttons | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| if st.button("Start Refreshing"): | |
| # Create and start the thread | |
| refresh_thread = threading.Thread( | |
| target=refresh_page, | |
| args=(url, refresh_interval, stop_event) | |
| ) | |
| refresh_thread.start() | |
| st.session_state.refresh_thread = refresh_thread | |
| st.success("Refreshing started!") | |
| with col2: | |
| if st.button("Stop Refreshing"): | |
| if hasattr(st.session_state, 'refresh_thread'): | |
| stop_event.set() | |
| st.session_state.refresh_thread.join() | |
| st.warning("Refreshing stopped!") | |
| if __name__ == "__main__": | |
| main() |