Build an Interactive Python Web App with Streamlit

A brief guide to building and deploying an interactive Python web app on Streamlit Cloud.

What is Streamlit?

Streamlit is an open-source Python library that allows you to create and share interactive web apps and data visualisations in Python with ease. You create web apps using Python code, but with powerful add-on capabilities by Streamlit. It includes built-in support for several data visualisation libraries like matplotlib, pandas, and plotly, making it easy to create interactive charts and graphs that update in real-time based on user input. Streamlit is quite popular among data scientists, machine learning (ML) engineers, and developers looking to create and share interactive web apps with their audience.

Due to its rising popularity in the ML and data science communities, Streamlit was acquired by Snowflake in March 2022. Just before they got acquired, Streamlit announced the launch of their cloud-hosted service. Streamlit Cloud is a platform that enables you to deploy, manage, and share Streamlit apps easily. It provides analytics and usage metrics for your app, which also updates instantly when you push code changes to your connected git repository. The base tier allows one private app and unlimited public apps to be hosted for free. In this guide, we'll deploy a simple stock quote app on Streamlit Cloud, as well as Railway.


Deploy a Simple Python App on Streamlit

This tutorial uses the Tradier API to retrieve stock quote information. Sign up for an account with the Tradier Developer portal, and generate an API token. If you are familiar with, or have access to, other financial data providers, feel free to use your preferred service instead.

Getting started with Streamlit is quite easy - create an account on Streamlit Cloud, fork my sample app GitHub repository, connect your Streamlit account to GitHub, and deploy your app using the forked repository in a few clicks. In fact, if you just want to create a simple Hello World app, all you need are two files - and requirements.txt. The former would have the following lines of code, while the latter would just include streamlit as a dependency.

import streamlit as st

st.write('Hello World!')

I'll assume that you have forked my repository. I'm using the tradier_python library to retrieve the stock data. Here's my file.

import os, streamlit as st
from tradier_python import TradierAPI

# Authenticate with the Tradier API
access_token = os.environ["TRADIER_TOKEN"]
tradier = TradierAPI(access_token, "sandbox")

# Define a function to get stock quotes
def get_stock_quote(symbol):
    quotes = tradier.get_quotes(symbol)
    for quote in quotes:
        if quote.symbol == symbol:
            return quote.last
    return None

# Set up the Streamlit app
st.title("Stocks App")
symbol = st.text_input("Enter a stock symbol", "AAPL")
if st.button("Get Quote"):
    quote = get_stock_quote(symbol)
    st.write(f"The last price for {symbol} is {quote}")

Connect to your GitHub account, and authorise access to Streamlit. From your Streamlit Cloud account, click New app to create a Streamlit app.

Streamlit app dashboard
Streamlit app dashboard

Select the main branch of the forked repository, and the default main file, and click Deploy.

Deploy a Streamlit app from GitHub
Deploy a Streamlit app from GitHub

While the app is building, head over to Settings and add a TRADIER_TOKEN environment variable along with the corresponding value as an encrypted secret. You can also click the Advanced settings option in the previous screen and specify the environment variable.

Configure Streamlit app secrets
Configure Streamlit app secrets

You may see the Your app is in the oven message for a while, but your app should get deployed to a *** domain shortly. Specify the stock symbol (e.g. AAPL) and click Get Quote to retrieve the stock quote information. Voila! You have now deployed a fairly simple yet interactive app using Streamlit.

A simple stock quote app deployed on Streamlit
A simple stock quote app deployed on Streamlit

Instead of Streamlit Cloud, you can also deploy the app to other web hosting platforms of your choice. In the next section, I'll deploy the same app on Railway.

Deploy the Streamlit App on Railway

Railway is a modern app hosting platform that makes it easy to deploy production-ready apps quickly. Railway offers persistent database services for PostgreSQL, MySQL, MongoDB, and Redis, as well as application services with a GitHub repository as the deployment source. For the latter, Railway can automatically determine the application runtime and deploy the service. Since we are just testing the waters, Railway's free tier should be sufficient to host the service.

Sign up for an account with Railway using GitHub, and click Authorize Railway App when redirected. Review and agree to Railway's Terms of Service and Fair Use Policy if prompted. Launch the Streamlit one-click starter template (or click the button below) to deploy Streamlit instantly on Railway.

Deploy on Railway

You'll be given an opportunity to change the default repository name and set it private, if you'd like. Provide the access token for Tradier API and click Deploy; the deployment will kick off immediately.

Deploy Streamlit using one-click starter on Railway
Deploy Streamlit using one-click starter on Railway

Once the deployment completes, the Streamlit app will be available at a default domain - launch this URL to access the app. If you are interested in setting up a custom domain, I covered it at length in a previous post - see the final section here.

Update: Retrieve Stock Quote using Yahoo Finance API

If you prefer to use Yahoo Finance API instead of Tradier API, see this repository for a sample Streamlit web app. And launch this Streamlit one-click starter template (or click the button below) to deploy Streamlit instantly on Railway.

Deploy on Railway

Subscribe to alphasec

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.