agentql icon indicating copy to clipboard operation
agentql copied to clipboard

resolve userdata ImportError, NameError, and unawaited new_page in Colab notebook

Open codeCraft-Ritik opened this issue 2 months ago • 0 comments

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:

  1. ImportError in Cell 6: Cell 6 attempts to import from google.colab import user_data with an underscore. The official Google Colab SDK module name is userdata (no underscore). This immediately raises: ImportError: cannot import name 'user_data' from 'google.colab'

  2. NameError in Cell 6: The subsequent line attempts to call os.environ["AGENTQL_API_KEY"] = userdata.get('AGENTQL_API_KEY'). Because user_data was imported instead of userdata, Python raises: NameError: name 'userdata' is not defined

  3. 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 to wrap_async causes runtime type issues.


🔍 Steps to Reproduce

  1. Open examples/python/run_script_online_in_google_colab/main.ipynb in Google Colab.
  2. Run Cells 1 through 5 (installation steps).
  3. Execute Cell 6:
import os
from google.colab import user_data
os.environ["AGENTQL_API_KEY"] = userdata.get('AGENTQL_API_KEY')
  1. 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_data to from google.colab import userdata in Cell 6, fixing both the ImportError and subsequent NameError.
  • Awaited Async Page Creation: Added missing await to browser.new_page() in Cell 8 when wrapping the page with agentql.wrap_async().
  • Tested & Verified: Validated that the notebook executes seamlessly in Google Colab without runtime import or coroutine errors.

codeCraft-Ritik avatar Sep 07 '26 04:09 codeCraft-Ritik