Update app.py
Browse files
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 |
-
|
6 |
|
7 |
# Function to capture a screenshot of a webpage
|
8 |
-
def capture_screenshot(url, output_path
|
9 |
chrome_options = Options()
|
10 |
-
chrome_options.add_argument('--headless')
|
11 |
-
chrome_options.add_argument('--disable-gpu')
|
12 |
-
chrome_options.add_argument('--no-sandbox')
|
13 |
-
chrome_options.add_argument('--window-size=1920,1080')
|
14 |
|
15 |
driver = webdriver.Chrome(options=chrome_options)
|
16 |
try:
|
17 |
-
driver.get(url)
|
18 |
-
driver.save_screenshot(output_path)
|
19 |
finally:
|
20 |
-
driver.quit()
|
21 |
|
22 |
-
# Streamlit
|
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 |
-
|
32 |
-
|
33 |
-
capture_screenshot(url.strip('"')) # Strip any extra quotes from the URL
|
34 |
|
35 |
-
#
|
36 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
except Exception as e:
|
39 |
st.error(f"An error occurred: {e}")
|
40 |
finally:
|
41 |
-
|
42 |
-
|
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)
|
|