Skip to main content

Streamlit Use Cases

Feature release status badge: PubPrev
PubPrev

There are many use cases that the DataOps development environment optimizes, including building data apps with Streamlit.

info

Examples on this page are using DevReady, the DataOps development environment in your browser.

Rapid development of Streamlit app

This example assumes a Streamlit project at dataops/streamlit/houseprices.

Streamlit itself supports dynamic reloading, so if your app is simply something like app.py, then in a terminal, just run:

streamlit run app.py

This runs the Streamlit app and shows a preview in the DataOps development environment:

file invalid name !!shadow!!

However, many customers will want to render Streamlit apps specific to different branches/environments, so a Streamlit app built in the Dev branch will point to the <DATAOPS_PREFIX>_DEV database.

The simplest way of doing this is to develop your Streamlit app as a Jinja2 template, e.g., app.template.py, and then use the DataOps renderer to render this. You can add a build.sh script to your Streamlit app directory that looks something like this:

#!/bin/bash
# Ensure the environment variables and other environment-specific details are set
/dataops-cde/scripts/dataops_cde_init.sh

# Run the DataOps template renderer to render app.template.py to app.py
/home/gitpod/.pyenv/versions/3.8.13/bin/python /dataops-cde/scripts/dataops-render -o --file $STREAMLIT_FOLDER/houseprices/app.template.py render-template

# Tell the DevReady to expect to be previewing a web application on port 8501
gp preview $(gp url 8501)

# Run the Streamlit app
streamlit run ${STREAMLIT_FOLDER}/houseprices/app.py $STREAMLIT_FOLDER/houseprices/app.template.py --server.port 8501 --server.allowRunOnSave true --server.runOnSave true --browser.gatherUsageStats False

You must change some minor details, like the folder name to make this simple script work.

Now, when you run build.sh, it renders and previews the Streamlit app:

file invalid name !!shadow!!

This means you can work on your Streamlit app, move to the terminal, "Ctrl-C" out of the build.sh, rerun it, and see your new app within seconds.

Good, but we can do better. The problem here is that we are saving app.template.py, and Streamlit is watching app.py for changes. We need something that detects every time app.template.py changes and reruns the render. For this, you can create a simple watch.sh script:

#!/bin/bash
# This watches for changes to the app.template.py and when they are detected, reruns the build.sh
watchfiles "${STREAMLIT_FOLDER}/houseprices/build.sh" $STREAMLIT_FOLDER/houseprices/app.template.py --sigint-timeout 0 --sigkill-timeout 5

You can now run this instead of the build.sh, since it runs build.sh itself, and every time it detects a change to app.template.py, it reruns the build.sh, which rerenders and previews: