resolve userdata ImportError, NameError, and unawaited new_page in Colab notebook
GITHUB ISSUE BODY:
🐛 Bug Description
When running the official Google Colab notebook at examples/python/run_script_online_in_google_colab/main.ipynb, the notebook encounters fatal crashes during execution in Cell 6 and Cell 8:
-
ImportErrorin Cell 6: Cell 6 attempts to importfrom google.colab import user_datawith an underscore. The official Google Colab SDK module name isuserdata(no underscore). This immediately raises: ImportError: cannot import name 'user_data' from 'google.colab' -
NameErrorin Cell 6: The subsequent line attempts to callos.environ["AGENTQL_API_KEY"] = userdata.get('AGENTQL_API_KEY'). Becauseuser_datawas imported instead ofuserdata, Python raises: NameError: name 'userdata' is not defined -
Unawaited Coroutine in Cell 8: Cell 8 calls
page = await agentql.wrap_async(browser.new_page()). Under Playwright Async API (playwright.async_api),browser.new_page()is an asynchronous coroutine method and must be awaited (await browser.new_page()). Passing an un-awaited coroutine towrap_asynccauses runtime type issues.
🔍 Steps to Reproduce
- Open
examples/python/run_script_online_in_google_colab/main.ipynbin Google Colab. - Run Cells 1 through 5 (installation steps).
- Execute Cell 6:
import os
from google.colab import user_data
os.environ["AGENTQL_API_KEY"] = userdata.get('AGENTQL_API_KEY')
- Observe the immediate fatal exception:
ImportError: cannot import name 'user_data' from 'google.colab' (/usr/local/lib/python3.10/dist-packages/google/colab/__init__.py)
🛠️ Proposed Solution / Code Diff
--- a/examples/python/run_script_online_in_google_colab/main.ipynb
+++ b/examples/python/run_script_online_in_google_colab/main.ipynb
@@ -48,8 +48,8 @@
"source": [
"import os\n",
"\n",
- "from google.colab import user_data\n",
+ "from google.colab import userdata\n",
"\n",
- "os.environ[\"AGENTQL_API_KEY\"]=userdata.get('AGENTQL_API_KEY')"
+ "os.environ[\"AGENTQL_API_KEY\"] = userdata.get('AGENTQL_API_KEY')"
]
},
@@ -87,7 +87,7 @@
"async with async_playwright() as playwright, await playwright.chromium.launch() as browser:\n",
" # Create a new page in the browser and wrap it to get access to the AgentQL's querying API\n",
- " page= await agentql.wrap_async(browser.new_page())\n",
+ " page = await agentql.wrap_async(await browser.new_page())\n",
"\n",
" await page.goto(URL)\n",
📄 Corrected Code for Notebook Cells:
Cell 6 (Fixed):
import os
from google.colab import userdata
os.environ["AGENTQL_API_KEY"] = userdata.get('AGENTQL_API_KEY')
Cell 8 (Fixed):
import agentql
from playwright.async_api import async_playwright
URL = "https://scrapeme.live/shop"
SEARCH_QUERY = """
{
search_products_box
}
"""
STOCK_NUMBER_QUERY = """
{
number_in_stock
}
"""
async with async_playwright() as playwright, await playwright.chromium.launch() as browser:
# Create a new page in the browser and wrap it to get access to the AgentQL's querying API
page = await agentql.wrap_async(await browser.new_page())
await page.goto(URL)
# Use query_elements() method to locate the search box and search button from the page
response = await page.query_elements(SEARCH_QUERY)
# Use Playwright's API to fill the search box and press Enter
await response.search_products_box.type("Charmander")
await page.keyboard.press("Enter")
# Use query_data() method to fetch the stock number from the page
response = await page.query_data(STOCK_NUMBER_QUERY)
print(response)
GITHUB PULL REQUEST (PR) TITLE:
fix(colab): fix userdata import typo and await browser.new_page in Colab notebook (#issue_number)
GITHUB PULL REQUEST (PR) DESCRIPTION:
Summary of Changes
- Fixed Google Colab Secret Retrieval: Corrected
from google.colab import user_datatofrom google.colab import userdatain Cell 6, fixing both theImportErrorand subsequentNameError. - Awaited Async Page Creation: Added missing
awaittobrowser.new_page()in Cell 8 when wrapping the page withagentql.wrap_async(). - Tested & Verified: Validated that the notebook executes seamlessly in Google Colab without runtime import or coroutine errors.