create-browser-app 0.1.2__py3-none-any.whl → 0.1.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ from .main import main
2
+
3
+ __all__ = ["main"]
@@ -9,112 +9,14 @@ from rich.prompt import Prompt, Confirm
9
9
  from rich.panel import Panel
10
10
  from rich.text import Text
11
11
  import shutil
12
+ from .templates import TEMPLATE_BASIC, TEMPLATE_REQUIREMENTS, TEMPLATE_ENV, TEMPLATE_README, TEMPLATE_GITIGNORE
13
+ from .template_fetcher import get_template_by_name, fetch_template_content
12
14
 
13
15
  console = Console()
14
16
 
15
- TEMPLATE_BASIC = '''from stagehand import Stagehand
16
- import asyncio
17
-
18
- async def main():
19
- """Basic Stagehand browser automation example."""
20
- async with Stagehand() as stagehand:
21
- page = stagehand.page
22
-
23
- # Navigate to a website
24
- await page.goto("https://example.com")
25
-
26
- # Your automation code here
27
- print(f"Page title: {await page.title()}")
28
-
29
- if __name__ == "__main__":
30
- asyncio.run(main())
31
- '''
32
-
33
- TEMPLATE_REQUIREMENTS = '''stagehand
34
- python-dotenv
35
- '''
36
-
37
- TEMPLATE_ENV = '''# Add your environment variables here
38
- # BROWSERBASE_API_KEY=your_api_key_here
39
- # BROWSERBASE_PROJECT_ID=your_project_id_here
40
- '''
41
-
42
- TEMPLATE_README = '''# {project_name}
43
-
44
- A Stagehand browser automation project.
45
-
46
- ## Setup
47
-
48
- 1. Install dependencies:
49
- ```bash
50
- pip install -r requirements.txt
51
- ```
52
-
53
- 2. Configure your environment variables in `.env`:
54
- ```bash
55
- BROWSERBASE_API_KEY=your_api_key_here
56
- BROWSERBASE_PROJECT_ID=your_project_id_here
57
- ```
58
-
59
- 3. Run the project:
60
- ```bash
61
- python main.py
62
- ```
63
-
64
- ## About Stagehand
65
-
66
- Stagehand is a Python library for browser automation built on Playwright. Learn more at:
67
- - [Stagehand Documentation](https://github.com/browserbase/stagehand)
68
- - [Browserbase](https://browserbase.com)
69
- '''
70
-
71
- TEMPLATE_GITIGNORE = '''# Python
72
- __pycache__/
73
- *.py[cod]
74
- *$py.class
75
- *.so
76
- .Python
77
- build/
78
- develop-eggs/
79
- dist/
80
- downloads/
81
- eggs/
82
- .eggs/
83
- lib/
84
- lib64/
85
- parts/
86
- sdist/
87
- var/
88
- wheels/
89
- *.egg-info/
90
- .installed.cfg
91
- *.egg
92
-
93
- # Virtual Environment
94
- venv/
95
- ENV/
96
- env/
97
- .venv
98
-
99
- # Environment variables
100
- .env
101
- .env.local
102
-
103
- # IDE
104
- .vscode/
105
- .idea/
106
- *.swp
107
- *.swo
108
- *~
109
- .DS_Store
110
-
111
- # Logs
112
- *.log
113
- '''
114
-
115
17
  @click.command()
116
18
  @click.argument('name', default='my-stagehand-app', required=False)
117
- @click.option('--template', '-t', default='basic', help='Template to get started with')
19
+ @click.option('--template', '-t', default='basic', help='Template to use (basic or GitHub examples: quickstart, example, agent-example)')
118
20
  def main(name, template):
119
21
  """Start your Stagehand project with a single command"""
120
22
 
@@ -152,9 +54,26 @@ def main(name, template):
152
54
  # Create directories
153
55
  project_path.mkdir(parents=True, exist_ok=True)
154
56
 
