memex-in commited on
Commit
1fc3cbb
·
verified ·
1 Parent(s): 4a92538

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -10
app.py CHANGED
@@ -4,26 +4,36 @@ from selenium import webdriver
4
  from selenium.webdriver.chrome.options import Options
5
  import ascii_magic
6
 
 
7
  def capture_screenshot(url, output_path='screenshot.png'):
8
  chrome_options = Options()
9
- chrome_options.add_argument('--headless')
10
- chrome_options.add_argument('--disable-gpu')
11
- chrome_options.add_argument('--no-sandbox')
12
- chrome_options.add_argument('--window-size=1920,1080')
13
 
14
  driver = webdriver.Chrome(options=chrome_options)
15
- driver.get(url)
16
- driver.save_screenshot(output_path)
17
- driver.quit()
 
 
18
 
 
19
  def convert_to_ascii(image_path):
20
- output = ascii_magic.from_image_path(image_path, columns=100, mode=ascii_magic.Modes.ASCII)
21
- return str(output)
 
 
 
22
 
 
23
  st.title("ASCII Web Screenshot Viewer")
24
 
 
25
  url = st.text_input("Enter a website URL:", "https://example.com")
26
 
 
27
  if st.button("Generate ASCII Screenshot"):
28
  try:
29
  screenshot_file = "screenshot.png"
@@ -33,8 +43,12 @@ if st.button("Generate ASCII Screenshot"):
33
  st.write("Converting to ASCII art...")
34
  ascii_output = convert_to_ascii(screenshot_file)
35
 
 
36
  st.text_area("ASCII Art Output", ascii_output, height=600)
37
- os.remove(screenshot_file)
38
 
39
  except Exception as e:
40
  st.error(f"An error occurred: {e}")
 
 
 
 
 
4
  from selenium.webdriver.chrome.options import Options
5
  import ascii_magic
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
+ # Function to convert an image to ASCII art
23
  def convert_to_ascii(image_path):
24
+ try:
25
+ output = ascii_magic.from_image_path(image_path, columns=100, mode=ascii_magic.Modes.ASCII)
26
+ return str(output)
27
+ except Exception as e:
28
+ raise Exception(f"Failed to convert image to ASCII: {e}")
29
 
30
+ # Streamlit app layout
31
  st.title("ASCII Web Screenshot Viewer")
32
 
33
+ # Input field for URL
34
  url = st.text_input("Enter a website URL:", "https://example.com")
35
 
36
+ # Button to trigger the screenshot and conversion process
37
  if st.button("Generate ASCII Screenshot"):
38
  try:
39
  screenshot_file = "screenshot.png"
 
43
  st.write("Converting to ASCII art...")
44
  ascii_output = convert_to_ascii(screenshot_file)
45
 
46
+ # Display ASCII art in a text area
47
  st.text_area("ASCII Art Output", ascii_output, height=600)
 
48
 
49
  except Exception as e:
50
  st.error(f"An error occurred: {e}")
51
+ finally:
52
+ # Clean up the screenshot file if it exists
53
+ if os.path.exists(screenshot_file):
54
+ os.remove(screenshot_file)