-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add code samples for Jupyter/IPython magics (#1013)
Follow-up to GoogleCloudPlatform/python-docs-samples#6889, which removed a BigQuery magics sample for using query parameters. Note: jupyter_tutorial_test.py is a copy of what is in the `samples/snippets` folder. Once the docs have been updated to point to this new version, we can remove that copy and remove the Jupyter/IPython depedencencies from `samples/snippets/requirements.txt`. Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary)
- Loading branch information
Showing
13 changed files
with
503 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,34 @@ | ||
IPython Magics for BigQuery | ||
=========================== | ||
|
||
To use these magics, you must first register them. Run the ``%load_ext`` magic | ||
in a Jupyter notebook cell. | ||
|
||
.. code:: | ||
%load_ext google.cloud.bigquery | ||
This makes the ``%%bigquery`` magic available. | ||
|
||
Code Samples | ||
------------ | ||
|
||
Running a query: | ||
|
||
.. literalinclude:: ./samples/magics/query.py | ||
:dedent: 4 | ||
:start-after: [START bigquery_jupyter_query] | ||
:end-before: [END bigquery_jupyter_query] | ||
|
||
Running a parameterized query: | ||
|
||
.. literalinclude:: ./samples/magics/query_params_scalars.py | ||
:dedent: 4 | ||
:start-after: [START bigquery_jupyter_query_params_scalars] | ||
:end-before: [END bigquery_jupyter_query_params_scalars] | ||
|
||
API Reference | ||
------------- | ||
|
||
.. automodule:: google.cloud.bigquery.magics.magics | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2021 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 | ||
# | ||
# https://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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright 2021 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 | ||
# | ||
# https://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. | ||
|
||
|
||
def strip_region_tags(sample_text): | ||
"""Remove blank lines and region tags from sample text""" | ||
magic_lines = [ | ||
line for line in sample_text.split("\n") if len(line) > 0 and "# [" not in line | ||
] | ||
return "\n".join(magic_lines) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright 2021 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 | ||
# | ||
# https://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 pytest | ||
|
||
interactiveshell = pytest.importorskip("IPython.terminal.interactiveshell") | ||
tools = pytest.importorskip("IPython.testing.tools") | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def ipython(): | ||
config = tools.default_config() | ||
config.TerminalInteractiveShell.simple_prompt = True | ||
shell = interactiveshell.TerminalInteractiveShell.instance(config=config) | ||
return shell | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def ipython_interactive(ipython): | ||
"""Activate IPython's builtin hooks | ||
for the duration of the test scope. | ||
""" | ||
with ipython.builtin_trap: | ||
yield ipython |
Oops, something went wrong.