Bootstrap a new OSDK Python application(引导创建新的 OSDK Python 应用)¶
On this page, we will walk through the process of creating a Python application that uses the OSDK. The example below can be used with many Python frameworks, such as Flask ↗, Streamlit ↗, and Jupyter ↗.
1: Prerequisites¶
Create a Developer Console application¶
Follow the steps listed in the create a new Developer Console application page.
Set up your token¶
Export your token in your local environment. Below is an example using a sample personal access token, but you can generate a longer-lived one in the Developer Console. This token should not be checked into source control because it is your personal access token.
export FOUNDRY_TOKEN=<YOUR-TOKEN-FROM-GETTING-STARTED-PAGE>
Check Python version¶
The Python SDK requires a Python version >=3.9 and <3.13. To check what version of Python you are using, enter the command below:
python3 --version
Optional: Set up certificate¶
If your organization requires certificates for network traffic, you may need to tell Python where that certificate lives.
export SSL_CERT_FILE="/path/to/my.crt"
export REQUESTS_CA_BUNDLE="/path/to/my.crt"
2: Install the latest version of your SDK¶
Run the following command to install the latest version of the SDK, replacing any < > with your application-specific value that can be found on your application Overview page.
pip install <YOUR-PACKAGE-NAME> --upgrade --extra-index-url "https://:$FOUNDRY_TOKEN@<INDEX-URL>"
Develop your frontend application¶
In your application, initialize the Foundry client and start developing.
import os
from <PACKAGE-NAME> import FoundryClient
from <PACKAGE-NAME>.core.api import UserTokenAuth
auth = UserTokenAuth(hostname="<YOUR-FOUNDRY-URL>", token=os.environ["FOUNDRY_TOKEN"])
client = FoundryClient(auth=auth, hostname="<YOUR-FOUNDRY-URL>")
object = client.ontology.objects.<ANY-OBJECT>
print(object.take(1))
中文翻译¶
引导创建新的 OSDK Python 应用¶
本页面将逐步指导您创建使用 OSDK 的 Python 应用程序。以下示例适用于多种 Python 框架,例如 Flask ↗、Streamlit ↗ 和 Jupyter ↗。
1:前提条件¶
创建 Developer Console 应用程序¶
请按照创建新的 Developer Console 应用程序页面中的步骤进行操作。
设置令牌¶
在本地环境中导出您的令牌。以下示例使用了一个示例个人访问令牌(personal access token),但您也可以在 Developer Console 中生成一个有效期更长的令牌。由于这是您的个人访问令牌,请勿将其检入源代码管理。
export FOUNDRY_TOKEN=<您的入门页面令牌>
检查 Python 版本¶
Python SDK 要求 Python 版本 >=3.9 且 <3.13。要检查您当前使用的 Python 版本,请输入以下命令:
python3 --version
可选:设置证书¶
如果您的组织要求对网络流量使用证书,您可能需要告知 Python 该证书的存放位置。
export SSL_CERT_FILE="/path/to/my.crt"
export REQUESTS_CA_BUNDLE="/path/to/my.crt"
2:安装最新版本的 SDK¶
运行以下命令安装最新版本的 SDK,并将所有 < > 替换为应用程序专属的值(可在应用程序概览页面找到)。
pip install <您的包名> --upgrade --extra-index-url "https://:$FOUNDRY_TOKEN@<索引URL>"
开发前端应用程序¶
在您的应用程序中,初始化 Foundry 客户端并开始开发。
import os
from <包名> import FoundryClient
from <包名>.core.api import UserTokenAuth
auth = UserTokenAuth(hostname="<您的Foundry URL>", token=os.environ["FOUNDRY_TOKEN"])
client = FoundryClient(auth=auth, hostname="<您的Foundry URL>")
object = client.ontology.objects.<任意对象>
print(object.take(1))