memex-in commited on
Commit
290a6c0
·
verified ·
1 Parent(s): 42dce2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -21
app.py CHANGED
@@ -1,43 +1,50 @@
1
  import os
 
2
  import streamlit as st
3
  from selenium import webdriver
4
  from selenium.webdriver.chrome.options import Options
5
- from PIL import Image
6
 
7
  # Function to capture a screenshot of a webpage
8
- def capture_screenshot(url, output_path='screenshot.png'):
9
  chrome_options = Options()
10
- chrome_options.add_argument('--headless') # Run Chrome in headless mode
11
- chrome_options.add_argument('--disable-gpu') # Disable GPU acceleration
12
- chrome_options.add_argument('--no-sandbox') # Disable sandbox for better compatibility
13
- chrome_options.add_argument('--window-size=1920,1080') # Set window size for consistent screenshots
14
 
15
  driver = webdriver.Chrome(options=chrome_options)
16
  try:
17
- driver.get(url) # Open the URL
18
- driver.save_screenshot(output_path) # Save screenshot to file
19
  finally:
20
- driver.quit() # Ensure the driver is closed even if an error occurs
21
 
22
- # Streamlit app layout
23
- st.title("Web Screenshot Viewer")
24
 
25
- # Input field for URL
26
  url = st.text_input("Enter a website URL:", "https://example.com")
 
27
 
28
- # Button to trigger the screenshot process
29
  if st.button("Generate Screenshot"):
 
30
  try:
31
- screenshot_file = "screenshot.png"
32
- st.write("Capturing website screenshot...")
33
- capture_screenshot(url.strip('"')) # Strip any extra quotes from the URL
34
 
35
- # Display the screenshot in the Streamlit app
36
- st.image(screenshot_file, caption='Screenshot of the webpage', use_column_width=True)
 
 
 
 
 
 
 
 
37
 
38
  except Exception as e:
39
  st.error(f"An error occurred: {e}")
40
  finally:
41
- # Clean up the screenshot file if it exists
42
- if os.path.exists(screenshot_file):
43
- os.remove(screenshot_file)
 
1
  import os
2
+ import time
3
  import streamlit as st
4
  from selenium import webdriver
5
  from selenium.webdriver.chrome.options import Options
6
+ import tempfile
7
 
8
  # Function to capture a screenshot of a webpage
9
+ def capture_screenshot(url, output_path):
10
  chrome_options = Options()
11
+ chrome_options.add_argument('--headless')
12
+ chrome_options.add_argument('--disable-gpu')
13
+ chrome_options.add_argument('--no-sandbox')
14
+ chrome_options.add_argument('--window-size=1920,1080')
15
 
16
  driver = webdriver.Chrome(options=chrome_options)
17
  try:
18
+ driver.get(url)
19
+ driver.save_screenshot(output_path)
20
  finally:
21
+ driver.quit()
22
 
23
+ # Streamlit UI
24
+ st.title("Timed Web Screenshot Viewer")
25
 
 
26
  url = st.text_input("Enter a website URL:", "https://example.com")
27
+ wait_time = st.number_input("Time to wait before taking screenshot (in seconds):", min_value=1, max_value=30, value=5, step=1)
28
 
 
29
  if st.button("Generate Screenshot"):
30
+ screenshot_path = None # Initialize to ensure it's defined
31
  try:
32
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_file:
33
+ screenshot_path = tmp_file.name
 
34
 
35
+ # Show a countdown
36
+ with st.empty():
37
+ for i in range(wait_time, 0, -1):
38
+ st.info(f"Waiting... {i} seconds left")
39
+ time.sleep(1)
40
+
41
+ st.write("Capturing screenshot...")
42
+ capture_screenshot(url.strip('"'), screenshot_path)
43
+
44
+ st.image(screenshot_path, caption='Screenshot of the webpage', use_column_width=True)
45
 
46
  except Exception as e:
47
  st.error(f"An error occurred: {e}")
48
  finally:
49
+ if screenshot_path and os.path.exists(screenshot_path):
50
+ os.remove(screenshot_path)