|
14 | 14 |
|
15 | 15 | """IPython Magics
|
16 | 16 |
|
17 |
| -.. function:: %%bigquery |
18 |
| -
|
19 |
| - IPython cell magic to run a query and display the result as a DataFrame |
20 |
| -
|
21 |
| - .. code-block:: python |
22 |
| -
|
23 |
| - %%bigquery [<destination_var>] [--project <project>] [--use_legacy_sql] |
24 |
| - [--verbose] [--params <params>] |
25 |
| - <query> |
26 |
| -
|
27 |
| - Parameters: |
28 |
| -
|
29 |
| - * ``<destination_var>`` (Optional[line argument]): |
30 |
| - variable to store the query results. The results are not displayed if |
31 |
| - this parameter is used. If an error occurs during the query execution, |
32 |
| - the corresponding ``QueryJob`` instance (if available) is stored in |
33 |
| - the variable instead. |
34 |
| - * ``--destination_table`` (Optional[line argument]): |
35 |
| - A dataset and table to store the query results. If table does not exists, |
36 |
| - it will be created. If table already exists, its data will be overwritten. |
37 |
| - Variable should be in a format <dataset_id>.<table_id>. |
38 |
| - * ``--no_query_cache`` (Optional[line argument]): |
39 |
| - Do not use cached query results. |
40 |
| - * ``--project <project>`` (Optional[line argument]): |
41 |
| - Project to use for running the query. Defaults to the context |
42 |
| - :attr:`~google.cloud.bigquery.magics.Context.project`. |
43 |
| - * ``--use_bqstorage_api`` (Optional[line argument]): |
44 |
| - [Deprecated] Not used anymore, as BigQuery Storage API is used by default. |
45 |
| - * ``--use_rest_api`` (Optional[line argument]): |
46 |
| - Use the BigQuery REST API instead of the Storage API. |
47 |
| - * ``--use_legacy_sql`` (Optional[line argument]): |
48 |
| - Runs the query using Legacy SQL syntax. Defaults to Standard SQL if |
49 |
| - this argument not used. |
50 |
| - * ``--verbose`` (Optional[line argument]): |
51 |
| - If this flag is used, information including the query job ID and the |
52 |
| - amount of time for the query to complete will not be cleared after the |
53 |
| - query is finished. By default, this information will be displayed but |
54 |
| - will be cleared after the query is finished. |
55 |
| - * ``--params <params>`` (Optional[line argument]): |
56 |
| - If present, the argument following the ``--params`` flag must be |
57 |
| - either: |
58 |
| -
|
59 |
| - * :class:`str` - A JSON string representation of a dictionary in the |
60 |
| - format ``{"param_name": "param_value"}`` (ex. ``{"num": 17}``). Use |
61 |
| - of the parameter in the query should be indicated with |
62 |
| - ``@param_name``. See ``In[5]`` in the Examples section below. |
63 |
| -
|
64 |
| - * :class:`dict` reference - A reference to a ``dict`` in the format |
65 |
| - ``{"param_name": "param_value"}``, where the value types must be JSON |
66 |
| - serializable. The variable reference is indicated by a ``$`` before |
67 |
| - the variable name (ex. ``$my_dict_var``). See ``In[6]`` and ``In[7]`` |
68 |
| - in the Examples section below. |
69 |
| -
|
70 |
| - * ``<query>`` (required, cell argument): |
71 |
| - SQL query to run. If the query does not contain any whitespace (aside |
72 |
| - from leading and trailing whitespace), it is assumed to represent a |
73 |
| - fully-qualified table ID, and the latter's data will be fetched. |
| 17 | +Install ``bigquery-magics`` and call ``%load_ext bigquery_magics`` to use the |
| 18 | +``%%bigquery`` cell magic. |
74 | 19 |
|
75 |
| - Returns: |
76 |
| - A :class:`pandas.DataFrame` with the query results. |
77 |
| -
|
78 |
| - .. note:: |
79 |
| - All queries run using this magic will run using the context |
80 |
| - :attr:`~google.cloud.bigquery.magics.Context.credentials`. |
| 20 | +See the `BigQuery Magics reference documentation |
| 21 | +<https://googleapis.dev/python/bigquery-magics/latest/>`_. |
81 | 22 | """
|
82 | 23 |
|
83 | 24 | from __future__ import print_function
|
|
109 | 50 | from google.cloud.bigquery.dbapi import _helpers
|
110 | 51 | from google.cloud.bigquery.magics import line_arg_parser as lap
|
111 | 52 |
|
| 53 | +try: |
| 54 | + import bigquery_magics # type: ignore |
| 55 | +except ImportError: |
| 56 | + bigquery_magics = None |
| 57 | + |
112 | 58 |
|
113 | 59 | IPYTHON_USER_AGENT = "ipython-{}".format(IPython.__version__)
|
114 | 60 |
|
@@ -280,7 +226,14 @@ def progress_bar_type(self, value):
|
280 | 226 | self._progress_bar_type = value
|
281 | 227 |
|
282 | 228 |
|
283 |
| -context = Context() |
| 229 | +# If bigquery_magics is available, we load that extension rather than this one. |
| 230 | +# Ensure google.cloud.bigquery.magics.context setters are on the correct magics |
| 231 | +# implementation in case the user has installed the package but hasn't updated |
| 232 | +# their code. |
| 233 | +if bigquery_magics is not None: |
| 234 | + context = bigquery_magics.context |
| 235 | +else: |
| 236 | + context = Context() |
284 | 237 |
|
285 | 238 |
|
286 | 239 | def _handle_error(error, destination_var=None):
|
|
0 commit comments