Run and share Streamlit apps

This example shows you how to run a Streamlit app with modal serve, and then deploy it as a serverless web app.

example streamlit app

This example is structured as two files:

  1. This module, which defines the Modal objects (name the script locally).
  2., which is any Streamlit script to be mounted into the Modal function (download script).
import shlex
import subprocess
from pathlib import Path

import modal

Define container dependencies

The script imports three third-party packages, so we include these in the example’s image definition.

image = modal.Image.debian_slim(python_version="3.11").pip_install(
    "streamlit~=1.35.0", "numpy~=1.26.4", "pandas~=2.2.2"

app = modal.App(name="example-modal-streamlit", image=image)

Mounting the script

We can just mount the script inside the container at a pre-defined path using a Modal Mount.

streamlit_script_local_path = Path(__file__).parent / ""
streamlit_script_remote_path = Path("/root/")

if not streamlit_script_local_path.exists():
    raise RuntimeError(
        " not found! Place the script with your streamlit app in the same directory."

streamlit_script_mount = modal.Mount.from_local_file(

Spawning the Streamlit server

Inside the container, we will run the Streamlit server in a background subprocess using subprocess.Popen. We also expose port 8000 using the @web_server decorator.

def run():
    target = shlex.quote(str(streamlit_script_remote_path))
    cmd = f"streamlit run {target} --server.port 8000 --server.enableCORS=false --server.enableXsrfProtection=false"
    subprocess.Popen(cmd, shell=True)

Iterate and Deploy

While you’re iterating on your screamlit app, you can run it “ephemerally” with modal serve. This will run a local process that watches your files and updates the app if anything changes.

modal serve

Once you’re happy with your changes, you can deploy your application with

modal deploy

If successful, this will print a URL for your app, that you can navigate to from your browser 🎉 .