Skip to content
This repository was archived by the owner on May 7, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions bigframes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""BigQuery DataFrames provides a DataFrame API scaled by the BigQuery engine."""

import IPython

from bigframes._config import option_context, options
from bigframes._config.bigquery_options import BigQueryOptions
from bigframes.core.global_session import close_session, get_global_session
Expand All @@ -22,6 +24,20 @@
from bigframes.session import connect, Session
from bigframes.version import __version__


def load_ipython_extension(ipython):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding an unload function that undoes any bigframes-specific configuration of %%bigquery

See: unload_ipython_extension in https://ipython.readthedocs.io/en/stable/config/extensions/#writing-extensions

"""Called by IPython when this module is loaded as an IPython extension."""
# Import here to avoid circular imports.
import bigframes.magics

if ipython is not None:
bigframes.magics.load_ipython_extension(ipython)


# Automatically load the extension, to make the magics easier to discover.
load_ipython_extension(IPython.get_ipython())


__all__ = [
"options",
"BigQueryOptions",
Expand Down
39 changes: 39 additions & 0 deletions bigframes/magics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import bigquery_magics # type: ignore
import bigquery_magics.config # type: ignore


def load_ipython_extension(ipython):
"""Called by IPython when this module is loaded as an IPython extension."""
bigquery_magics.load_ipython_extension(ipython)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, check if it is registered already before calling this and break early.

https://github.com/googleapis/python-bigquery-magics/blob/cbb3e44fcdb6aec7cb04d0460a34501f0fd6d271/bigquery_magics/__init__.py#L22


if bigquery_magics.context.credentials is not None:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pandas-gbq sets the default credentials in-memory after the first run, but it doesn't look like the magics have any such feature yet.

TODO: bigquery-magics to cache the credentials and/or client objects after the first.

# The %%bigquery magics must have been run before BigQuery DataFrames
# was imported. In this case, we don't want to break any existing
# notebooks, so don't make any BigQuery DataFrames changes to the
# magics.
return

bigquery_magics.context = Context()
Comment on lines +23 to +30
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DELETE ME

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, just delete this last part. Set the engine and default variable if we make it this far (e.g. the magics haven't been used yet)



class Context(bigquery_magics.config.Context):
"""A provider for bigquery-magics configuration, derived from bigframes
global options and global default session.
"""

def __init__(self):
pass
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
release_status = "Development Status :: 5 - Production/Stable"
dependencies = [
# please keep these in sync with the minimum versions in testing/constraints-3.9.txt
"bigquery-magics >= 0.5.0",
"cloudpickle >= 2.0.0",
"fsspec >=2023.3.0",
"gcsfs >=2023.3.0",
Expand Down
2 changes: 2 additions & 0 deletions testing/constraints-3.11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# IMPORTANT: When Python 3.10 support is dropped, update these to
# match the minimums in setup.py.
2 changes: 2 additions & 0 deletions testing/constraints-3.12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# IMPORTANT: When Python 3.11 support is dropped, update these to
# match the minimums in setup.py.
2 changes: 2 additions & 0 deletions testing/constraints-3.13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# IMPORTANT: When Python 3.12 support is dropped, update these to
# match the minimums in setup.py.
1 change: 1 addition & 0 deletions testing/constraints-3.9.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# please keep these in sync with the minimum versions in setup.py
bigquery-magics==0.5.0
cloudpickle==2.0.0
fsspec==2023.3.0
gcsfs==2023.3.0
Expand Down