Skip to content

Commit 9ac0a92

Browse files
authored
Merge pull request #24 from browserbase/fix/cleanup-docs
cleanup docs and quickstart
2 parents eeb9e8f + ebf1923 commit 9ac0a92

File tree

2 files changed

+47
-43
lines changed

2 files changed

+47
-43
lines changed

Diff for: README.md

+33-38
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,45 @@ The full API of this library can be found in [api.md](api.md).
2828

2929
```python
3030
import os
31+
from playwright.sync_api import Playwright, sync_playwright
3132
from browserbase import Browserbase
3233

33-
client = Browserbase(
34+
BROWSERBASE_API_KEY = os.environ.get("BROWSERBASE_API_KEY")
35+
BROWSERBASE_PROJECT_ID = os.environ.get("BROWSERBASE_PROJECT_ID")
36+
37+
bb = Browserbase(
3438
# This is the default and can be omitted
35-
api_key=os.environ.get("BROWSERBASE_API_KEY"),
39+
api_key=BROWSERBASE_API_KEY,
3640
)
3741

38-
context = client.contexts.create(
39-
project_id="projectId",
40-
)
41-
print(context.id)
42-
```
42+
def run(playwright: Playwright) -> None:
43+
# Create a session on Browserbase
44+
session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID)
45+
46+
# Connect to the remote session
47+
chromium = playwright.chromium
48+
browser = chromium.connect_over_cdp(session.connect_url)
49+
context = browser.contexts[0]
50+
page = context.pages[0]
4351

44-
While you can provide an `api_key` keyword argument,
45-
we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
46-
to add `BROWSERBASE_API_KEY="My API Key"` to your `.env` file
47-
so that your API Key is not stored in source control.
52+
# Execute Playwright actions on the remote browser tab
53+
page.goto("https://news.ycombinator.com/")
54+
page_title = page.title()
55+
assert (
56+
page_title == "Hacker News"
57+
), f"Page title is not 'Hacker News', it is '{page_title}'"
58+
page.screenshot(path="screenshot.png")
59+
60+
page.close()
61+
browser.close()
62+
print("Done!")
63+
64+
65+
if __name__ == "__main__":
66+
with sync_playwright() as playwright:
67+
run(playwright)
68+
69+
```
4870

4971
## Examples
5072

@@ -63,33 +85,6 @@ rye run example playwright_basic # replace with the example you want to run
6385
> [!NOTE]
6486
> Make sure you have a `.env` file that matches the [.env.example](.env.example) file in the root of this repository.
6587
66-
## Async usage
67-
68-
Simply import `AsyncBrowserbase` instead of `Browserbase` and use `await` with each API call:
69-
70-
```python
71-
import os
72-
import asyncio
73-
from browserbase import AsyncBrowserbase
74-
75-
client = AsyncBrowserbase(
76-
# This is the default and can be omitted
77-
api_key=os.environ.get("BROWSERBASE_API_KEY"),
78-
)
79-
80-
81-
async def main() -> None:
82-
context = await client.contexts.create(
83-
project_id="projectId",
84-
)
85-
print(context.id)
86-
87-
88-
asyncio.run(main())
89-
```
90-
91-
Functionality between the synchronous and asynchronous clients is otherwise identical.
92-
9388
## Using types
9489

9590
Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev) which also provide helper methods for things like:

Diff for: examples/playwright_basic.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1+
import os
2+
13
from playwright.sync_api import Playwright, sync_playwright
24

3-
from examples import (
4-
BROWSERBASE_PROJECT_ID,
5-
bb,
5+
from browserbase import Browserbase
6+
7+
BROWSERBASE_API_KEY = os.environ.get("BROWSERBASE_API_KEY", "")
8+
if not BROWSERBASE_API_KEY:
9+
raise ValueError("BROWSERBASE_API_KEY is not set")
10+
BROWSERBASE_PROJECT_ID = os.environ.get("BROWSERBASE_PROJECT_ID", "")
11+
if not BROWSERBASE_PROJECT_ID:
12+
raise ValueError("BROWSERBASE_PROJECT_ID is not set")
13+
14+
bb = Browserbase(
15+
# This is the default and can be omitted
16+
api_key=BROWSERBASE_API_KEY,
617
)
718

819

920
def run(playwright: Playwright) -> None:
1021
# Create a session on Browserbase
1122
session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID)
12-
assert session.id is not None
13-
assert session.status == "RUNNING", f"Session status is {session.status}"
1423

1524
# Connect to the remote session
1625
chromium = playwright.chromium

0 commit comments

Comments
 (0)