Skip to main content

App

A Targon App is the central object for creating and managing a deployable application. It groups your remote functions together for deployment and defines their runtime environment. It also provides entrypoints for deploying and running your application.

Creating a Targon Instance

You can create a Targon application and register a function to run on remote hardware with just a few lines of code.

First, you instantiate a targon.App, giving it a name for identification and defining a default image for its functions to run in. The targon.Image.debian_slim() method is a built-in helper that provides a minimal Debian Linux environment with Python. You can learn more about customizing images on the targon.Image reference page.

Then, you can decorate any Python function with @app.function() to register it with the app. To run the function, you can use the asynchronous app.run() context manager, which handles the application's lifecycle. Inside this block, you call your function with .remote() to execute it on Targon's infrastructure.

import targon

# Create an App to group our functions and define their environment
app = targon.App(
name="example-app",
image=targon.Image.debian_slim()
)

# Register a function with the app to run it remotely
@app.function()
def hello_world():
print("Hello, World!")

# Run the remote function when this script executes
if __name__ == "__main__":
async def main():
async with app.run():
await hello_world.remote()

import asyncio

asyncio.run(main())

App.__init__

Construct a new App. This is the main entry point for any Targon application.

All App instances require a name for identification and a default image that remote functions will run in.

def __init__(self, name: str, image: _Image, project_name: str = "default"):

Arguments:

NameTypeDescription
namestrThe name of the application.
image_ImageThe default image to use for functions in this app.
project_namestr, optionalThe name of the project this app belongs to.

Example:

import targon

image = targon.Image.debian_slim().pip_install("requests")
app = targon.App(name="my-app", image=image, project_name="my-project")

App.function

This decorator registers a Python function with your App, marking it for remote execution on Targon. Any function decorated with @app.function() will be deployed as part of the app.

def function(
self,
*,
image: Optional[_Image] = None,
resource: str = Compute.CPU_SMALL,
min_replicas: int = 1,
max_replicas: int = 3,
timeout: int = 300,
**kwargs,
):

Arguments:

NameTypeDescription
image_Image, optionalThe image to run the function in. If not provided, the app's default image is used.
resourcestr, optionalHardware tier constant (see targon.Compute). Defaults to Compute.CPU_SMALL.
min_replicasint, optionalMinimum number of warm replicas to keep running. Must be ≥ 0.
max_replicasint, optionalMaximum number of replicas that auto-scaling can create. Must be ≥ 1 and ≥ min_replicas.
timeoutint, optionalMaximum execution time for the function in seconds. Defaults to 300.

Example:

# targon-sdk/examples/llm/vllm_example.py
@app.function(resource="h200-small", max_replicas=1)
@targon.web_server(port=8080)
def serve():
...
tip

Use the targon.Compute constants for better readability, for example resource=Compute.H200_SMALL.

App.run

Run your application in an ephemeral session managed by the Targon runtime. The method is an asynchronous context manager, so it should be used inside asyncio.run() or any other event loop.

async with app.run():
await hello.remote()

Behavior

  • Boots the application, builds images if necessary, and streams logs.
  • Tears down resources when the context exits.
  • Useful for local testing, short-lived jobs, or invoking functions programmatically.

Example

import asyncio

async def main():
async with app.run():
result = await hello_world.remote()
print(result)

asyncio.run(main())
note

The CLI command targon run wraps your local entrypoint with app.run() automatically (see the Run CLI reference).

App.local_entrypoint

Decorate a function to make it a command-line entrypoint for your application.

Local entrypoints are useful for defining code that runs locally to set up and interact with your remote Targon functions. The Targon CLI can parse primitive types like str, int, and float as command-line arguments.

def local_entrypoint(self):

Example:

# targon-sdk/examples/gettin_started/getting_started.py
@app.function()
def hello(message: str) -> dict[str, str]:
return {"message": message, "status": "success"}

@app.local_entrypoint()
def main(message: str):
return hello.remote(message)

You can call the function using targon run directly from the CLI:

targon run my_app.py --some-value 123

App.deploy

Deploy the App and all its registered functions, making them persistently available on the Targon platform.

This method is the programmatic equivalent of running targon deploy <your_script.py> from the command line. When called, it packages your App object and sends it to Targon to build the necessary images and provision infrastructure.

Deployed apps are intended for long-running services. Once deployed, functions can be triggered by webhooks or called by other Targon applications. Unlike app.run(), this method returns as soon as the deployment is complete, and it does not stream logs from remote functions back to your client.

def deploy(self):

Example:

You can create a deployment script to programmatically deploy your app. It's best practice to guard this call with if __name__ == "__main__".

# in deploy_app.py
import targon

app = targon.App(
name="my-persistent-app",
image=targon.Image.debian_slim()
)

@app.function()
def my_deployed_function():
print("This function is part of a deployed service!")

if __name__ == "__main__":
app.deploy()
print(f"App deployed successfully with ID: {app.app_id}")
# app.app_id is set after deployment

You can then deploy your app by running the script from your terminal:

python deploy_app.py

Which will output something like:

App deployed successfully with ID: app-abc123