Streamlit app
Streamlit is a popular framework for building interactive web applications and dashboards. Flyte makes it easy to deploy Streamlit apps as long-running services.
Basic Streamlit app
The simplest way to deploy a Streamlit app is to use the built-in Streamlit “hello” demo:
import flyte
import flyte.app
image = flyte.Image.from_debian_base(python_version=(3, 12)).with_pip_packages("streamlit==1.41.1")
app_env = flyte.app.AppEnvironment(
name="streamlit-hello",
image=image,
args="streamlit hello --server.port 8080",
port=8080,
resources=flyte.Resources(cpu="1", memory="1Gi"),
requires_auth=False,
)
if __name__ == "__main__":
flyte.init_from_config()
app = flyte.deploy(app_env)
print(f"Deployed app: {app[0].summary_repr()}")
This just serves the built-in Streamlit “hello” demo.
Single-file Streamlit app
For a single-file Streamlit app, you can wrap the app code in a function and use the args parameter to specify the command to run the app.
Note that the command is running the file itself, and uses the --server flag to start the server.
This is useful when you have a relatively small and simple app that you want to deploy as a single file.
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 = 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 logging
import sys
if "--server" in sys.argv:
main()
else:
flyte.init_from_config(
root_dir=Path(__file__).parent,
log_level=logging.DEBUG,
)
app = flyte.serve(app_env)
print(f"App URL: {app.url}")
Note that the if __name__ == "__main__" block is used to both serve the AppEnvironment and run the app code via
the streamlit run command using the --server flag.
Multi-file Streamlit app
When your streamlit application grows more complex, you may want to split your app into multiple files.
For a multi-file Streamlit app, use the include parameter to bundle your app files:
image = flyte.Image.from_debian_base(python_version=(3, 12)).with_pip_packages(
"streamlit==1.41.1",
"pandas==2.2.3",
"numpy==2.2.3",
)
app_env = flyte.app.AppEnvironment(
name="streamlit-multi-file-app",
image=image,
args="streamlit run main.py --server.port 8080",
port=8080,
include=["main.py", "utils.py"], # Include your app files
resources=flyte.Resources(cpu="1", memory="1Gi"),
requires_auth=False,
)
Where your project structure looks like this:
project/
├── main.py # Main Streamlit app
├── utils.py # Utility functions
└── components.py # Reusable componentsYour main.py file would contain your Streamlit app code:
all_columns = ["Apples", "Orange", "Pineapple"]
with st.container(border=True):
columns = st.multiselect("Columns", all_columns, default=all_columns)
all_data = st.cache_data(generate_data)(columns=all_columns, seed=101)
data = all_data[columns]
tab1, tab2 = st.tabs(["Chart", "Dataframe"])
tab1.line_chart(data, height=250)
tab2.dataframe(data, height=250, use_container_width=True)
st.write(f"Environment: {os.environ}")
Example: Data visualization dashboard
Here’s a complete example of a Streamlit dashboard, all in a single file.
Define the streamlit app in the main function:
def main():
st.set_page_config(page_title="Sales Dashboard", page_icon="📊")
st.title("Sales Dashboard")
# Load data
@st.cache_data
def load_data():
return pd.DataFrame({
"date": pd.date_range("2024-01-01", periods=100, freq="D"),
"sales": np.random.randint(1000, 5000, 100),
})
data = load_data()
# Sidebar filters
st.sidebar.header("Filters")
start_date = st.sidebar.date_input("Start date", value=data["date"].min())
end_date = st.sidebar.date_input("End date", value=data["date"].max())
# Filter data
filtered_data = data[
(data["date"] >= pd.Timestamp(start_date)) &
(data["date"] <= pd.Timestamp(end_date))
]
# Display metrics
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Total Sales", f"${filtered_data['sales'].sum():,.0f}")
with col2:
st.metric("Average Sales", f"${filtered_data['sales'].mean():,.0f}")
with col3:
st.metric("Days", len(filtered_data))
# Chart
st.line_chart(filtered_data.set_index("date")["sales"])
Define the AppEnvironment to serve the app:
file_name = Path(__file__).name
app_env = flyte.app.AppEnvironment(
name="sales-dashboard",
image=flyte.Image.from_debian_base(python_version=(3, 12)).with_pip_packages(
"streamlit==1.41.1",
"pandas==2.2.3",
"numpy==2.2.3",
),
args=["streamlit run", file_name, "--server.port", "8080", "--", "--server"],
port=8080,
resources=flyte.Resources(cpu="2", memory="2Gi"),
requires_auth=False,
)
And finally the app serving logic:
if __name__ == "__main__":
import logging
import sys
if "--server" in sys.argv:
main()
else:
flyte.init_from_config(
root_dir=Path(__file__).parent,
log_level=logging.DEBUG,
)
app = flyte.serve(app_env)
print(f"Dashboard URL: {app.url}")
Best practices
- Use
includefor custom apps: Always include your app files when deploying custom Streamlit code - Set the port correctly: Ensure your Streamlit app uses
--server.port 8080(or match yourportsetting) - Cache data: Use
@st.cache_datafor expensive computations to improve performance - Resource sizing: Adjust resources based on your app’s needs (data size, computations)
- Public vs private: Set
requires_auth=Falsefor public dashboards,Truefor internal tools
Troubleshooting
App not loading:
- Verify the port matches (use
--server.port 8080) - Check that all required files are included
- Review container logs for errors
Missing dependencies:
- Ensure all required packages are in your image’s pip packages
- Check that file paths in
includeare correct
Performance issues:
- Increase CPU/memory resources
- Use Streamlit’s caching features (
@st.cache_data,@st.cache_resource) - Optimize data processing