{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d10bfca4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copyright 2025 Google LLC\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     https://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acca43ae",
   "metadata": {},
   "source": [
    "# Demo to Show Anywidget mode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ca22f059",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bigframes.pandas as bpd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04406a4d",
   "metadata": {},
   "source": [
    "This notebook demonstrates the anywidget display mode, which provides an interactive table experience.\n",
    "Key features include:\n",
    "- **Column Sorting:** Click on column headers to sort data in ascending, descending, or unsorted states.\n",
    "- **Adjustable Column Widths:** Drag the dividers between column headers to resize columns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1bc5aaf3",
   "metadata": {},
   "outputs": [],
   "source": [
    "bpd.options.bigquery.ordering_mode = \"partial\"\n",
    "bpd.options.display.repr_mode = \"anywidget\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a354c69",
   "metadata": {},
   "source": [
    "Load Sample Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f289d250",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "✅ Completed. "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "✅ Completed. \n",
       "    Query processed 0 Bytes in a moment of slot time.\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "state gender  year     name  number\n",
      "   AL      F  1910     Vera      71\n",
      "   AR      F  1910    Viola      37\n",
      "   AR      F  1910    Alice      57\n",
      "   AR      F  1910     Edna      95\n",
      "   AR      F  1910    Ollie      40\n",
      "   CA      F  1910 Beatrice      37\n",
      "   CT      F  1910   Marion      36\n",
      "   CT      F  1910    Marie      36\n",
      "   FL      F  1910    Alice      53\n",
      "   GA      F  1910   Thelma     133\n",
      "...\n",
      "\n",
      "[5552452 rows x 5 columns]\n"
     ]
    }
   ],
   "source": [
    "df = bpd.read_gbq(\"bigquery-public-data.usa_names.usa_1910_2013\")\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a73e472",
   "metadata": {},
   "source": [
    "Display Series in anywidget mode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "42bb02ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Computation deferred. Computation will process 44.4 MB\n"
     ]
    }
   ],
   "source": [
    "test_series = df[\"year\"]\n",
    "print(test_series)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bcf1bb7",
   "metadata": {},
   "source": [
    "Display with Pagination"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ce250157",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "✅ Completed. "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "✅ Completed. "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2aad385a8a2f411c822dafe7b07fbad8",
       "version_major": 2,
       "version_minor": 1
      },
      "text/html": [
       "<table border=\"1\" class=\"dataframe table table-striped table-hover\" id=\"table-4036e4dc-c2f5-4f7a-8e53-8b31df4a8e13\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: left;\">\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">state</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">gender</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">year</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">name</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">number</div></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        AL\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        F\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        1910\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Cora\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        61\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        AL\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        F\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        1910\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Anna\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        74\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        AR\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        F\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        1910\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Willie\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        132\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        CO\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        F\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        1910\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Anna\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        42\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        FL\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        F\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        1910\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Louise\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        70\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        GA\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        F\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        1910\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Catherine\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        57\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        IL\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        F\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        1910\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Jessie\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        43\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        IN\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        F\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        1910\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Anna\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        100\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        IN\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        F\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        1910\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Pauline\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        77\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        IN\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        F\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        1910\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Beulah\n",
       "      </td>\n",
       "      <td style=\"text-align: right; padding: 0.5em;\">\n",
       "        39\n",
       "      </td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "state gender  year      name  number\n",
       "   AL      F  1910      Cora      61\n",
       "   AL      F  1910      Anna      74\n",
       "   AR      F  1910    Willie     132\n",
       "   CO      F  1910      Anna      42\n",
       "   FL      F  1910    Louise      70\n",
       "   GA      F  1910 Catherine      57\n",
       "   IL      F  1910    Jessie      43\n",
       "   IN      F  1910      Anna     100\n",
       "   IN      F  1910   Pauline      77\n",
       "   IN      F  1910    Beulah      39\n",
       "...\n",
       "\n",
       "[5552452 rows x 5 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sorting-intro",
   "metadata": {},
   "source": [
    "### Sorting by Single-Column\n",
    "You can sort the table by clicking on the headers of columns that have orderable data types (like numbers, strings, and dates). Non-orderable columns (like arrays or structs) do not have sorting controls.\n",
    "\n",
    "**Sorting indicators (▲, ▼) are always visible for sorted columns. The unsorted indicator (●) is only visible when you hover over an unsorted column header.** The sorting control cycles through three states:\n",
    "- **Unsorted (no indicator by default, ● on hover):** The default state. Click the header to sort in ascending order.\n",
    "- **Ascending (▲):** The data is sorted from smallest to largest. Click again to sort in descending order.\n",
    "- **Descending (▼):** The data is sorted from largest to smallest. Click again to return to the unsorted state."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adjustable-width-intro",
   "metadata": {},
   "source": [
    "### Adjustable Column Widths\n",
    "You can easily adjust the width of any column in the table. Simply hover your mouse over the vertical dividers between column headers. When the cursor changes to a resize icon, click and drag to expand or shrink the column to your desired width. This allows for better readability and customization of your table view."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb15bab6",
   "metadata": {},
   "source": [
    "Programmatic Navigation Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6920d49b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "✅ Completed. "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "✅ Completed. "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total pages: 555246\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0c0b83e7e3c048ff8abb525e1bfd6c5f",
       "version_major": 2,
       "version_minor": 1
      },
      "text/plain": [
       "TableWidget(orderable_columns=['state', 'gender', 'year', 'name', 'number'], page_size=10, row_count=5552452, …"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from bigframes.display.anywidget import TableWidget\n",
    "import math\n",
    "  \n",
    "# Create widget programmatically  \n",
    "widget = TableWidget(df)\n",
    "print(f\"Total pages: {math.ceil(widget.row_count / widget.page_size)}\")\n",
    "  \n",
    "# Display the widget\n",
    "widget"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02cbd1be",
   "metadata": {},
   "source": [
    "Test Navigation Programmatically"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "12b68f15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Current page: 0\n",
      "After next: 1\n",
      "After prev: 0\n"
     ]
    }
   ],
   "source": [
    "# Simulate button clicks programmatically\n",
    "print(\"Current page:\", widget.page)\n",
    "\n",
    "# Go to next page\n",
    "widget.page = 1\n",
    "print(\"After next:\", widget.page)\n",
    "\n",
    "# Go to previous page\n",
    "widget.page = 0\n",
    "print(\"After prev:\", widget.page)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d310138",
   "metadata": {},
   "source": [
    "Edge Case Demonstration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a9d5d13a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "✅ Completed. \n",
       "    Query processed 171.4 MB in a moment of slot time.\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "✅ Completed. \n",
       "    Query processed 0 Bytes in a moment of slot time.\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Small dataset pages: 1\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6a60e5dd37c64e76a8e3804dd3531f70",
       "version_major": 2,
       "version_minor": 1
      },
      "text/plain": [
       "TableWidget(orderable_columns=['state', 'gender', 'year', 'name', 'number'], page_size=10, row_count=5, table_…"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test with very small dataset\n",
    "small_df = df.sort_values([\"name\", \"year\", \"state\"]).head(5)\n",
    "small_widget = TableWidget(small_df)\n",
    "print(f\"Small dataset pages: {math.ceil(small_widget.row_count / small_widget.page_size)}\")\n",
    "small_widget"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "added-cell-2",
   "metadata": {},
   "source": [
    "### Displaying Generative AI results containing JSON\n",
    "The `AI.GENERATE` function in BigQuery returns results in a JSON column. While BigQuery's JSON type is not natively supported by the underlying Arrow `to_pandas_batches()` method used in anywidget mode ([Apache Arrow issue #45262](https://github.com/apache/arrow/issues/45262)), BigQuery Dataframes automatically converts JSON columns to strings for display. This allows you to view the results of generative AI functions seamlessly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "added-cell-1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "✅ Completed. \n",
       "    Query processed 85.9 kB in 14 seconds of slot time.\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/google/home/shuowei/src/python-bigquery-dataframes/bigframes/dtypes.py:987: JSONDtypeWarning: JSON columns will be represented as pandas.ArrowDtype(pyarrow.json_())\n",
      "instead of using `db_dtypes` in the future when available in pandas\n",
      "(https://github.com/pandas-dev/pandas/issues/60958) and pyarrow.\n",
      "  warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "✅ Completed. "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "✅ Completed. "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/google/home/shuowei/src/python-bigquery-dataframes/bigframes/dtypes.py:987: JSONDtypeWarning: JSON columns will be represented as pandas.ArrowDtype(pyarrow.json_())\n",
      "instead of using `db_dtypes` in the future when available in pandas\n",
      "(https://github.com/pandas-dev/pandas/issues/60958) and pyarrow.\n",
      "  warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "893065f8a0164648b241f2cc3d1a9271",
       "version_major": 2,
       "version_minor": 1
      },
      "text/html": [
       "<table border=\"1\" class=\"dataframe table table-striped table-hover\" id=\"table-531b95b3-561a-4b18-a203-89c867b7bddc\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: left;\">\n",
       "      <th style=\"text-align: left;\" ><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">result</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">gcs_path</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">issuer</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">language</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">publication_date</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">class_international</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">class_us</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">application_number</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">filing_date</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">priority_date_eu</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">representative_line_1_eu</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">applicant_line_1</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">inventor_line_1</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">title_line_1</div></th>\n",
       "      <th style=\"text-align: left;\" class=\"sortable\"><div style=\"resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;\">number</div></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        {&#x27;application_number&#x27;: None, &#x27;class_international&#x27;: None, &#x27;filing_date&#x27;: None, &#x27;publication_date&#x27;: None, &#x27;full_response&#x27;: &#x27;{}&#x27;, &#x27;status&#x27;: &#x27;INVALID_ARGUMENT: Invalid field in objectref details, only a JSON object named gcs_metadata is allowed [type.googleapis.com/util.MessageSetPayload=\\&#x27;[dremel.DremelErrorWithDetails] { argument_error { query_error { } } debug_info { error_message_template: &quot;Invalid field in objectref details, only a JSON object named $0 is allowed&quot; error_id: 3270173750 } }\\&#x27;]&#x27;}\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        gs://gcs-public-data--labeled-patents/espacenet_de73.pdf\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        EU\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        DE\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        03.10.2018\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        H05B 6/12\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        <em style=\"color: gray;\">&lt;NA&gt;</em>\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        18165514.3\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        03.04.2018\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        30.03.2017\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        <em style=\"color: gray;\">&lt;NA&gt;</em>\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        BSH Hausger√§te GmbH\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Acero Acero, Jesus\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        VORRICHTUNG ZUR INDUKTIVEN ENERGIE√úBERTRAGUNG\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        EP 3 383 141 A2\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        {&#x27;application_number&#x27;: None, &#x27;class_international&#x27;: None, &#x27;filing_date&#x27;: None, &#x27;publication_date&#x27;: None, &#x27;full_response&#x27;: &#x27;{}&#x27;, &#x27;status&#x27;: &#x27;INVALID_ARGUMENT: Invalid field in objectref details, only a JSON object named gcs_metadata is allowed [type.googleapis.com/util.MessageSetPayload=\\&#x27;[dremel.DremelErrorWithDetails] { argument_error { query_error { } } debug_info { error_message_template: &quot;Invalid field in objectref details, only a JSON object named $0 is allowed&quot; error_id: 3270173750 } }\\&#x27;]&#x27;}\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        gs://gcs-public-data--labeled-patents/espacenet_de2.pdf\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        EU\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        DE\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        29.08.018\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        E04H 6/12\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        <em style=\"color: gray;\">&lt;NA&gt;</em>\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        18157874.1\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        21.02.2018\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        22.02.2017\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Liedtke &amp; Partner Patentanw√§lte\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        SHB Hebezeugbau GmbH\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        VOLGER, Alexander\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        STEUERUNGSSYSTEM F√úR AUTOMATISCHE PARKH√ÑUSER\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        EP 3 366 869 A1\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        {&#x27;application_number&#x27;: None, &#x27;class_international&#x27;: None, &#x27;filing_date&#x27;: None, &#x27;publication_date&#x27;: None, &#x27;full_response&#x27;: &#x27;{}&#x27;, &#x27;status&#x27;: &#x27;INVALID_ARGUMENT: Invalid field in objectref details, only a JSON object named gcs_metadata is allowed [type.googleapis.com/util.MessageSetPayload=\\&#x27;[dremel.DremelErrorWithDetails] { argument_error { query_error { } } debug_info { error_message_template: &quot;Invalid field in objectref details, only a JSON object named $0 is allowed&quot; error_id: 3270173750 } }\\&#x27;]&#x27;}\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        gs://gcs-public-data--labeled-patents/espacenet_de70.pdf\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        EU\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        DE\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        03.10.2018\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        H01L 21/20\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        <em style=\"color: gray;\">&lt;NA&gt;</em>\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        18166536.5\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        16.02.2016\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        <em style=\"color: gray;\">&lt;NA&gt;</em>\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Scheider, Sascha et al\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        EV Group E. Thallner GmbH\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Kurz, Florian\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        VORRICHTUNG ZUM BONDEN VON SUBSTRATEN\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        EP 3 382 744 A1\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        {&#x27;application_number&#x27;: None, &#x27;class_international&#x27;: None, &#x27;filing_date&#x27;: None, &#x27;publication_date&#x27;: None, &#x27;full_response&#x27;: &#x27;{}&#x27;, &#x27;status&#x27;: &#x27;INVALID_ARGUMENT: Invalid field in objectref details, only a JSON object named gcs_metadata is allowed [type.googleapis.com/util.MessageSetPayload=\\&#x27;[dremel.DremelErrorWithDetails] { argument_error { query_error { } } debug_info { error_message_template: &quot;Invalid field in objectref details, only a JSON object named $0 is allowed&quot; error_id: 3270173750 } }\\&#x27;]&#x27;}\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        gs://gcs-public-data--labeled-patents/espacenet_de5.pdf\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        EU\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        DE\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        03.10.2018\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        G06F 11/30\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        <em style=\"color: gray;\">&lt;NA&gt;</em>\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        18157347.8\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        19.02.2018\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        31.03.2017\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Hoffmann Eitle\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        FUJITSU LIMITED\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Kukihara, Kensuke\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        METHOD EXECUTED BY A COMPUTER, INFORMATION PROCESSING APPARATUS AND\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        EP 3 382 553 A1\n",
       "      </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        {&#x27;application_number&#x27;: None, &#x27;class_international&#x27;: None, &#x27;filing_date&#x27;: None, &#x27;publication_date&#x27;: None, &#x27;full_response&#x27;: &#x27;{}&#x27;, &#x27;status&#x27;: &#x27;INVALID_ARGUMENT: Invalid field in objectref details, only a JSON object named gcs_metadata is allowed [type.googleapis.com/util.MessageSetPayload=\\&#x27;[dremel.DremelErrorWithDetails] { argument_error { query_error { } } debug_info { error_message_template: &quot;Invalid field in objectref details, only a JSON object named $0 is allowed&quot; error_id: 3270173750 } }\\&#x27;]&#x27;}\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        gs://gcs-public-data--labeled-patents/espacenet_de56.pdf\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        EU\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        DE\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        03.10.2018\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        A01K 31/00\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        <em style=\"color: gray;\">&lt;NA&gt;</em>\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        18171005.4\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        05.02.2015\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        05.02.2014\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Stork Bamberger Patentanw√§lte\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Linco Food Systems A/S\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        Thrane, Uffe\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        MASTH√ÑHNCHENCONTAINER ALS BESTANDTEIL EINER EINHEIT UND EINER ANORDNUNG\n",
       "      </td>\n",
       "      <td style=\"text-align: left; padding: 0.5em;\">\n",
       "        EP 3 381 276 A1\n",
       "      </td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "                                              result  \\\n",
       "0  {'application_number': None, 'class_internatio...   \n",
       "1  {'application_number': None, 'class_internatio...   \n",
       "2  {'application_number': None, 'class_internatio...   \n",
       "3  {'application_number': None, 'class_internatio...   \n",
       "4  {'application_number': None, 'class_internatio...   \n",
       "\n",
       "                                            gcs_path issuer language  \\\n",
       "0  gs://gcs-public-data--labeled-patents/espacene...     EU       DE   \n",
       "1  gs://gcs-public-data--labeled-patents/espacene...     EU       DE   \n",
       "2  gs://gcs-public-data--labeled-patents/espacene...     EU       DE   \n",
       "3  gs://gcs-public-data--labeled-patents/espacene...     EU       DE   \n",
       "4  gs://gcs-public-data--labeled-patents/espacene...     EU       DE   \n",
       "\n",
       "  publication_date class_international class_us application_number  \\\n",
       "0       03.10.2018           H05B 6/12     <NA>         18165514.3   \n",
       "1        29.08.018           E04H 6/12     <NA>         18157874.1   \n",
       "2       03.10.2018          H01L 21/20     <NA>         18166536.5   \n",
       "3       03.10.2018          G06F 11/30     <NA>         18157347.8   \n",
       "4       03.10.2018          A01K 31/00     <NA>         18171005.4   \n",
       "\n",
       "  filing_date priority_date_eu          representative_line_1_eu  \\\n",
       "0  03.04.2018       30.03.2017                              <NA>   \n",
       "1  21.02.2018       22.02.2017  Liedtke & Partner Patentanw√§lte   \n",
       "2  16.02.2016             <NA>            Scheider, Sascha et al   \n",
       "3  19.02.2018       31.03.2017                    Hoffmann Eitle   \n",
       "4  05.02.2015       05.02.2014    Stork Bamberger Patentanw√§lte   \n",
       "\n",
       "            applicant_line_1     inventor_line_1  \\\n",
       "0       BSH Hausger√§te GmbH  Acero Acero, Jesus   \n",
       "1       SHB Hebezeugbau GmbH   VOLGER, Alexander   \n",
       "2  EV Group E. Thallner GmbH       Kurz, Florian   \n",
       "3            FUJITSU LIMITED   Kukihara, Kensuke   \n",
       "4     Linco Food Systems A/S        Thrane, Uffe   \n",
       "\n",
       "                                        title_line_1           number  \n",
       "0     VORRICHTUNG ZUR INDUKTIVEN ENERGIE√úBERTRAGUNG  EP 3 383 141 A2  \n",
       "1     STEUERUNGSSYSTEM F√úR AUTOMATISCHE PARKH√ÑUSER  EP 3 366 869 A1  \n",
       "2              VORRICHTUNG ZUM BONDEN VON SUBSTRATEN  EP 3 382 744 A1  \n",
       "3  METHOD EXECUTED BY A COMPUTER, INFORMATION PRO...  EP 3 382 553 A1  \n",
       "4  MASTH√ÑHNCHENCONTAINER ALS BESTANDTEIL EINER E...  EP 3 381 276 A1  \n",
       "\n",
       "[5 rows x 15 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bpd._read_gbq_colab(\"\"\"\n",
    "  SELECT\n",
    "    AI.GENERATE(\n",
    "      prompt=>(\\\"Extract the values.\\\", OBJ.GET_ACCESS_URL(OBJ.FETCH_METADATA(OBJ.MAKE_REF(gcs_path, \\\"us.conn\\\")), \\\"r\\\")),\n",
    "      connection_id=>\\\"bigframes-dev.us.bigframes-default-connection\\\",\n",
    "      output_schema=>\\\"publication_date string, class_international string, application_number string, filing_date string\\\") AS result,\n",
    "    *\n",
    "  FROM `bigquery-public-data.labeled_patents.extracted_data`\n",
    "  LIMIT 5;\n",
    "\"\"\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
