Serving#
Union lets you build and serve your own web apps, enabling you to build interactive dashboards and other interfaces to interact with and visualize data and models from your workflows, using your favorite Python-based front-end frameworks (Streamlit, Gradio, Tensorboard, FastHTML, Dash, Panel, Voila, FiftyOne).
Warning
Serving on Union is an experimental feature. The API is subject to change.
Example app#
We will start with a simple Streamlit app (other frameworks are available). In this case we will use the default Streamlit “Hello, World!” app.
In a local directory, create the following file:
└── app.py
App declaration#
The file app.py
contains the app declaration:
""" A simple Union app using Streamlit"""
from union.app import App
from union import Resources
app = App(
name="streamlit-hello",
container_image="ghcr.io/thomasjpfan/streamlit-app:0.1.37",
command=[
"streamlit",
"hello",
"--server.port",
"8080",
],
port=8080,
limits=Resources(cpu="2", mem="3Gi"),
)
Here the App
constructor is initialized with the following parameters:
name
: The name of the app. This name will be displayed in app listings (via CLI and UI) and used to refer to the app when deploying and stopping.container_image
: The container image that will be used to for the container that will run the app. Here we use a prebuilt container provided by Union that support Streamlit.command
: The command that will be used within the container to start the app. The individual strings in this array will be concatenated and the invoked as a single command.port
: The port of the app container from which the app will be served.limits
: Aunion.Resources
object defining the resource limits for the app container. The same object is used for the same purpose in the@task
decorator in Union workflows. See The requests and limits settings for details.
The parameters above are the minimum needed to initialize the app.
There are a few additional available parameters that we do not use in this example (but we will cover later):
include
: A list of files to be added to the container at deployment time, containing the custom code that defines the specific functionality of your app.inputs
: AList
ofunion.app.Input
objects. Used to provide default inputs to the app on startup.requests
: Aflytekit.Resources
object defining the resource requests for the app container. The same object is used for the same purpose in the@task
decorator in Union workflows (see The requests and limits settings for details).min_replicas
: The minimum number of replica containers permitted for this app. This defines the lower bound for auto-scaling the app. The default is 0 .max_replicas
: The maximum number of replica containers permitted for this app. This defines the upper bound for auto-scaling the app. The default is 1 .
Deploy the app#
Deploy the app with:
$ union deploy apps APP_FILE APP_NAME
APP_FILE
is the Python file that contains one or more app declarations.APP_NAME
is the name of (one of) the declared apps in APP_FILE. The name of an app is the value of thename
parameter passed into theApp
constructor.
If an app with the name APP_NAME
does not yet exist on the system then this command creates that app and starts it.
If an app by that name already exists then this command stops the app, updates its code and restarts it.
In this case, we do the following:
$ union deploy apps app.py streamlit-hello
This will return output like the following:
✨ Creating Application: streamlit-demo
Created Endpoint at: https://withered--firefly--8ca31.apps.demo.hosted.unionai.cloud/
Click on the displayed endpoint to go to the app:
Viewing deployed apps#
Go to Apps in the left sidebar in Union to see a list of all your deployed apps:
To connect to an app click on its Endpoint. To see more information about the app, click on its Name. This will take you to the App view:
Buttons to Copy Endpoint and Start app are available at the top of the view.
You can also view all apps deployed in your Union instance from the command-line with:
$ union get apps
This will display the app list:
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━┓
┃ Name ┃ Link ┃ Status ┃ Desired State ┃ CPU ┃ Memory ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━╇━━━━━━━━┩
│ streamlit-query-2 │ Click Here │ Started │ Stopped │ 2 │ 2Gi │
│ streamlit-demo-1 │ Click Here │ Started │ Started │ 3 │ 2Gi │
│ streamlit-query-3 │ Click Here │ Started │ Started │ 2 │ 2Gi │
│ streamlit-demo │ Click Here │ Unassigned │ Started │ 2 │ 2Gi │
└─────────────────────────────────────────┴────────────┴────────────┴───────────────┴─────┴────────┘
Stopping apps#
To stop an app from the command-line, perform the following command:
$ union stop apps APP_NAME
APP_NAME
is the name of an app deployed on the Union instance.