Integrate Streamlit with Tiger Data
Connect Streamlit to Tiger Cloud or self-hosted TimescaleDB to build interactive Python data apps and dashboards.
Streamlit is an open-source Python framework for turning data scripts into interactive web apps, without requiring any frontend experience.
This page shows you how to connect a Streamlit app to Tiger Data, so that you can query your data and display it in a live app.
In this integration guide, you:
- Install the packages Streamlit needs to connect to your service or database.
- Store your connection details as a Streamlit secret.
- Load data from your service or database into a DataFrame and display it.
Prerequisites for this integration guide
To follow these steps, you'll need:
- A Tiger Cloud service, or a running instance of self-hosted TimescaleDB.
- Your connection details.
- Python 3.9 or later, with
pipinstalled. - Streamlit 1.28 or later, which added the built-in
st.connectionSQL connection.
Connect Streamlit to your service or database
Section titled “Connect Streamlit to your service or database”Streamlit's built-in st.connection handles secrets retrieval, connection setup, and query caching for you, using SQLAlchemy and the psycopg2 driver underneath.
- Install the required packages
In your project's virtual environment, install Streamlit and the PostgreSQL driver:
Terminal window pip install streamlit psycopg2-binary sqlalchemypsycopg2-binaryinstalls thepsycopg2driver as a prebuilt wheel, so you don't need a local PostgreSQL build toolchain. - Store your connection details as a secret
In your project, create
.streamlit/secrets.tomland add your connection details:[connections.tigerdata]dialect = "postgresql"host = "<host>"port = "<port>"database = "<dbname>"username = "<user>"password = "<password>"Tiger Cloud requires an encrypted connection. If your connection fails without it, add
sslmode = "require"to the[connections.tigerdata]section.Add
.streamlit/secrets.tomlto.gitignoreso you don't commit credentials to version control. - Connect and load data into a DataFrame
In your app's Python file, open the connection and query your data:
import streamlit as stconn = st.connection("tigerdata", type="sql")df = conn.query("SELECT * FROM <table_name> LIMIT 10;", ttl="10m")st.dataframe(df)st.connectionreads the[connections.tigerdata]section from your secrets file by matching the name you pass in. Thettlargument caches the query result for 10 minutes; set it to0while you're developing to see every change immediately.
Verify the integration
Section titled “Verify the integration”To confirm Streamlit is working with your service or database:
- Run your app
From your project directory, start the app:
Terminal window streamlit run app.py - Check the rendered data
In the browser tab Streamlit opens, you see the DataFrame from your service or database rendered as a table, confirming the connection works.
You have successfully integrated Streamlit with Tiger Data.
Troubleshooting
Section titled “Troubleshooting”ModuleNotFoundError: No module named 'psycopg2': install thepsycopg2-binarypackage rather thanpsycopg2unless you have PostgreSQL build tools installed locally.- Connection refused or times out: confirm the host and port, and that your network allows outbound connections from where the app runs. For Tiger Cloud, make sure
sslmodeis set torequire. KeyErroron the connection name: the name passed tost.connection(tigerdataabove) must match the[connections.<name>]section insecrets.tomlexactly.
For other connectivity and authentication issues, see Troubleshoot Tiger Cloud integrations.