Connecting to the Remote Platform
To interact with a Flyte platform, you must first establish a connection using the flyte-sdk client. This tutorial guides you through initializing the connection and using the built-in URL builder to navigate your platform resources.
Prerequisites
Before you begin, ensure you have the following information:
- Your Flyte platform endpoint (e.g.,
dns:///localhost:8090orhttps://flyte.example.com). - The project and domain names you intend to work with (e.g.,
flytesnacksanddevelopment).
Step 1: Initialize the Global Connection
The most common way to connect to Flyte is by using the global init function. This sets up a singleton client that can be accessed throughout your application.
from flyte import init
# Initialize connection to a local Flyte sandbox
init(endpoint="dns:///localhost:8090", insecure=True)
The init function in flyte-sdk handles authentication and service discovery. If you are connecting to a production environment with SSL, set insecure=False.
Step 2: Access the Client
Once initialized, you can retrieve the active ClientSet instance using get_client. This object provides access to various Flyte services like tasks, runs, and apps.
from flyte import get_client
client = get_client()
print(f"Connected to: {client.endpoint}")
The ClientSet class (found in src/flyte/remote/_client/controlplane.py) aggregates multiple service clients, such as run_service and task_service, which you can use for programmatic platform operations.
Step 3: Programmatic Initialization (Optional)
If you prefer not to use a global singleton, you can create a ClientSet instance directly using the for_endpoint class method. Note that this method is asynchronous.
import asyncio
from flyte.remote._client.controlplane import ClientSet
async def setup_client():
client = await ClientSet.for_endpoint(
endpoint="https://flyte.example.com",
insecure=False
)
return client
client = asyncio.run(setup_client())
Step 4: Generate Console URLs
The ClientSet includes a Console instance (accessible via client.console) that builds deep links to the Flyte Console UI. This is useful for logging or reporting where a specific task or run can be viewed in the browser.
# Generate a URL for a specific task
task_url = client.console.task_url(
project="flytesnacks",
domain="development",
task_name="core.control_flow.merge_sort.merge"
)
# Generate a URL for a specific execution (run)
run_url = client.console.run_url(
project="flytesnacks",
domain="development",
run_name="f0a1b2c3d4e5f6g7"
)
print(f"Task UI: {task_url}")
print(f"Run UI: {run_url}")
The Console class (defined in src/flyte/remote/_client/controlplane.py) supports building URLs for:
- Tasks:
task_url(project, domain, task_name) - Runs:
run_url(project, domain, run_name) - Apps:
app_url(project, domain, app_name) - Triggers:
trigger_url(project, domain, task_name, trigger_name)
Step 5: Understand Localhost Port Mapping
When you connect to a local Flyte sandbox, the gRPC API typically runs on port 8090, while the HTTP Console UI runs on port 8080. The Console class in flyte-sdk automatically detects this scenario and adjusts generated URLs accordingly.
If your endpoint is localhost:8090, the Console builder will automatically map the domain to localhost:8080 for all generated links, ensuring they open correctly in your browser.
Complete Example
Here is a complete script that initializes the connection and prints a link to a deployed task:
from flyte import init, get_client
def main():
# 1. Connect to the platform
init(endpoint="dns:///localhost:8090", insecure=True)
# 2. Get the client
client = get_client()
# 3. Build a resource URL
url = client.console.task_url(
project="flytesnacks",
domain="development",
task_name="core.control_flow.merge_sort.merge"
)
print(f"View your task at: {url}")
if __name__ == "__main__":
main()
By following these steps, you have successfully established a connection to Flyte and learned how to navigate its resources using flyte-sdk. You can now use the client to trigger executions or manage platform entities.