I am new to Google Cloud and I want to use the Cloud Code extension.
Give me some examples of Google services that I can use to build and deploy a
sample app using containers.
Gemini 会在回答中列出 Google Cloud 服务
信息。
在本示例中,假设 Gemini 建议
Cloud Run 和 Cloud Run 作为 Google Cloud 中的两个
这些服务可以帮助您构建和部署示例应用,但您还需要更多
这些服务的相关信息
假设您希望使用 Cloud Run 创建和部署
基本 Python 应用。因为您刚开始接触 Cloud Run
Cloud Code,您需要有关创建应用步骤的帮助。
在本部分中,您需要提示 Gemini 输入以下内容:
可帮助您在 Cloud Run 中构建 Hello World Python 应用。
启动 IDE(如果尚未启动)。
在 IDE 的活动栏中,点击
sparkGemini Code Assist。
在 Gemini Code Assist: Chat 窗格中,输入以下提示,然后
然后点击发送:
How to create a new Cloud Run app in Cloud Code using the command palette?
What languages are supported?
VS Code 中的命令面板提供了所有命令的列表,包括
Cloud Code 的命令。
输出类似于以下内容:
To create a new Cloud Run app in Cloud Code using the command palette,
follow these steps:
1. Open the command palette (press Ctrl/Cmd+Shift+P or click View > Command Palette).
2. Run the "Cloud Code: New Cloud Run App" command.
...
Gemini 还会显示
Cloud Run 应用。
在此示例中,您选择使用基于 Python 的 Cloud Run
模板创建示例应用。
如需了解如何使用 Cloud Run 模板创建 Python 应用,
输入以下提示:
How do I create a Python app from a Cloud Run template using the
command palette? I am using Cloud Code.
输出类似于以下内容:
To create a Python app from a Cloud Run template using the command palette,
follow these steps:
1. Open Cloud Code.
2. Click the Command Palette
(press Ctrl/Cmd+Shift+P or click View > Command Palette).
3. Run the "Cloud Code: New Application" command.
...
完成 Gemini 在回答以下问题时提供的步骤
在 Cloud Run 中创建一个基本的 Hello World Python 应用。
您还可以参考以下部分来创建 Hello World
Python 应用。
创建 Hello World Python 应用的替代步骤
启动 IDE(如果尚未启动)。
打开命令面板:按 Command+Shift+P(在 macOS 上)或
Ctrl+Shift+P(适用于 Windows 和 Linux)。
在命令面板字段中,输入 Cloud Code: New Application,以及
然后点击相应结果
从可用示例列表中,选择 Cloud Run 应用。
从可用模板列表中,选择 Python (Flask): Cloud Run。
将新应用保存到首选位置。
系统会显示一条通知,确认您的应用已创建。
系统会在 IDE 中打开所选服务的 README 文件的预览,
如图 1 所示:
<ph type="x-smartling-placeholder"></ph>
图 1 为在 Cloud Run 中创建的 Hello World Python 应用显示的 README 文件。
探索 Python 版 Hello World 应用
现在,您已经在 Cloud Run 中创建了 Hello World 应用,
你可以使用 Gemini 来解释
部署在 IDE 中的项目。探索示例应用的代码
请按照以下步骤操作:
启动 IDE。
打开Explorer:按 Command+Shift+E(在 macOS 上)或
Ctrl+Shift+E(适用于 Windows 和 Linux)。
What is the function of the app.py file in Dockerfile?
Gemini 会生成类似于以下内容的回答:
The app.py file is the entrypoint for the container. It is the file that
will be executed when the container is launched. In this case, the app.py
file will run the Python code that is contained within it.
service = os.environ.get('K_SERVICE', 'Unknown service')
revision = os.environ.get('K_REVISION', 'Unknown revision')
响应类似于以下示例:
The code above is using the os.environ module to get the values of the
K_SERVICE and K_REVISION environment variables. These variables are set by
Cloud Run when it deploys the service, and they contain the name of the
service and the revision number, respectively.
...
Create a variable called inventory which is a list of 3 JSON objects.
Each JSON object has 2 attributes: productid and onhandqty.
Both attributes are strings.
Gemini 会为三个 JSON 对象生成示例代码。
在回复中,点击
addInsert in current file,将代码示例插入
inventory.py 文件,如图 3 所示:
# Generate an App route to display a list of inventory# items in the JSON format from the# inventory.py file. Use the GET method.@app.route('/inventory',methods=['GET'])defget_inventory():
"""Returnalistofinventoryitems."""
returnjsonify(inventory)
如需添加其他 API 方法,请在 app.py 文件中输入
以下提示,然后按 Ctrl+Enter 键:
# Generate an App route to get an inventory item
# given the productid. Use the GET method.
# If there is an invalid productid,
# display a 404 error.
Gemini 在 app.py 文件中生成回答。
将指针悬停在回答的任意部分上。
系统随即会显示 Gemini 工具栏。
要接受某个建议的代码示例,请在工具栏中点击接受。
输出类似于以下内容:
# Generate an App route to get an inventory item# given the productid. Use the GET method.# If there is an invalid productid,# display a 404 error.@app.route('/inventory/<productid>',methods=['GET'])defget_inventory_item(productid):
"""Returnaninventoryitemgiventheproductid."""
foritemininventory:ifitem["productid"]==productid:returnjsonify(item)returnjsonify({"error": "Itemnotfound"}),404
您已成功添加了两个用于创建 Inventory 应用的 API 方法。
从 IDE 在本地运行应用
您已准备好通过 VS Code 在本地部署应用。
如需部署应用,请按以下步骤操作:
启动 IDE(如果尚未启动)。
在 IDE 的活动栏中,点击
sparkGemini Code Assist。
在 Gemini Code Assist 窗格中,输入以下提示,然后
点击 Send:
How do I run a Cloud Run app locally within Cloud Code?
Is there an emulator?
按照 Gemini 回答中的步骤运行该应用
从 VS Code 本地运行您还可以参阅以下部分
VS Code 中的本地 Cloud Run 应用示例。
从 IDE 在本地运行应用的其他步骤
如果你没有按照 Gemini 回答中的步骤进行操作
您可以按照以下步骤部署示例应用:
启动 IDE。
打开命令面板:按 Command+Shift+P(在 macOS 上)
或 Ctrl+Shift+P(适用于 Windows 和 Linux)。
在命令面板字段中,输入 Run on Cloud Run Emulator,以及
然后点击相应结果
在 Build 环境文件中,保留默认值,然后点击 Run。
本教程使用 Docker 作为构建器选项。
在 IDE 的 Output 窗格中,您可以查看构建进度。
实际的部署过程可能需要一些时间才能完成。
Starting to run the app using configuration 'Cloud Run:
Run/Debug Locally' from .vscode/launch.json...
To view more detailed logs, go to Output channel : "Cloud Run: Run/Debug Locally - Detailed"
Dependency check started
Dependency check succeeded
Starting minikube, this may take a while......
...
要查看您的实时应用,请在部署完成后点击
输出窗格。对于 VS Code 的本地副本,如果您指定了 localhost 8080,
您的示例应用位于以下网址:http://localhost:8080。