How To: Setup Streamlit App in Data Lab
Overview
Streamlit is an open-source Python framework for building interactive web apps for data science and ML - with no frontend experience needed. You write pure Python and Streamlit handles the UI.
Streamlit apps cannot be developed or edited inline inside Data Lab notebook cells. Streamlit runs as a standalone web server process. Data Lab is used here only to launch and embed the app. All your app code lives in separate .py files.
Steps to Setup Streamlit App
Open a Data Lab Project.
Open a new terminal/command line interface.
Install all the required packages. (e.g., streamlit using pip)
BASHpip install streamlitFrom the Launcher, create a new Python file and name it
app.py. This is the entrypoint for your Streamlit app - all your app logic goes here.PY# app.py - your Streamlit entry point import streamlit as st st.title("My Streamlit App") st.write("Hello from Streamlit!")
You can grow this file into a full app - add charts, filters, file uploads, etc.
In the same directory as
app.py, create a notebook and paste below code into a notebook cell.PYimport time import requests def is_live(url, timeout=30, interval=0.5): start = time.time() while time.time() - start < timeout: try: response = requests.get(url, timeout=2) if response.status_code == 200: return True except (requests.exceptions.ConnectionError, requests.exceptions.Timeout, requests.exceptions.RequestException): pass except Exception as e: print(f"Unexpected error: {e}") return False time.sleep(interval) return FalsePYimport subprocess from IPython.display import IFrame ENTRYPOINT = "app.py" # Change this according to your startup filename PORT = str(spy.utils.get_open_port()) LOG_LEVEL = "ERROR" # DEBUG | INFO | WARNING | ERROR | CRITICAL SERVER_URL = os.getenv("SEEQ_SERVER_URL") LOCAL_URL = f"http://0.0.0.0:{PORT}" SERVER_PROXY_URL = f"{spy.utils.get_data_lab_project_url(False)}/proxy/{PORT}/" proc = subprocess.Popen(["streamlit", "run", ENTRYPOINT, "--logger.level", LOG_LEVEL, "--logger.hideWelcomeMessage", "true", "--server.port", PORT, "--server.headless", "true", "--browser.gatherUsageStats", "false", "--browser.serverAddress", SERVER_URL]) if is_live(LOCAL_URL): display(IFrame(src=SERVER_PROXY_URL, width='100%', height='820px')) else: print("Streamlit did not start within timeout.") proc.terminate()Execute the cells. Once the code is executed, the Streamlit app should run inline within your notebook.
Compatibility Notes
The spy.utils.get_open_port() utility function used in the previous example is available from Spy version 192.16 and above. If your Spy version is lower, utilize the helper function provided below.
def get_open_port() -> int:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', 0))
return s.getsockname()[1]