@@ -11,46 +11,71 @@ If you already have a Notte implementation, you can easily switch to using Kerne
1111### 1. Install the required SDKs
1212
1313``` bash
14- pip install notte-sdk kernel
14+ uv pip install notte-sdk kernel
1515```
1616
17- ### 2. Initialize Kernel and create a headless browser
17+ ### 2. Initialize Kernel and create a browser
1818
19- Import the libraries and create a headless cloud browser session :
19+ Import the libraries and create a cloud browser session :
2020
2121``` python
2222from kernel import Kernel
2323from notte_sdk import NotteClient
24+ import os
25+ from dotenv import load_dotenv
2426
25- # Initialize Kernel client
26- client = Kernel( api_key = " your-api-key " )
27+ # Load environment variables from .env file
28+ load_dotenv( )
2729
28- # Create a headless Kernel browser session
29- kernel_browser = client.browsers.create(headless = True )
30+ # Initialize clients
31+ kernel_api_key = os.getenv(" KERNEL_API_KEY" )
32+ notte_api_key = os.getenv(" NOTTE_API_KEY" )
33+
34+ if not kernel_api_key:
35+ raise ValueError (" KERNEL_API_KEY not found in environment variables" )
36+ if not notte_api_key:
37+ raise ValueError (" NOTTE_API_KEY not found in environment variables" )
38+
39+ kernel_client = Kernel(api_key = kernel_api_key)
40+ notte_client = NotteClient(api_key = notte_api_key)
41+
42+ # Create a headful browser on Kernel
43+ print (" Creating browser session on Kernel..." )
44+ kernel_browser = kernel_client.browsers.create(headless = False )
3045```
3146
3247### 3. Connect Notte to Kernel's CDP endpoint
3348
3449Use Kernel's CDP WebSocket URL to create a Notte session:
3550
3651``` python
37- # Initialize Notte client
38- notte = NotteClient()
39-
40- # Connect to Kernel browser via CDP
41- with notte.Session(cdp_url = kernel_browser.cdp_ws_url) as session:
42- # Create and run your agent
43- agent = notte.Agent(session = session, max_steps = 5 )
44- agent.run(task = " extract pricing plans from https://www.notte.cc/" )
52+ try :
53+ # Connect Notte to Kernel's browser via CDP
54+ print (" Connecting Notte to Kernel browser..." )
55+ with notte_client.Session(cdp_url = kernel_browser.cdp_ws_url) as session:
56+ # Create an agent with a task
57+ agent = notte_client.Agent(session = session, max_steps = 10 )
58+
59+ # Run your automation task
60+ result = agent.run(
61+ task = " extract pricing plans from https://www.notte.cc"
62+ )
63+
64+ print (f " Task completed: { result.answer} " )
65+
66+ except Exception as e:
67+ print (f " Error during automation: { e} " )
4568```
4669
4770### 4. Clean up the browser session
4871
4972After your automation completes, tear down the Kernel browser:
5073
5174``` python
52- # Clean up
53- client.browsers.delete_by_id(kernel_browser.session_id)
75+ finally :
76+ # Always clean up the browser session
77+ kernel_client.browsers.delete_by_id(kernel_browser.session_id)
78+ print (" Browser session cleaned up" )
5479```
5580
5681## Complete example script
@@ -60,34 +85,50 @@ Here's a complete, runnable script that demonstrates the full integration:
6085``` python
6186from kernel import Kernel
6287from notte_sdk import NotteClient
88+ import os
89+ from dotenv import load_dotenv
90+
91+ # Load environment variables from .env file
92+ load_dotenv()
6393
6494def main ():
6595 # Initialize clients
66- client = Kernel(api_key = " your-api-key" )
67- notte = NotteClient()
96+ kernel_api_key = os.getenv(" KERNEL_API_KEY" )
97+ notte_api_key = os.getenv(" NOTTE_API_KEY" )
98+
99+ if not kernel_api_key:
100+ raise ValueError (" KERNEL_API_KEY not found in environment variables" )
101+ if not notte_api_key:
102+ raise ValueError (" NOTTE_API_KEY not found in environment variables" )
103+
104+ kernel_client = Kernel(api_key = kernel_api_key)
105+ notte_client = NotteClient(api_key = notte_api_key)
68106
69- # Create a headless browser on Kernel
70- kernel_browser = client.browsers.create(headless = True )
107+ # Create a headful browser on Kernel
108+ print (" Creating browser session on Kernel..." )
109+ kernel_browser = kernel_client.browsers.create(headless = False )
110+ # print(kernel_browser.browser_live_view_url)
71111
72112 try :
73113 # Connect Notte to Kernel's browser via CDP
74- with notte.Session(cdp_url = kernel_browser.cdp_ws_url) as session:
114+ print (" Connecting Notte to Kernel browser..." )
115+ with notte_client.Session(cdp_url = kernel_browser.cdp_ws_url) as session:
75116 # Create an agent with a task
76- agent = notte .Agent(session = session, max_steps = 10 )
117+ agent = notte_client .Agent(session = session, max_steps = 10 )
77118
78119 # Run your automation task
79120 result = agent.run(
80- task = " Go to https://example.com and extract the main heading "
121+ task = " extract pricing plans from https://www.notte.cc "
81122 )
82123
83- print (f " Task completed: { result} " )
124+ print (f " Task completed: { result.answer } " )
84125
85126 except Exception as e:
86127 print (f " Error during automation: { e} " )
87128
88129 finally :
89130 # Always clean up the browser session
90- client .browsers.delete_by_id(kernel_browser.session_id)
131+ kernel_client .browsers.delete_by_id(kernel_browser.session_id)
91132 print (" Browser session cleaned up" )
92133
93134if __name__ == " __main__" :
0 commit comments