another-bigquery-magic 0.1.5
Unofficial IPython magic command for bigquery
Requires Python: >=3.7

# another-bigquery-magic
Unofficial bigquery magic command for IPython notebook

[![](https://badge.fury.io/py/another-bigquery-magic.svg)](https://badge.fury.io/py/another-bigquery-magic)

## Installation

```python
# from pypi
$ pip install another-bigquery-magic

# alternatively, from github
$ git clone https://github.com/kota7/another-bigquery-magic.git --depth 1
$ pip install -U ./another-bigquery-magic
```

## Usage


```python
# Set the project ID
project_id = "<google-cloud-project-id>"
!gcloud config set project {project_id}
```


```python
# If you are authenticated to the google cloud already, skip this cell.
# Otherwise, authenticate with your choice of method.

# Example 1. Authentication on colab
from google.colab import auth
auth.authenticate_user()

# Example 2. Authentication by user log-in
# Note: to access external table with google drive,
#       we also need "https://www.googleapis.com/auth/drive" in the scope
!gcloud auth application-default login --scopes="https://www.googleapis.com/auth/bigquery"

# Example 3. Authentication with a local json file
jsonfile = "<json-file-path>"
%config BigqueryMagic.localjson = jsonfile
```


```python
# Load the bq magic command
%load_ext bq

# %bq magic command runs the query and returns the pandas data frame
%bq SELECT 1 AS test
```

    Start query at 2024-01-12 15:31:07.286991
    End query at 2024-01-12 15:31:10.047083 (Execution time: 0:00:02.760092, Processed: 0.0 GB)


<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
    </tr>
  </tbody>
</table>
</div>



```python
# Use polars as the output type
q = "SELECT 1 AS n, 3.14 AS pi, 'test' AS mode"

%config BigqueryMagic.usepolars = True
x = %bq {q}
display(x)
print(type(x))

# Reset to pandas
%config BigqueryMagic.usepolars = False
x = %bq {q}
display(x)
print(type(x))
```

    Start query at 2025-07-29 12:45:32.515976
    End query at 2025-07-29 12:45:34.439488 (Execution time: 0:00:01.923512, Processed: 0.0 GB)



<div><style>
.dataframe > thead > tr,
.dataframe > tbody > tr {
  text-align: right;
  white-space: pre-wrap;
}
</style>
<small>shape: (1, 3)</small><table border="1" class="dataframe"><thead><tr><th>n</th><th>pi</th><th>mode</th></tr><tr><td>i64</td><td>f64</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>3.14</td><td>&quot;test&quot;</td></tr></tbody></table></div>


    <class 'polars.dataframe.frame.DataFrame'>


    Start query at 2025-07-29 12:45:35.458804
    End query at 2025-07-29 12:45:37.287104 (Execution time: 0:00:01.828300, Processed: 0.0 GB)



<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>n</th>
      <th>pi</th>
      <th>mode</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>3.14</td>
      <td>test</td>
    </tr>
  </tbody>
</table>
</div>


    <class 'pandas.core.frame.DataFrame'>


See [example.ipynb](./example.ipynb) for more examples.