Skip to content

Commit 5da1d69

Browse files
authored
Added headless browser example (#89)
* added headless browser example * change folder
1 parent 36c7f54 commit 5da1d69

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Generated by Beam SDK
2+
.beamignore
3+
pyproject.toml
4+
.git
5+
.idea
6+
.python-version
7+
.vscode
8+
.venv
9+
venv
10+
__pycache__
11+
.DS_Store
12+
.config
13+
drive/MyDrive
14+
.coverage
15+
.pytest_cache
16+
.ipynb
17+
.ruff_cache
18+
.dockerignore
19+
.ipynb_checkpoints
20+
.env.local
21+
.envrc
22+
**/__pycache__/
23+
**/.pytest_cache/
24+
**/node_modules/
25+
**/.venv/
26+
*.pyc
27+
.next/
28+
.circleci
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Headless Browser API Example
2+
3+
A headless browser API powered by Playwright, capturing full-page website screenshots.
4+
5+
## Deployment
6+
7+
Deploy the app on Beam:
8+
9+
```
10+
beam deploy app.py:browser
11+
```
12+
13+
## API Usage
14+
15+
Send a `POST` request to the endpoint with the following JSON body:
16+
17+
```json
18+
{
19+
"url": "https://your-website-url"
20+
}
21+
```
22+
23+
### Example Request:
24+
25+
```json
26+
{
27+
"url": "https://example.com"
28+
}
29+
```
30+
31+
### Example Response:
32+
33+
```json
34+
{
35+
"output_url": "https://app.beam.cloud/output/id/9dfbb7a1-a3de-489c-a602-423b4c859f84"
36+
}
37+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from beam import Output, endpoint, Image, env
2+
import asyncio
3+
4+
if env.is_remote():
5+
from playwright.async_api import async_playwright
6+
7+
image = (
8+
Image(python_version="python3.11")
9+
.add_python_packages(
10+
[
11+
"playwright",
12+
]
13+
)
14+
.add_commands(["playwright install chromium", "playwright install-deps chromium"])
15+
)
16+
17+
18+
@endpoint(name="headless-browser", cpu=2, memory="16Gi", image=image)
19+
async def browser(url: str = "https://example.com"):
20+
print(f"Navigating to: {url}")
21+
output_path = "/tmp/screenshot.png"
22+
23+
async with async_playwright() as playwright:
24+
browser = await playwright.chromium.launch(
25+
headless=True,
26+
args=["--no-sandbox", "--disable-dev-shm-usage"],
27+
)
28+
print("Browser launched successfully")
29+
30+
try:
31+
page = await browser.new_page()
32+
await page.set_viewport_size({"width": 1920, "height": 1080})
33+
34+
await page.goto(url, wait_until="networkidle")
35+
print("Page loaded, waiting 2 seconds for any dynamic content...")
36+
37+
await asyncio.sleep(2)
38+
39+
await page.screenshot(path=output_path, full_page=True, type="png")
40+
print(f"Screenshot saved as: {output_path}")
41+
42+
finally:
43+
await browser.close()
44+
45+
output_file = Output(path=output_path)
46+
output_file.save()
47+
public_url = output_file.public_url(expires=400)
48+
49+
return {"output_url": public_url}

0 commit comments

Comments
 (0)