Skip to content

Commit 5e4ff04

Browse files
committed
Add Chrome and ChromeDriver installation to Dockerfile; enhance screenshot functionality with improved options and logging
1 parent 256f37c commit 5e4ff04

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

Dockerfile

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
# Use a Python image with uv pre-installed
22
FROM ghcr.io/astral-sh/uv:python3.11-bookworm
33

4+
# Install Chrome and dependencies
5+
RUN apt-get update && apt-get install -y \
6+
wget \
7+
gnupg \
8+
curl \
9+
unzip \
10+
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
11+
&& echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
12+
&& apt-get update \
13+
&& apt-get install -y \
14+
google-chrome-stable \
15+
&& rm -rf /var/lib/apt/lists/*
16+
17+
# Install ChromeDriver
18+
RUN CHROME_VERSION=$(google-chrome --version | awk '{print $3}' | awk -F'.' '{print $1}') \
19+
&& CHROMEDRIVER_VERSION=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROME_VERSION") \
20+
&& wget -q "https://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip" \
21+
&& unzip chromedriver_linux64.zip \
22+
&& mv chromedriver /usr/local/bin/ \
23+
&& chmod +x /usr/local/bin/chromedriver \
24+
&& rm chromedriver_linux64.zip
25+
426
# Create a non-root user
527
RUN useradd -m -u 1000 appuser
628

@@ -50,6 +72,4 @@ USER appuser
5072

5173
EXPOSE 7860
5274
# Run the FastAPI application by default
53-
# Uses `fastapi dev` to enable hot-reloading when the `watch` sync occurs
54-
# Uses `--host 0.0.0.0` to allow access from outside the container
5575
CMD ["python", "main.py"]

src/modules/apps/__init__.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from selenium import webdriver
55
from selenium.webdriver.chrome.options import Options
66
from selenium.webdriver.chrome.service import Service
7+
from subprocess import PIPE, STDOUT
78
print("Gradio app loaded.")
89

910
def capture_page(url: str, output_file: str = "screenshot.png"):
@@ -14,15 +15,31 @@ def capture_page(url: str, output_file: str = "screenshot.png"):
1415
:param output_file: The filename to save the screenshot.
1516
"""
1617
options = Options()
17-
options.add_argument("--headless") # Run in headless mode
18-
options.add_argument("--window-size=1920,1080") # Set window size
18+
options.add_argument('--headless')
19+
options.add_argument('--no-sandbox') # Required in Docker
20+
options.add_argument('--disable-dev-shm-usage') # Required in Docker
21+
options.add_argument('--disable-gpu') # Required in Docker
22+
options.add_argument('--disable-software-rasterizer')
23+
options.add_argument('--window-size=1920,1080')
24+
options.add_argument('--disable-extensions')
25+
options.add_argument('--disable-infobars')
1926

20-
# Initialize Chrome service
21-
service = Service()
22-
driver = webdriver.Chrome(service=service, options=options)
27+
# Set up Chrome service with explicit path to chromedriver and logging
28+
service = Service(
29+
executable_path='/usr/local/bin/chromedriver',
30+
log_output=PIPE # Redirect logs to pipe
31+
)
32+
33+
# Initialize Chrome with the service and options
34+
driver = webdriver.Chrome(
35+
service=service,
36+
options=options
37+
)
2338

2439
try:
2540
driver.get(url)
41+
# Add a small delay to ensure page loads completely
42+
driver.implicitly_wait(5)
2643
driver.save_screenshot(output_file)
2744
print(f"Screenshot saved: {output_file}")
2845
finally:
@@ -44,6 +61,7 @@ def capture_and_show(url: str):
4461
# Return the image path
4562
return temp_path
4663
except Exception as e:
64+
print(f"Error in capture_and_show: {str(e)}") # Add detailed logging
4765
return f"Error capturing page: {str(e)}"
4866

4967
def create_gradio_app():

0 commit comments

Comments
 (0)