FlyteInteractive Vscode Decorator

@vscode converts a Python task into a Visual Studio Code server with just one line change, enabling connection and debugging in remote environments.

from flytekit import task, workflow
from flytekitplugins.flyteinteractive import vscode

Usage

Add @vscode decorator to task function definition

@task
@vscode
def train():
    print("forward")
    print("backward")


@workflow
def wf_train():
    train()

The @vscode decorator, when applied, converts a task into a Visual Studio Code server during runtime. This process overrides the standard execution of the task’s function body, initiating a command to start a Visual Studio Code server instead.

2. Connect to the VSCode server

You can connect in two ways:

  1. (Recommended) Expose a URL on the Flyte console. Set up ingress on the Flyte backend to expose a URL on the Flyte console. Details are to be determined (TBD).

  2. Use port forwarding. To use port forwarding, execute the following command:

    $ kubectl port-forward <pod name> <port>
    

    Then, open a browser and navigate to localhost:<port>, replacing <port> with the port number configured above. You should be presented with the interface shown in the image below.

3. Interactively debug the task

To run the task in VSCode, select “Run and debug” from the left panel and execute the “interactive debugging” configuration.

This will run your task with inputs from the previous task. It’s important to note that the task runs entirely within VSCode and does not write the output to Flyte storage.

For inspecting intermediate states, set breakpoints in the Python code and use the debugger for tracing.

The launch.json file generated by FlyteInteractive offers a convenient method to run the task, but you can still use VSCode as you would locally. For instance, you can run a Python script directly from the embedded terminal using python hello.py.

4. Resume your task with updated code

After you finish debugging, you can resume your task with updated code by executing the “resume task” configuration. This will terminate the code server, run the task with inputs from the previous task, and write the output to Flyte storage.

** Note: Remember to persist your code (e.g. to GitHub) before resuming the task, since you will lose the connection to the VSCode server afterwards.**

Advanced usage

Install extensions

Like local VSCode, you can install a variety of extensions to assist development. Available extensions differ from official VSCode for legal reasons and are hosted on Open VSX Registry.

Python and Jupyter extensions are installed by default. Additional extensions can be added as shown below:

from flytekit import task, workflow
from flytekitplugins.flyteinteractive import COPILOT_EXTENSION, VscodeConfig, vscode

config = VscodeConfig()
config.add_extensions(COPILOT_EXTENSION)  # Use predefined URL
config.add_extensions(
    "https://open-vsx.org/api/vscodevim/vim/1.27.0/file/vscodevim.vim-1.27.0.vsix"
)  # Copy raw URL from Open VSX


@task(container_image="localhost:30000/flytekit-flyteinteractive:0.0.0")
@vscode(config=config)
def t_ext():
    pass


@workflow
def wf_ext():
    t_ext()

Manage resources

To manage resources, FlyteInteractive can terminate pods after a period of idleness (no active HTTP connections). Idleness is monitored via a heartbeat file.

from flytekit import task, workflow
from flytekitplugins.flyteinteractive import vscode


@task
@vscode(max_idle_seconds=60000)  # 60000 seconds
def task_with_max_idle():
    pass


@workflow
def wf_idle():
    task_with_max_idle()

Pre/Post hooks

FlyteInteractive allows execution of functions before and after VSCode starts. This can be used for tasks requiring setup or cleanup.

from flytekit import task, workflow
from flytekitplugins.flyteinteractive import vscode


def set_up_proxy():
    print("set up")


def push_code():
    print("push code")


@task
@vscode(pre_execute=set_up_proxy, post_execute=push_code)
def t_hook():
    pass


@workflow
def wf_hook():
    task_with_max_idle()

Run FlyteInteractive alongside tasks to initiate VSCode after failure

FlyteInteractive can initiate VSCode after task failure, preventing task termination and enabling inspection.

from flytekit import task, workflow
from flytekitplugins.flyteinteractive import vscode


@task
@vscode(run_task_first=True)
def t_exception():
    return 1 // 0  # causes exception


@workflow
def wf_exception():
    task_with_max_idle()

Prebuild a Docker image with VSCode

To skip downloading VSCode and extensions at runtime, they can be prebuilt into a Docker image, accelerating setup.

# Include this line if `curl` isn't installed in the image.
RUN apt-get -y install curl
Download and extract VSCode.
RUN mkdir -p /tmp/code-server
RUN curl -kfL -o /tmp/code-server/code-server-4.18.0-linux-amd64.tar.gz https://github.com/coder/code-server/releases/download/v4.18.0/code-server-4.18.0-linux-amd64.tar.gz
RUN tar -xzf /tmp/code-server/code-server-4.18.0-linux-amd64.tar.gz -C /tmp/code-server/
ENV PATH="/tmp/code-server/code-server-4.18.0-linux-amd64/bin:${PATH}"
# TODO: download and install extensions