Skip to content

Commit 62a88e8

Browse files
authored
docs: show best practice of closing the session to cleanup resources in sample notebooks (#1095)
BEGIN_COMMIT_OVERRIDE docs: show best practice of closing the session to cleanup resources in sample notebooks (#1095) test: include stale cloud functions cleanup in the default nox run (#1095) END_COMMIT_OVERRIDE 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://togithub.com/googleapis/python-bigquery-dataframes/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) Fixes #<issue_number_goes_here> 🦕
1 parent 863d694 commit 62a88e8

File tree

8 files changed

+97
-23
lines changed

8 files changed

+97
-23
lines changed

bigframes/core/global_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
_global_session_state.thread_local_session = None
3131

3232

33-
def _try_close_session(session):
33+
def _try_close_session(session: bigframes.session.Session):
3434
"""Try to close the session and warn if couldn't."""
3535
try:
3636
session.close()

notebooks/generative_ai/bq_dataframes_llm_code_generation.ipynb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,15 @@
12171217
"Otherwise, you can uncomment the remaining cells and run them to delete the individual resources you created in this tutorial:"
12181218
]
12191219
},
1220+
{
1221+
"cell_type": "code",
1222+
"execution_count": null,
1223+
"metadata": {},
1224+
"outputs": [],
1225+
"source": [
1226+
"bf.close_session()"
1227+
]
1228+
},
12201229
{
12211230
"cell_type": "code",
12221231
"execution_count": 24,

notebooks/getting_started/getting_started_bq_dataframes.ipynb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,16 @@
17041704
"Otherwise, you can uncomment the remaining cells and run them to delete the individual resources you created in this tutorial:"
17051705
]
17061706
},
1707+
{
1708+
"cell_type": "code",
1709+
"execution_count": null,
1710+
"metadata": {},
1711+
"outputs": [],
1712+
"source": [
1713+
"# Delete the temporary cloud artifacts created during the bigframes session \n",
1714+
"bpd.close_session()"
1715+
]
1716+
},
17071717
{
17081718
"cell_type": "code",
17091719
"execution_count": 25,

notebooks/location/regionalized.ipynb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
"source": [
88
"# README\n",
99
"\n",
10-
"This Notebook runs differently depending on the following environent variable:\n",
11-
"1. BIGQUERY_LOCATION - can take values as per https://cloud.google.com/bigquery/docs/locations, e.g. `us`, `asia-east1`"
10+
"This Notebook runs requiring the following environent variable:\n",
11+
"1. GOOGLE_CLOUD_PROJECT - The google cloud project id.\n",
12+
"1. BIGQUERY_LOCATION - can take values as per https://cloud.google.com/bigquery/docs/locations, e.g. `us`, `asia-east1`."
1213
]
1314
},
1415
{
@@ -1420,8 +1421,8 @@
14201421
}
14211422
],
14221423
"source": [
1423-
"import bigframes.pandas as pd\n",
1424-
"help(pd.remote_function)"
1424+
"import bigframes.pandas as bpd\n",
1425+
"help(bpd.remote_function)"
14251426
]
14261427
},
14271428
{
@@ -1460,7 +1461,7 @@
14601461
}
14611462
],
14621463
"source": [
1463-
"@pd.remote_function([float], str, bigquery_connection='bigframes-rf-conn')\n",
1464+
"@bpd.remote_function([float], str, bigquery_connection='bigframes-rf-conn')\n",
14641465
"def get_bucket(num):\n",
14651466
" if not num: return \"NA\"\n",
14661467
" boundary = 4000\n",
@@ -2784,6 +2785,22 @@
27842785
"source": [
27852786
"model.to_gbq(f\"{DATASET}.penguins_model\", replace=True)"
27862787
]
2788+
},
2789+
{
2790+
"cell_type": "markdown",
2791+
"metadata": {},
2792+
"source": [
2793+
"# Clean Up"
2794+
]
2795+
},
2796+
{
2797+
"cell_type": "code",
2798+
"execution_count": null,
2799+
"metadata": {},
2800+
"outputs": [],
2801+
"source": [
2802+
"bpd.close_session()"
2803+
]
27872804
}
27882805
],
27892806
"metadata": {

notebooks/remote_functions/remote_function_usecases.ipynb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"cell_type": "markdown",
2626
"metadata": {},
2727
"source": [
28-
"# Setup"
28+
"# Set Up"
2929
]
3030
},
3131
{
@@ -1379,6 +1379,22 @@
13791379
"df1 = df.assign(duration_cat=df[\"duration_minutes\"].apply(duration_category))\n",
13801380
"df1.peek()"
13811381
]
1382+
},
1383+
{
1384+
"cell_type": "markdown",
1385+
"metadata": {},
1386+
"source": [
1387+
"# Clean Up"
1388+
]
1389+
},
1390+
{
1391+
"cell_type": "code",
1392+
"execution_count": null,
1393+
"metadata": {},
1394+
"outputs": [],
1395+
"source": [
1396+
"bpd.close_session()"
1397+
]
13821398
}
13831399
],
13841400
"metadata": {

notebooks/remote_functions/remote_function_vertex_claude_model.ipynb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,21 @@
452452
"df"
453453
]
454454
},
455+
{
456+
"cell_type": "markdown",
457+
"metadata": {},
458+
"source": [
459+
"# Clean Up"
460+
]
461+
},
455462
{
456463
"cell_type": "code",
457464
"execution_count": null,
458465
"metadata": {},
459466
"outputs": [],
460-
"source": []
467+
"source": [
468+
"bpd.close_session()"
469+
]
461470
}
462471
],
463472
"metadata": {

noxfile.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
"system-3.9",
106106
"system-3.12",
107107
"cover",
108+
"cleanup",
108109
]
109110

