Skip to content

Commit 256f37c

Browse files
committed
Enhance Dockerfile and screenshot functionality: create cache directories, set permissions, and initialize Chrome service for improved screenshot capture.
1 parent 1aa5770 commit 256f37c

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

Dockerfile

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ ENTRYPOINT []
3434
# Run setup.py
3535
RUN python setup.py
3636

37-
# Create cache directory and set permissions
37+
# Create cache directories and set permissions
3838
RUN mkdir -p /.cache/selenium && \
39+
mkdir -p /tmp/screenshots && \
3940
chown -R appuser:appuser /.cache/selenium && \
40-
chmod 755 /.cache/selenium
41+
chown -R appuser:appuser /tmp/screenshots && \
42+
chmod 755 /.cache/selenium && \
43+
chmod 755 /tmp/screenshots
44+
45+
# Set temporary directory environment variable
46+
ENV TMPDIR=/tmp/screenshots
4147

4248
# Set user
4349
USER appuser

src/modules/apps/__init__.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import os
44
from selenium import webdriver
55
from selenium.webdriver.chrome.options import Options
6+
from selenium.webdriver.chrome.service import Service
67
print("Gradio app loaded.")
8+
79
def capture_page(url: str, output_file: str = "screenshot.png"):
810
"""
911
Captures a screenshot of the given webpage.
@@ -14,8 +16,10 @@ def capture_page(url: str, output_file: str = "screenshot.png"):
1416
options = Options()
1517
options.add_argument("--headless") # Run in headless mode
1618
options.add_argument("--window-size=1920,1080") # Set window size
17-
18-
driver = webdriver.Chrome(options=options)
19+
20+
# Initialize Chrome service
21+
service = Service()
22+
driver = webdriver.Chrome(service=service, options=options)
1923

2024
try:
2125
driver.get(url)
@@ -27,9 +31,12 @@ def capture_page(url: str, output_file: str = "screenshot.png"):
2731
def capture_and_show(url: str):
2832
"""Capture webpage and return the image"""
2933
try:
30-
# Create temporary file for the screenshot
31-
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
32-
temp_path = tmp.name
34+
# Get the temporary directory path (defaulting to /tmp if TMPDIR is not set)
35+
temp_dir = os.getenv('TMPDIR', '/tmp')
36+
os.makedirs(temp_dir, exist_ok=True)
37+
38+
# Create temporary file in the specified directory
39+
temp_path = os.path.join(temp_dir, f"screenshot_{os.urandom(8).hex()}.png")
3340

3441
# Capture the webpage
3542
capture_page(url, temp_path)

0 commit comments

Comments
 (0)