Single-script apps
The simplest way to build and deploy an app with Flyte is to write everything in a single Python script. This approach is perfect for:
- Quick prototypes: Rapidly test ideas and concepts
- Simple services: Basic HTTP servers, APIs, or dashboards
- Learning: Understanding how Flyte apps work without complexity
- Minimal examples: Demonstrating core functionality
All the code for your app—the application logic, the app environment configuration, and the deployment code—lives in one file. This makes it easy to understand, share, and deploy.
Plain Python HTTP server
The simplest possible app is a plain Python HTTP server using Python’s built-in http.server module. This requires no external dependencies beyond the Flyte SDK.
"""A plain Python HTTP server example - the simplest possible app."""
import flyte
import flyte.app
from pathlib import Path
# Create a simple HTTP server handler
from http.server import HTTPServer, BaseHTTPRequestHandler
class SimpleHandler(BaseHTTPRequestHandler):
"""A simple HTTP server handler."""
def do_GET(self):
if self.path == "/":
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(b"<h1>Hello from Plain Python Server!</h1>")
elif self.path == "/health":
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(b'{"status": ___STRING_10___}')
else:
self.send_response(404)
self.end_headers()
file_name = Path(__file__).name
app_env = flyte.app.AppEnvironment(
name="plain-python-server",
image=flyte.Image.from_debian_base(python_version=(3, 12)),
args=["python", file_name, "--server"],
port=8080,
resources=flyte.Resources(cpu="1", memory="512Mi"),
requires_auth=False,
)
if __name__ == "__main__":
import sys
if "--server" in sys.argv:
server = HTTPServer(("0.0.0.0", 8080), SimpleHandler)
print("Server running on port 8080")
server.serve_forever()
else:
flyte.init_from_config(root_dir=Path(__file__).parent)
app = flyte.serve(app_env)
print(f"App URL: {app.url}")
Key points
- No external dependencies: Uses only Python’s standard library
- Simple handler: Define request handlers as Python classes
- Basic command: Run the server with a simple Python command
- Minimal resources: Requires only basic CPU and memory
Streamlit app
Streamlit makes it easy to build interactive web dashboards. Here’s a complete single-script Streamlit app:
"""A single-script Streamlit app example."""
import pathlib
import streamlit as st
import flyte
import flyte.app
def main():
st.set_page_config(page_title="Simple Streamlit App", page_icon="🚀")
st.title("Hello from Streamlit!")
st.write("This is a simple single-script Streamlit app.")
name = st.text_input("What's your name?", "World")
st.write(f"Hello, {name}!")
if st.button("Click me!"):
st.balloons()
st.success("Button clicked!")
file_name = pathlib.Path(__file__).name
app_env = flyte.app.AppEnvironment(
name="streamlit-single-script",
image=flyte.Image.from_debian_base(python_version=(3, 12)).with_pip_packages(
"streamlit==1.41.1"
),
args=["streamlit", "run", file_name, "--server.port", "8080", "--", "--server"],
port=8080,
resources=flyte.Resources(cpu="1", memory="1Gi"),
requires_auth=False,
)
if __name__ == "__main__":
import sys
if "--server" in sys.argv:
main()
else:
flyte.init_from_config(root_dir=pathlib.Path(__file__).parent)
app = flyte.serve(app_env)
print(f"App URL: {app.url}")
Key points
- Interactive UI: Streamlit provides widgets and visualizations out of the box
- Single file: All UI logic and deployment code in one script
- Simple deployment: Just specify the Streamlit command and port
- Rich ecosystem: Access to Streamlit’s extensive component library
FastAPI app
FastAPI is a modern, fast web framework for building APIs. Here’s a minimal single-script FastAPI app:
"""A single-script FastAPI app example - the simplest FastAPI app."""
from fastapi import FastAPI
import pathlib
import flyte
from flyte.app.extras import FastAPIAppEnvironment
app = FastAPI(
title="Simple FastAPI App",
description="A minimal single-script FastAPI application",
version="1.0.0",
)
@app.get("/")
async def root():
return {"message": "Hello, World!"}
@app.get("/health")
async def health():
return {"status": "healthy"}
app_env = FastAPIAppEnvironment(
name="fastapi-single-script",
app=app,
image=flyte.Image.from_debian_base(python_version=(3, 12)).with_pip_packages(
"fastapi",
"uvicorn",
),
resources=flyte.Resources(cpu=1, memory="512Mi"),
requires_auth=False,
)
if __name__ == "__main__":
flyte.init_from_config(root_dir=pathlib.Path(__file__).parent)
app_deployment = flyte.serve(app_env)
print(f"Deployed: {app_deployment[0].url}")
print(f"API docs: {app_deployment[0].url}/docs")
Key points
- FastAPIAppEnvironment: Automatically configures uvicorn and FastAPI
- Type hints: FastAPI uses Python type hints for automatic validation
- Auto docs: Interactive API documentation at
/docsendpoint - Async support: Built-in support for async/await patterns
Running single-script apps
To run any of these examples:
- Save the script to a file (e.g.,
my_app.py) - Ensure you have a config file (
./.flyte/config.yamlor./config.yaml) - Run the script:
python my_app.pyOr using uv:
uv run my_app.pyThe script will:
- Initialize Flyte from your config
- Deploy the app to your Union/Flyte instance
- Print the app URL
When to use single-script apps
Use single-script apps when:
- Building prototypes or proof-of-concepts
- Creating simple services with minimal logic
- Learning how Flyte apps work
- Sharing complete, runnable examples
- Building demos or tutorials
Consider multi-script apps when:
- Your app grows beyond a few hundred lines
- You need to organize code into modules
- You want to reuse components across apps
- You’re building production applications
See Multi-script apps for examples of organizing apps across multiple files.