110111
# Error if a python version is missing
@@ -952,25 +953,24 @@ def release_dry_run(session):
952953
def cleanup(session):
953954
"""Clean up stale and/or temporary resources in the test project."""
954955
google_cloud_project = os.getenv("GOOGLE_CLOUD_PROJECT")
955-
if not google_cloud_project:
956-
session.error(
957-
"Set GOOGLE_CLOUD_PROJECT environment variable to run notebook session."
958-
)
956+
cleanup_options = []
957+
if google_cloud_project:
958+
cleanup_options.append(f"--project-id={google_cloud_project}")
959959

960960
# Cleanup a few stale (more than 12 hours old) temporary cloud run
961961
# functions created by bigframems. This will help keeping the test GCP
962962
# project within the "Number of functions" quota
963963
# https://cloud.google.com/functions/quotas#resource_limits
964964
recency_cutoff_hours = 12
965-
cleanup_count_per_location = 10
965+
cleanup_count_per_location = 20
966+
cleanup_options.extend(
967+
[
968+
f"--recency-cutoff={recency_cutoff_hours}",
969+
"cleanup",
970+
f"--number={cleanup_count_per_location}",
971+
]
972+
)
966973

967974
session.install("-e", ".")
968975

969-
session.run(
970-
"python",
971-
"scripts/manage_cloud_functions.py",
972-
f"--project-id={google_cloud_project}",
973-
f"--recency-cutoff={recency_cutoff_hours}",
974-
"cleanup",
975-
f"--number={cleanup_count_per_location}",
976-
)
976+
session.run("python", "scripts/manage_cloud_functions.py", *cleanup_options)

scripts/manage_cloud_functions.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ def list_str(values):
153153
return [val for val in values.split(",") if val]
154154

155155

156+
def get_project_from_environment():
157+
from google.cloud import bigquery
158+
159+
return bigquery.Client().project
160+
161+
156162
if __name__ == "__main__":
157163
parser = argparse.ArgumentParser(
158164
description="Manage cloud functions created to serve bigframes remote functions."
@@ -161,9 +167,10 @@ def list_str(values):
161167
"-p",
162168
"--project-id",
163169
type=str,
164-
required=True,
170+
required=False,
165171
action="store",
166-
help="GCP project-id.",
172+
help="GCP project-id. If not provided, the project-id resolved by the"
173+
" BigQuery client from the user environment would be used.",
167174
)
168175
parser.add_argument(
169176
"-r",
@@ -212,4 +219,10 @@ def hours_to_timedelta(hrs):
212219
parser_cleanup.set_defaults(func=cleanup_gcfs)
213220

214221
args = parser.parse_args(sys.argv[1:])
222+
if args.project_id is None:
223+
args.project_id = get_project_from_environment()
224+
if args.project_id is None:
225+
raise ValueError(
226+
"Could not resolve a project. Plese set it via --project-id option."
227+
)
215228
args.func(args)

0 commit comments

Comments
 (0)