Skip to content

Streamlit for creating interactive web apps for data science and machine learning in minutes.

Streamlit, an open-source Python library that allows you to create interactive web apps for data science and machine learning in minutes.

It is popular because it lets you build full web applications using only Python, without needing to know HTML, CSS, or JavaScript.

1. What is Streamlit used for?

Streamlit is primarily used by data scientists and engineers to:

  • Showcase Data: Turn data analysis scripts into shareable web apps.
  • Prototype Quickly: Build a user interface (UI) for a machine learning model to test it out.
  • Create Dashboards: Visualize real-time metrics (sales data, model performance, stock prices).

2. Key Features

  • Pure Python: You write standard Python scripts. There is no separation between “backend” and “frontend.”
  • Interactivity: You can easily add widgets like sliders, text inputs, buttons, and file uploaders.
  • Hot Reloading: When you save your Python file, the app updates instantly in your browser.
  • Compatible: It works seamlessly with libraries you already use, like Pandas, NumPy, Matplotlib, Plotly, and Scikit-learn.

3. How does it work?

Streamlit has a unique execution model. Every time a user interacts with a widget (like clicking a button or moving a slider), Streamlit reruns the entire Python script from top to bottom.

This sounds inefficient, but Streamlit uses caching (@st.cache_data) to skip expensive computations (like loading a large dataset) so the app remains fast.

4. Simple Example

Installation: use

pip install streamlit

Here is what a basic Streamlit app looks like. If you save this as app.py and run it, you get a working web page.

Python

import streamlit as st
import pandas as pd
import numpy as np

# 1. Add a title
st.title('My First Streamlit App')

# 2. Add some text
st.write("Here is a simple chart using random data:")

# 3. Generate random data
data = pd.DataFrame(
    np.random.randn(20, 3),
    columns=['a', 'b', 'c']
)

# 4. Display a line chart
st.line_chart(data)

# 5. Add a widget (Slider)
number = st.slider('Pick a number', 0, 100)
st.write(f'You selected: {number}')

To run this, you would simply type streamlit run app.py in your terminal.

See also  PyTorch function song & examples:ย Autograd, Random Number Generation, Loss Functions, optimization

The output looks something like this

5. Streamlit vs. Others

  • Vs. Dash (Plotly): Dash is more powerful and customizable for enterprise-grade apps but has a steeper learning curve (requires understanding callbacks/HTML structure). Streamlit is faster for prototyping.
  • Vs. Gradio: Gradio is excellent for specifically demonstrating Machine Learning models (input -> model -> output), whereas Streamlit is better for general dashboards and data exploration.

Deployment

The standard open-source version of Streamlit does not have built-in authentication (login screens) or user management out of the box. To restrict access to “internal people,” you must add a security layer. Here are the three most common ways to do it, ranging from easiest to most robust.

1. The “Code-Only” Method: streamlit-authenticator

If you are hosting the app yourself (e.g., on a basic cloud server) and want a quick, Python-only solution, you can use a library called streamlit-authenticator.

This adds a login widget directly to your app sidebar

  • How it works: You store hashed passwords in a configuration file. The app checks these before showing the main content.
  • Pros: Free, no complex server setup, works anywhere Python runs.
  • Cons: You have to manage user credentials manually; less secure than enterprise SSO.

Basic Logic:

Python

import streamlit as st
import streamlit_authenticator as stauth

# (Setup your authenticator object here with names/passwords...)

name, authentication_status, username = authenticator.login('Login', 'main')

if authentication_status:
    st.write(f'Welcome *{name}*')
    # Your internal app code goes here
elif authentication_status == False:
    st.error('Username/password is incorrect')
elif authentication_status == None:
    st.warning('Please enter your username and password')

2. The “Network” Method: VPN or IP Whitelisting

If your company has a Virtual Private Network (VPN) or an internal office network:

  • How it works: You deploy the app on a server that is only accessible from inside your company’s network (Intranet).
  • Pros: Very secure; no login screen needed (if you trust everyone on the network).
  • Cons: Users cannot access it from home unless they are on the company VPN.
See also  How to open a jupyter notebook in Google Drive/Github using Colab

3. The “Cloud Identity” Method (SSO)

This is the professional standard (e.g., “Log in with Google” or “Log in with Microsoft/Okta”).

  • How it works: You put an authentication proxy in front of your Streamlit app. Common tools include:
    • Cloud Provider Tools: AWS Cognito, Google Identity-Aware Proxy (IAP), or Azure Active Directory.
    • Nginx/Apache: Using Basic Auth (a popup asking for user/pass before the site loads).
  • Pros: Extremely secure; integrates with your company’s existing email/login system.
  • Cons: Requires DevOps knowledge to set up the server/proxy.

Comparison Table

MethodBest For…DifficultySecurity Level
Streamlit Community CloudPersonal projects/Small teamsVery EasyMedium (Invites by email)
streamlit-authenticatorQuick internal tools/PrototypesEasyMedium (App level)
VPN / IntranetCorporate Office ToolsMediumHigh (Network level)
SSO / Identity ProxyEnterprise Production AppsHardVery High (Professional)

Leave a Reply

error: Content is protected !!