From 94e1e5e6e7ece45c4878197706180f80823f57c0 Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Tue, 29 Jul 2025 14:59:19 -0400 Subject: [PATCH 01/11] feat: add helloworld samples for FastAPI and Streamlit --- run/helloworld-fastapi/README.md | 49 +++++++++++++++++++ run/helloworld-fastapi/main.py | 27 ++++++++++ run/helloworld-fastapi/main_test.py | 37 ++++++++++++++ run/helloworld-fastapi/requirements-test.txt | 1 + run/helloworld-fastapi/requirements.txt | 2 + run/helloworld-streamlit/README.md | 49 +++++++++++++++++++ run/helloworld-streamlit/main.py | 43 ++++++++++++++++ run/helloworld-streamlit/main_test.py | 20 ++++++++ .../requirements-test.txt | 1 + run/helloworld-streamlit/requirements.txt | 1 + 10 files changed, 230 insertions(+) create mode 100644 run/helloworld-fastapi/README.md create mode 100644 run/helloworld-fastapi/main.py create mode 100644 run/helloworld-fastapi/main_test.py create mode 100644 run/helloworld-fastapi/requirements-test.txt create mode 100644 run/helloworld-fastapi/requirements.txt create mode 100644 run/helloworld-streamlit/README.md create mode 100644 run/helloworld-streamlit/main.py create mode 100644 run/helloworld-streamlit/main_test.py create mode 100644 run/helloworld-streamlit/requirements-test.txt create mode 100644 run/helloworld-streamlit/requirements.txt diff --git a/run/helloworld-fastapi/README.md b/run/helloworld-fastapi/README.md new file mode 100644 index 00000000000..63e1d639cac --- /dev/null +++ b/run/helloworld-fastapi/README.md @@ -0,0 +1,49 @@ +# Cloud Run Hello World FastAPI Sample + +This sample shows how to deploy a Hello World FastAPI application to Cloud Run. + +[![Run in Google Cloud][run_img]][run_link] + +[run_img]: https://storage.googleapis.com/cloudrun/button.svg +[run_link]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&cloudshell_working_dir=run/helloworld-fastapi + +## Build + +* Set an environment variable with your GCP Project ID: + +``` +export GOOGLE_CLOUD_PROJECT= +``` + +* Use a [Buildpack](https://github.com/GoogleCloudPlatform/buildpacks) to build the container: + +```sh +gcloud builds submit --pack image=gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-fastapi +``` + +## Run Locally + +```sh +docker run --rm gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-fastapi +``` + +## Test + +``` +pytest +``` + +_Note: you may need to install `pytest` using `pip install pytest`._ + +## Deploy + +```sh +# Set an environment variable with your GCP Project ID +export GOOGLE_CLOUD_PROJECT= + +# Deploy to Cloud Run +gcloud run deploy helloworld-fastapi --source . +``` + + +For more details on how to work with this sample read the [Python Cloud Run Samples README](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/run) diff --git a/run/helloworld-fastapi/main.py b/run/helloworld-fastapi/main.py new file mode 100644 index 00000000000..9fd7ba6ca6b --- /dev/null +++ b/run/helloworld-fastapi/main.py @@ -0,0 +1,27 @@ +# Copyright 2025 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. + +# [START cloudrun_helloworld_fastapi] +from fastapi import FastAPI + +app = FastAPI() + + +@app.get("/") +def hello(name: str = "World"): + """Return a friendly HTTP greeting.""" + return { + "message": f"Hello {name}!" + } +# [END cloudrun_helloworld_fastapi] diff --git a/run/helloworld-fastapi/main_test.py b/run/helloworld-fastapi/main_test.py new file mode 100644 index 00000000000..8eb12f5fa30 --- /dev/null +++ b/run/helloworld-fastapi/main_test.py @@ -0,0 +1,37 @@ +# Copyright 2025 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 pytest +from fastapi.testclient import TestClient + +import main + + +@pytest.fixture +def client(): + return TestClient(main.app) + + +def test_handler_no_param(client): + r = client.get("/") + + assert r.json() == {"message": "Hello World!"} + assert r.status_code == 200 + + +def test_handler_with_param(client): + r = client.get("/", params={"name": "Foo"}) + + assert r.json() == {"message": "Hello Foo!"} + assert r.status_code == 200 diff --git a/run/helloworld-fastapi/requirements-test.txt b/run/helloworld-fastapi/requirements-test.txt new file mode 100644 index 00000000000..15d066af319 --- /dev/null +++ b/run/helloworld-fastapi/requirements-test.txt @@ -0,0 +1 @@ +pytest==8.2.0 diff --git a/run/helloworld-fastapi/requirements.txt b/run/helloworld-fastapi/requirements.txt new file mode 100644 index 00000000000..5c986378479 --- /dev/null +++ b/run/helloworld-fastapi/requirements.txt @@ -0,0 +1,2 @@ +fastapi[standard]==0.116.1 +uvicorn==0.35.0 diff --git a/run/helloworld-streamlit/README.md b/run/helloworld-streamlit/README.md new file mode 100644 index 00000000000..329e182b0e4 --- /dev/null +++ b/run/helloworld-streamlit/README.md @@ -0,0 +1,49 @@ +# Cloud Run Hello World Streamlit Sample + +This sample shows how to deploy a Hello World Streamlit application to Cloud Run. + +[![Run in Google Cloud][run_img]][run_link] + +[run_img]: https://storage.googleapis.com/cloudrun/button.svg +[run_link]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&cloudshell_working_dir=run/helloworld-streamlit + +## Build + +* Set an environment variable with your GCP Project ID: + +``` +export GOOGLE_CLOUD_PROJECT= +``` + +* Use a [Buildpack](https://github.com/GoogleCloudPlatform/buildpacks) to build the container: + +```sh +gcloud builds submit --pack image=gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-streamlit +``` + +## Run Locally + +```sh +docker run --rm gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-streamlit +``` + +## Test + +``` +pytest +``` + +_Note: you may need to install `pytest` using `pip install pytest`._ + +## Deploy + +```sh +# Set an environment variable with your GCP Project ID +export GOOGLE_CLOUD_PROJECT= + +# Deploy to Cloud Run +gcloud run deploy helloworld-streamlit --source . +``` + + +For more details on how to work with this sample read the [Python Cloud Run Samples README](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/run) diff --git a/run/helloworld-streamlit/main.py b/run/helloworld-streamlit/main.py new file mode 100644 index 00000000000..9d3c73a0a9b --- /dev/null +++ b/run/helloworld-streamlit/main.py @@ -0,0 +1,43 @@ +# Copyright 2025 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. + +# [START cloudrun_helloworld_streamlit] +import streamlit as st + +st.title(f"Hello World! 👋🌎") +st.markdown( + """ + This is a demo Streamlit app. + + Enter your name in the text box below and press a button to see some fun features in Streamlit. + """ +) + +name = st.text_input("Enter your name:") + +# Use columns to create buttons side by side +col1, col2 = st.columns(2) + +with col1: + if st.button("Send balloons! 🎈"): + st.balloons() + st.write(f"Time to celebrate {name}! 🥳") + st.write("You deployed a Streamlit app! 👏") + +with col2: + if st.button("Send snow! ❄️"): + st.snow() + st.write(f"Let it snow {name}! 🌨️") + st.write("You deployed a Streamlit app! 👏") +# [END cloudrun_helloworld_streamlit] diff --git a/run/helloworld-streamlit/main_test.py b/run/helloworld-streamlit/main_test.py new file mode 100644 index 00000000000..f185b75cbfd --- /dev/null +++ b/run/helloworld-streamlit/main_test.py @@ -0,0 +1,20 @@ +from streamlit.testing.v1 import AppTest + +def test_balloons(): + """A user presses the balloons button""" + at = AppTest.from_file("main.py").run() + at.text_input[0].set_value("Foo").run() + at.button[0].click().run() + assert at.markdown.values[0] == "This is a demo Streamlit app.\n\nEnter your name in the text box below and press a button to see some fun features in Streamlit." + assert at.markdown.values[1] == "Time to celebrate Foo! 🥳" + assert at.markdown.values[2] == "You deployed a Streamlit app! 👏" + +def test_snow(): + """A user presses the snow button""" + at = AppTest.from_file("main.py").run() + at.text_input[0].set_value("Foo").run() + at.button[1].click().run() + assert at.markdown.values[0] == "This is a demo Streamlit app.\n\nEnter your name in the text box below and press a button to see some fun features in Streamlit." + assert at.markdown.values[1] == "Let it snow Foo! 🌨️" + assert at.markdown.values[2] == "You deployed a Streamlit app! 👏" + diff --git a/run/helloworld-streamlit/requirements-test.txt b/run/helloworld-streamlit/requirements-test.txt new file mode 100644 index 00000000000..15d066af319 --- /dev/null +++ b/run/helloworld-streamlit/requirements-test.txt @@ -0,0 +1 @@ +pytest==8.2.0 diff --git a/run/helloworld-streamlit/requirements.txt b/run/helloworld-streamlit/requirements.txt new file mode 100644 index 00000000000..56cf7132a1a --- /dev/null +++ b/run/helloworld-streamlit/requirements.txt @@ -0,0 +1 @@ +streamlit==1.47.1 From c2f88f056a5b7e288966181ba202bfbc35d6b996 Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Tue, 29 Jul 2025 15:03:01 -0400 Subject: [PATCH 02/11] chore: update header license --- run/helloworld-streamlit/main_test.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/run/helloworld-streamlit/main_test.py b/run/helloworld-streamlit/main_test.py index f185b75cbfd..fcdfa4df495 100644 --- a/run/helloworld-streamlit/main_test.py +++ b/run/helloworld-streamlit/main_test.py @@ -1,3 +1,17 @@ +# Copyright 2025 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. + from streamlit.testing.v1 import AppTest def test_balloons(): From 797e44ad747153df34a7647db270a3ab62fe3f47 Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Tue, 29 Jul 2025 15:05:33 -0400 Subject: [PATCH 03/11] chore: gemini review advice --- run/helloworld-fastapi/README.md | 4 ++-- run/helloworld-streamlit/README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/run/helloworld-fastapi/README.md b/run/helloworld-fastapi/README.md index 63e1d639cac..10dba162c0d 100644 --- a/run/helloworld-fastapi/README.md +++ b/run/helloworld-fastapi/README.md @@ -11,7 +11,7 @@ This sample shows how to deploy a Hello World FastAPI application to Cloud Run. * Set an environment variable with your GCP Project ID: -``` +```sh export GOOGLE_CLOUD_PROJECT= ``` @@ -29,7 +29,7 @@ docker run --rm gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-fastapi ## Test -``` +```sh pytest ``` diff --git a/run/helloworld-streamlit/README.md b/run/helloworld-streamlit/README.md index 329e182b0e4..83c2e315329 100644 --- a/run/helloworld-streamlit/README.md +++ b/run/helloworld-streamlit/README.md @@ -11,7 +11,7 @@ This sample shows how to deploy a Hello World Streamlit application to Cloud Run * Set an environment variable with your GCP Project ID: -``` +```sh export GOOGLE_CLOUD_PROJECT= ``` @@ -29,7 +29,7 @@ docker run --rm gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-streamlit ## Test -``` +```sh pytest ``` From 063082ad239e7d41308ef502dee6a121b948cb2a Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Tue, 29 Jul 2025 15:10:33 -0400 Subject: [PATCH 04/11] chore: add fixture to Streamlit test --- run/helloworld-streamlit/main_test.py | 34 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/run/helloworld-streamlit/main_test.py b/run/helloworld-streamlit/main_test.py index fcdfa4df495..30b64bca26e 100644 --- a/run/helloworld-streamlit/main_test.py +++ b/run/helloworld-streamlit/main_test.py @@ -13,22 +13,28 @@ # limitations under the License. from streamlit.testing.v1 import AppTest +import pytest -def test_balloons(): - """A user presses the balloons button""" + +@pytest.fixture +def app_test() -> AppTest: + """Fixture for creating a test app instance and setting a name.""" at = AppTest.from_file("main.py").run() at.text_input[0].set_value("Foo").run() - at.button[0].click().run() - assert at.markdown.values[0] == "This is a demo Streamlit app.\n\nEnter your name in the text box below and press a button to see some fun features in Streamlit." - assert at.markdown.values[1] == "Time to celebrate Foo! 🥳" - assert at.markdown.values[2] == "You deployed a Streamlit app! 👏" + return at + + +def test_balloons(app_test: AppTest): + """A user presses the balloons button""" + app_test.button[0].click().run() + assert app_test.markdown.values[0] == "This is a demo Streamlit app.\n\nEnter your name in the text box below and press a button to see some fun features in Streamlit." + assert app_test.markdown.values[1] == "Time to celebrate Foo! 🥳" + assert app_test.markdown.values[2] == "You deployed a Streamlit app! 👏" + -def test_snow(): +def test_snow(app_test: AppTest): """A user presses the snow button""" - at = AppTest.from_file("main.py").run() - at.text_input[0].set_value("Foo").run() - at.button[1].click().run() - assert at.markdown.values[0] == "This is a demo Streamlit app.\n\nEnter your name in the text box below and press a button to see some fun features in Streamlit." - assert at.markdown.values[1] == "Let it snow Foo! 🌨️" - assert at.markdown.values[2] == "You deployed a Streamlit app! 👏" - + app_test.button[1].click().run() + assert app_test.markdown.values[0] == "This is a demo Streamlit app.\n\nEnter your name in the text box below and press a button to see some fun features in Streamlit." + assert app_test.markdown.values[1] == "Let it snow Foo! 🌨️" + assert app_test.markdown.values[2] == "You deployed a Streamlit app! 👏" From a53e998f4f363e43eba53357ee018c3552dc52b0 Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Tue, 29 Jul 2025 15:13:32 -0400 Subject: [PATCH 05/11] chore: lint --- run/helloworld-fastapi/main_test.py | 2 +- run/helloworld-streamlit/main.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/run/helloworld-fastapi/main_test.py b/run/helloworld-fastapi/main_test.py index 8eb12f5fa30..ae57ffe4357 100644 --- a/run/helloworld-fastapi/main_test.py +++ b/run/helloworld-fastapi/main_test.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pytest from fastapi.testclient import TestClient +import pytest import main diff --git a/run/helloworld-streamlit/main.py b/run/helloworld-streamlit/main.py index 9d3c73a0a9b..7d7d55c446e 100644 --- a/run/helloworld-streamlit/main.py +++ b/run/helloworld-streamlit/main.py @@ -15,7 +15,7 @@ # [START cloudrun_helloworld_streamlit] import streamlit as st -st.title(f"Hello World! 👋🌎") +st.title("Hello World! 👋🌎") st.markdown( """ This is a demo Streamlit app. From 55dca2c975543567884bfddb9493b47797561381 Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Tue, 29 Jul 2025 16:07:03 -0400 Subject: [PATCH 06/11] chore: lint --- run/helloworld-streamlit/main_test.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/run/helloworld-streamlit/main_test.py b/run/helloworld-streamlit/main_test.py index 30b64bca26e..84282d0bc78 100644 --- a/run/helloworld-streamlit/main_test.py +++ b/run/helloworld-streamlit/main_test.py @@ -12,9 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from streamlit.testing.v1 import AppTest import pytest - +from streamlit.testing.v1 import AppTest @pytest.fixture def app_test() -> AppTest: From b16060f8c0389a0b5a12973945337f6ef57b8081 Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Tue, 29 Jul 2025 16:21:40 -0400 Subject: [PATCH 07/11] chore: lint --- run/helloworld-streamlit/main_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/run/helloworld-streamlit/main_test.py b/run/helloworld-streamlit/main_test.py index 84282d0bc78..f196dc471c6 100644 --- a/run/helloworld-streamlit/main_test.py +++ b/run/helloworld-streamlit/main_test.py @@ -15,6 +15,7 @@ import pytest from streamlit.testing.v1 import AppTest + @pytest.fixture def app_test() -> AppTest: """Fixture for creating a test app instance and setting a name.""" From cfd98590b504c6c371f76d808e2e5a6712aa4be2 Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Wed, 30 Jul 2025 10:28:01 -0400 Subject: [PATCH 08/11] chore: update READMEs --- run/helloworld-fastapi/README.md | 33 ++---------------------------- run/helloworld-streamlit/README.md | 33 ++---------------------------- 2 files changed, 4 insertions(+), 62 deletions(-) diff --git a/run/helloworld-fastapi/README.md b/run/helloworld-fastapi/README.md index 10dba162c0d..c280b7dd41b 100644 --- a/run/helloworld-fastapi/README.md +++ b/run/helloworld-fastapi/README.md @@ -7,43 +7,14 @@ This sample shows how to deploy a Hello World FastAPI application to Cloud Run. [run_img]: https://storage.googleapis.com/cloudrun/button.svg [run_link]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&cloudshell_working_dir=run/helloworld-fastapi -## Build - -* Set an environment variable with your GCP Project ID: - -```sh -export GOOGLE_CLOUD_PROJECT= -``` - -* Use a [Buildpack](https://github.com/GoogleCloudPlatform/buildpacks) to build the container: - -```sh -gcloud builds submit --pack image=gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-fastapi -``` - -## Run Locally - -```sh -docker run --rm gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-fastapi -``` - -## Test - -```sh -pytest -``` - -_Note: you may need to install `pytest` using `pip install pytest`._ - ## Deploy ```sh -# Set an environment variable with your GCP Project ID -export GOOGLE_CLOUD_PROJECT= +# Ensure you have set your Google Cloud Project ID +gcloud config set project # Deploy to Cloud Run gcloud run deploy helloworld-fastapi --source . ``` - For more details on how to work with this sample read the [Python Cloud Run Samples README](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/run) diff --git a/run/helloworld-streamlit/README.md b/run/helloworld-streamlit/README.md index 83c2e315329..4994743f9af 100644 --- a/run/helloworld-streamlit/README.md +++ b/run/helloworld-streamlit/README.md @@ -7,43 +7,14 @@ This sample shows how to deploy a Hello World Streamlit application to Cloud Run [run_img]: https://storage.googleapis.com/cloudrun/button.svg [run_link]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&cloudshell_working_dir=run/helloworld-streamlit -## Build - -* Set an environment variable with your GCP Project ID: - -```sh -export GOOGLE_CLOUD_PROJECT= -``` - -* Use a [Buildpack](https://github.com/GoogleCloudPlatform/buildpacks) to build the container: - -```sh -gcloud builds submit --pack image=gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-streamlit -``` - -## Run Locally - -```sh -docker run --rm gcr.io/${GOOGLE_CLOUD_PROJECT}/helloworld-streamlit -``` - -## Test - -```sh -pytest -``` - -_Note: you may need to install `pytest` using `pip install pytest`._ - ## Deploy ```sh -# Set an environment variable with your GCP Project ID -export GOOGLE_CLOUD_PROJECT= +# Ensure you have set your Google Cloud Project ID +gcloud config set project # Deploy to Cloud Run gcloud run deploy helloworld-streamlit --source . ``` - For more details on how to work with this sample read the [Python Cloud Run Samples README](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/run) From fcb0005f033097e2ef617e39c4f2921e01ea8978 Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Wed, 30 Jul 2025 10:29:15 -0400 Subject: [PATCH 09/11] chore: update README --- run/helloworld-fastapi/README.md | 2 +- run/helloworld-streamlit/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/run/helloworld-fastapi/README.md b/run/helloworld-fastapi/README.md index c280b7dd41b..4d86597fc8a 100644 --- a/run/helloworld-fastapi/README.md +++ b/run/helloworld-fastapi/README.md @@ -17,4 +17,4 @@ gcloud config set project gcloud run deploy helloworld-fastapi --source . ``` -For more details on how to work with this sample read the [Python Cloud Run Samples README](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/run) +For more details on how to work with this sample, read the [Python Cloud Run Samples README](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/run) diff --git a/run/helloworld-streamlit/README.md b/run/helloworld-streamlit/README.md index 4994743f9af..bd2b0446d33 100644 --- a/run/helloworld-streamlit/README.md +++ b/run/helloworld-streamlit/README.md @@ -17,4 +17,4 @@ gcloud config set project gcloud run deploy helloworld-streamlit --source . ``` -For more details on how to work with this sample read the [Python Cloud Run Samples README](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/run) +For more details on how to work with this sample, read the [Python Cloud Run Samples README](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/run) From 5a9126a9d074c4272c8e4e71ae21ce2331eef443 Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Thu, 31 Jul 2025 11:38:25 -0400 Subject: [PATCH 10/11] feat: add Gradio sample --- run/helloworld-gradio/README.md | 20 +++++++++++++++ run/helloworld-gradio/main.py | 34 ++++++++++++++++++++++++++ run/helloworld-gradio/requirements.txt | 1 + 3 files changed, 55 insertions(+) create mode 100644 run/helloworld-gradio/README.md create mode 100644 run/helloworld-gradio/main.py create mode 100644 run/helloworld-gradio/requirements.txt diff --git a/run/helloworld-gradio/README.md b/run/helloworld-gradio/README.md new file mode 100644 index 00000000000..aa33c32010a --- /dev/null +++ b/run/helloworld-gradio/README.md @@ -0,0 +1,20 @@ +# Cloud Run Hello World Gradio Sample + +This sample shows how to deploy a Hello World Gradio application to Cloud Run. + +[![Run in Google Cloud][run_img]][run_link] + +[run_img]: https://storage.googleapis.com/cloudrun/button.svg +[run_link]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&cloudshell_working_dir=run/helloworld-gradio + +## Deploy + +```sh +# Ensure you have set your Google Cloud Project ID +gcloud config set project + +# Deploy to Cloud Run +gcloud run deploy helloworld-gradio --source . +``` + +For more details on how to work with this sample, read the [Python Cloud Run Samples README](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/run) diff --git a/run/helloworld-gradio/main.py b/run/helloworld-gradio/main.py new file mode 100644 index 00000000000..ce481275b4b --- /dev/null +++ b/run/helloworld-gradio/main.py @@ -0,0 +1,34 @@ +# Copyright 2025 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. + +# [START cloudrun_helloworld_gradio] +import gradio as gr + +def hello(name, intensity): + """Return a friendly greeting.""" + return "Hello " + name + "!" * int(intensity) + +demo = gr.Interface( + fn=hello, + inputs=["text", "slider"], + outputs=["text"], + title="Hello World 👋🌎", + description=("Type your name below and hit 'Submit', and try the slider to " + "make the greeting louder!"), + theme="soft", + flagging_mode="never", +) + +demo.launch() +# [END cloudrun_helloworld_gradio] diff --git a/run/helloworld-gradio/requirements.txt b/run/helloworld-gradio/requirements.txt new file mode 100644 index 00000000000..f4fb04bb92f --- /dev/null +++ b/run/helloworld-gradio/requirements.txt @@ -0,0 +1 @@ +gradio==5.39.0 From bf0ff72ab46d9cdbf524a38833f0bf8d45a9b2e1 Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Thu, 31 Jul 2025 11:55:41 -0400 Subject: [PATCH 11/11] chore: lint --- run/helloworld-gradio/main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/run/helloworld-gradio/main.py b/run/helloworld-gradio/main.py index ce481275b4b..d1d5ed4ed2b 100644 --- a/run/helloworld-gradio/main.py +++ b/run/helloworld-gradio/main.py @@ -15,10 +15,12 @@ # [START cloudrun_helloworld_gradio] import gradio as gr + def hello(name, intensity): """Return a friendly greeting.""" return "Hello " + name + "!" * int(intensity) + demo = gr.Interface( fn=hello, inputs=["text", "slider"],