57
+ # Determine which template to use
58
+ template_content = TEMPLATE_BASIC
59
+
60
+ # If not using basic template, try to fetch from GitHub
61
+ if template != 'basic':
62
+ console.print(f"Fetching template [cyan]{template}[/cyan] from GitHub...")
63
+ template_info = get_template_by_name(template)
64
+ if template_info:
65
+ fetched_content = fetch_template_content(template_info)
66
+ if fetched_content:
67
+ template_content = fetched_content
68
+ console.print(f"[green]✓[/green] Using template from GitHub: {template}")
69
+ else:
70
+ console.print(f"[yellow]⚠[/yellow] Could not fetch template, using basic template")
71
+ else:
72
+ console.print(f"[yellow]⚠[/yellow] Template '{template}' not found, using basic template")
73
+
155
74
  # Create main.py
156
75
  main_file = project_path / "main.py"
157
- main_file.write_text(TEMPLATE_BASIC)
76
+ main_file.write_text(template_content)
158
77
 
159
78
  # Create requirements.txt
160
79
  requirements_file = project_path / "requirements.txt"
@@ -178,10 +97,9 @@ def main(name, template):
178
97
  # Styled next steps
179
98
  console.print(Panel(
180
99
  f"[bold cyan]1.[/bold cyan] cd {project_dir}\n"
181
- f"[bold cyan]2.[/bold cyan] pip install -r requirements.txt\n"
182
- f"[bold cyan]3.[/bold cyan] cp .env.example .env\n"
183
- f"[bold cyan]4.[/bold cyan] [dim]Add your Browserbase API key to .env[/dim]\n"
184
- f"[bold cyan]5.[/bold cyan] python main.py",
100
+ f"[bold cyan]2.[/bold cyan] cp .env.example .env\n"
101
+ f"[bold cyan]3.[/bold cyan] [dim]Add your Browserbase API key to .env[/dim]\n"
102
+ f"[bold cyan]4.[/bold cyan] python main.py",
185
103
  title="[bold yellow]🚀 Launch now 🚀[/bold yellow]",
186
104
  border_style="yellow",
187
105
  padding=(1, 2)
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env python3
2
+ import urllib.request
3
+ import json
4
+ from typing import Dict, List, Optional
5
+ from pathlib import Path
6
+ import base64
7
+
8
+ GITHUB_API_BASE = "https://api.github.com"
9
+ REPO_OWNER = "browserbase"
10
+ REPO_NAME = "stagehand-python"
11
+ EXAMPLES_PATH = "examples"
12
+
13
+ def get_available_templates() -> List[Dict[str, str]]:
14
+ """Fetch list of available templates from GitHub repository."""
15
+ try:
16
+ url = f"{GITHUB_API_BASE}/repos/{REPO_OWNER}/{REPO_NAME}/contents/{EXAMPLES_PATH}"
17
+ req = urllib.request.Request(url)
18
+ req.add_header("Accept", "application/vnd.github.v3+json")
19
+ req.add_header("User-Agent", "create-browser-app-py")
20
+
21
+ with urllib.request.urlopen(req) as response:
22
+ data = json.loads(response.read().decode())
23
+
24
+ templates = []
25
+ for item in data:
26
+ if item["type"] == "file" and item["name"].endswith(".py"):
27
+ # Keep the original name without replacing underscores
28
+ template_name = item["name"].replace(".py", "")
29
+ templates.append({
30
+ "name": template_name,
31
+ "filename": item["name"],
32
+ "url": item["download_url"],
33
+ "api_url": item["url"]
34
+ })
35
+
36
+ return templates
37
+ except Exception as e:
38
+ print(f"Error fetching templates: {e}")
39
+ return []
40
+
41
+ def fetch_template_content(template_info: Dict[str, str]) -> Optional[str]:
42
+ """Fetch the content of a specific template from GitHub."""
43
+ try:
44
+ url = template_info.get("api_url", template_info.get("url"))
45
+ req = urllib.request.Request(url)
46
+ req.add_header("Accept", "application/vnd.github.v3+json")
47
+ req.add_header("User-Agent", "create-browser-app-py")
48
+
49
+ with urllib.request.urlopen(req) as response:
50
+ data = json.loads(response.read().decode())
51
+
52
+ if "content" in data:
53
+ content = base64.b64decode(data["content"]).decode("utf-8")
54
+ else:
55
+ # Fallback to direct download
56
+ with urllib.request.urlopen(template_info["url"]) as response:
57
+ content = response.read().decode("utf-8")
58
+
59
+ return content
60
+ except Exception as e:
61
+ print(f"Error fetching template content: {e}")
62
+ return None
63
+
64
+ def get_template_by_name(name: str) -> Optional[Dict[str, str]]:
65
+ """Get a specific template by name."""
66
+ templates = get_available_templates()
67
+ for template in templates:
68
+ if template["name"] == name:
69
+ return template
70
+ return None
71
+
72
+ def list_templates() -> List[str]:
73
+ """Get a list of template names."""
74
+ templates = get_available_templates()
75
+ return [t["name"] for t in templates]
@@ -0,0 +1,131 @@
1
+ TEMPLATE_BASIC = '''from stagehand import Stagehand
2
+ import asyncio
3
+ import os
4
+ from dotenv import load_dotenv
5
+ from pydantic import BaseModel
6
+ load_dotenv()
7
+
8
+ async def main():
9
+ """Basic Stagehand browser automation example."""
10
+
11
+ # Check all the configurations in StagehandConfig
12
+ stagehand = Stagehand(
13
+ env="LOCAL", # or "BROWSERBASE"
14
+ verbose=1, # 0: silent, 1: info, 2: debug
15
+ model_name="gpt-4.1",
16
+ model_api_key=os.getenv("OPENAI_API_KEY"),
17
+ )
18
+ await stagehand.init()
19
+
20
+ page = stagehand.page
21
+ await page.goto("https://docs.stagehand.dev")
22
+ result = await page.extract("In a few words, what is Stagehand?")
23
+ print("\n", result, "\n")
24
+
25
+ await page.act("click on models")
26
+ await page.act({
27
+ "method": "click",
28
+ "selector": "/html/body[1]/div[2]/div[2]/div[3]/div[2]/div[2]/a[1]",
29
+ "description": "the model evaluation card",
30
+ "args": []
31
+ })
32
+ elements = await page.observe("find the graph with the list of most accurate models")
33
+ print("\n", elements, "\n")
34
+
35
+ class Model(BaseModel):
36
+ name: str
37
+ provider: str
38
+ accuracy: float
39
+
40
+ extraction = await page.extract(
41
+ "the most accurate model",
42
+ schema=Model
43
+ )
44
+ print("\n", extraction, "\n")
45
+
46
+ if __name__ == "__main__":
47
+ asyncio.run(main())
48
+
49
+ '''
50
+
51
+ TEMPLATE_REQUIREMENTS = '''stagehand
52
+ python-dotenv
53
+ '''
54
+
55
+ TEMPLATE_ENV = '''# Add your environment variables here
56
+ # BROWSERBASE_API_KEY=your_api_key_here
57
+ # BROWSERBASE_PROJECT_ID=your_project_id_here
58
+ '''
59
+
60
+ TEMPLATE_README = '''# {project_name}
61
+
62
+ A Stagehand browser automation project.
63
+
64
+ ## Setup
65
+
66
+ 1. Install dependencies:
67
+ ```bash
68
+ pip install -r requirements.txt
69
+ ```
70
+
71
+ 2. Configure your environment variables in `.env`:
72
+ ```bash
73
+ BROWSERBASE_API_KEY=your_api_key_here
74
+ BROWSERBASE_PROJECT_ID=your_project_id_here
75
+ ```
76
+
77
+ 3. Run the project:
78
+ ```bash
79
+ python main.py
80
+ ```
81
+
82
+ ## About Stagehand
83
+
84
+ Stagehand is a Python library for browser automation built on Playwright. Learn more at:
85
+ - [Stagehand Documentation](https://github.com/browserbase/stagehand)
86
+ - [Browserbase](https://browserbase.com)
87
+ '''
88
+
89
+ TEMPLATE_GITIGNORE = '''# Python
90
+ __pycache__/
91
+ *.py[cod]
92
+ *$py.class
93
+ *.so
94
+ .Python
95
+ build/
96
+ develop-eggs/
97
+ dist/
98
+ downloads/
99
+ eggs/
100
+ .eggs/
101
+ lib/
102
+ lib64/
103
+ parts/
104
+ sdist/
105
+ var/
106
+ wheels/
107
+ *.egg-info/
108
+ .installed.cfg
109
+ *.egg
110
+
111
+ # Virtual Environment
112
+ venv/
113
+ ENV/
114
+ env/
115
+ .venv
116
+
117
+ # Environment variables
118
+ .env
119
+ .env.local
120
+
121
+ # IDE
122
+ .vscode/
123
+ .idea/
124
+ *.swp
125
+ *.swo
126
+ *~
127
+ .DS_Store
128
+
129
+ # Logs
130
+ *.log
131
+ '''
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: create-browser-app
3
- Version: 0.1.2
3
+ Version: 0.1.4
4
4
  Summary: Start your Stagehand project with a single command
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -8,6 +8,7 @@ Requires-Dist: stagehand
8
8
  Requires-Dist: click>=8.0
9
9
  Requires-Dist: rich>=13.0
10
10
  Requires-Dist: inquirer>=3.0
11
+ Requires-Dist: requests>=2.25.0
11
12
 
12
13
  # create-browser-app (Python)
13
14
 
@@ -21,7 +22,7 @@ uvx create-browser-app
21
22
 
22
23
  ## What You Get
23
24
 
24
- -  Pre-configured Stagehand project
25
- -  Environment setup for Browserbase
26
- -  Ready-to-run example code
27
- -  All dependencies included
25
+ - Pre-configured Stagehand project
26
+ - Environment setup for Browserbase
27
+ - Ready-to-run example code
28
+ - All dependencies included
@@ -0,0 +1,9 @@
1
+ create_browser_app/__init__.py,sha256=izlkaSvrxNqPnulbHlD8VYldR618NxRQRlKQFgQn0Gk,42
2
+ create_browser_app/main.py,sha256=eY3dEQK_Qop07SCPNdkOtZKUyX9RnoBJZpmpU_Y1oQI,4789
3
+ create_browser_app/template_fetcher.py,sha256=kfNOFdJlBPjgtAuroVsD-zuXWd04wSpQHTBmYXHDJAc,2723
4
+ create_browser_app/templates.py,sha256=f2YhewO6lStBO-46mfrDXdSQVhGLY0jUgRwjbZG1UpU,2482
5
+ create_browser_app-0.1.4.dist-info/METADATA,sha256=aD65Xl5VoCh0_qRCbhjVkEV7wLnCw3BxcqonxKOr5UA,648
6
+ create_browser_app-0.1.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
7
+ create_browser_app-0.1.4.dist-info/entry_points.txt,sha256=yyAKhFZs2kraGA_ixwnRZeStL0uxWvBCMzysg8S-0VE,68
8
+ create_browser_app-0.1.4.dist-info/top_level.txt,sha256=sLW8imVtlXvOy1D5aGPBDxbMP3Yvonif7VxcLu_Izy4,19
9
+ create_browser_app-0.1.4.dist-info/RECORD,,
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ create-browser-app = create_browser_app.main:main
@@ -0,0 +1 @@
1
+ create_browser_app
@@ -1,6 +0,0 @@
1
- main.py,sha256=-f8bcxjBgscyy_036mebiBGinRgOouyA4qX1XWrbmFo,5327
2
- create_browser_app-0.1.2.dist-info/METADATA,sha256=_RG8PCnzjT2tYnp-UFSq4Fq7VgDWfw1b6WnbuTo4xzM,624
3
- create_browser_app-0.1.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
4
- create_browser_app-0.1.2.dist-info/entry_points.txt,sha256=S7VEMgVttj-lbn3gh_gPTJJpwczelIrb5fZgo5ecKHs,49
5
- create_browser_app-0.1.2.dist-info/top_level.txt,sha256=ZAMgPdWghn6xTRBO6Kc3ML1y3ZrZLnjZlqbboKXc_AE,5
6
- create_browser_app-0.1.2.dist-info/RECORD,,
@@ -1,2 +0,0 @@
1
- [console_scripts]
2
- create-browser-app = main:main
@@ -1 +0,0 @@
1
- main