Streamlit: Beyond Data Science Dashboards

· 5 min read
Streamlit: Beyond Data Science Dashboards

Streamlit is an open-source Python framework for creating web apps for machine learning and data science. Without having to learn any front-end technologies or frameworks, it enables you to create apps in the same manner that Python scripts are created. Streamlit apps are responsive, interactive, and simple to share and use.

Streamlit can be used to develop practical tools that go beyond data exploration and visualisation, although it is frequently used to build data science dashboards and applications. In this article, we'll look at a few of Streamlit's features that make it a potent tool for creating web applications across a range of uses and industries.


Streamlit's Capabilities

💡
Streamlit offers many features and capabilities that make it a versatile and powerful tool for building web apps.

Here are some of them:

Widgets

Streamlit offers a number of widgets, including sliders, buttons, checkboxes, radio buttons, choose boxes, multiselects, file uploaders, colour pickers, date pickers, etc., that may be used to build interactive and dynamic apps. Widgets can be used to record user inputs, manage programme parameters, launch processes, or show results.

Charts

Streamlit supports a number of charting libraries, including Altair, Bokeh, Plotly, Matplotlib, PyDeck, etc., that may be used to produce stunning and interactive data visualisations.

Maps

Streamlit integrates with PyDeck and Deck.GL to create stunning and interactive maps that can display geospatial data, such as points, lines, polygons, heatmaps, etc. Maps can be used to visualize and analyse spatial distributions, clusters, routes, or regions.

Media

Streamlit can display various types of media files that can enhance the app experience, such as images, audio, video, etc. Media files can be uploaded by the user or loaded from a URL or a local file. Media files can also be manipulated or processed using libraries such as OpenCV or PIL.

Components

Streamlit Components allow you to extend the functionality of Streamlit by creating custom widgets or embedding any web content into your app. Components can be written in HTML, CSS, JavaScript, React, or any other web technology. Components can also communicate with Streamlit via bi-directional messaging.

Session State

Streamlit Session State allows you to persist and modify data across reruns of your app. Session State can be used to store user inputs, app parameters, intermediate results, or any other data that you want to keep between reruns. Session State can also be used to implement complex logic or functionality that requires state management.

🟩
Components and Session State are the tools that will alow us to expand our apps further

Streamlit vs. Other Python GUI Libraries

Graphical user interfaces (GUIs) may be made using a variety of Python packages, including Tkinter, PyQt, wxPython, Kivy, PySimpleGUI, etc. However, the most of them need a substantial amount of boilerplate code, complex logic, and event-driven programming expertise. They also have drawbacks in terms of aesthetics, scalability, and cross-platform compatibility.

On the other hand, Streamlit makes the process of developing GUIs simpler by employing a declarative syntax that automatically manages the app's layout, state, and interactions. Streamlit also makes use of web technologies to build responsive, modern-looking apps that work with any device and browser. Streamlit Community Cloud and other hosting options make it simple to deploy Streamlit programs to the cloud.

Let's examine how to develop a basic app that accepts user input and shows it on the screen in order to demonstrate the differences between Streamlit and other Python GUI tools.

Using Tkinter:

import tkinter as tk

# Create a window
window = tk.Tk()
window.title("Tkinter App")

# Create a label
label = tk.Label(window, text="Enter something:")
label.pack()

# Create an entry
entry = tk.Entry(window)
entry.pack()

# Create a button
button = tk.Button(window, text="Submit")

# Define a callback function
def submit():
    # Get the entry value
    value = entry.get()
    # Create a new label with the value
    new_label = tk.Label(window, text=value)
    # Display the new label
    new_label.pack()

# Bind the button to the callback function
button.config(command=submit)
button.pack()

# Start the main loop
window.mainloop()

Using Streamlit:

import streamlit as st

# Create a title
st.title("Streamlit App")

# Create an input
value = st.text_input("Enter something:")

# Display the input
st.write(value)

As you can see, Streamlit requires much less code and complexity than Tkinter to create a similar app. Streamlit also automatically updates the app whenever the user changes the input, without the need to define any callback functions or event handlers.

Streamlit vs. Windows Forms Application Development

Using .NET technologies, Windows Forms is a framework for developing desktop applications. It offers a comprehensive selection of controls and elements for creating user interfaces, including buttons, text boxes, menus, dialogue boxes, and more. Applications for Windows Forms can be created in a variety of languages, including C# and Visual Basic.

On the other hand, Streamlit may produce web applications that can function on any operating system that supports Python and a web browser. Regarding deployment possibilities and speed of development, Streamlit is far superior than Windows Forms.

While Streamlit cannot make standalone executable applications, the functionality of a desktop application can be mimicked by using Streamlit as a front end handler that calls on external scripts to execute backend functions, more on that later.

Let's examine how to make a straightforward app that accepts two integers as inputs and shows their total on the screen to demonstrate the differences between Streamlit and Windows Forms application development.

Using Windows Forms

using System;
using System.Windows.Forms;

namespace WindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Get the input values
            int num1 = int.Parse(textBox1.Text);
            int num2 = int.Parse(textBox2.Text);
            // Calculate the sum
            int sum = num1 + num2;
            // Display the sum
            label3.Text = sum.ToString();
        }
    }
}

Using Streamlit

import streamlit as st

# Create a title
st.title("Streamlit App")

# Create two inputs
num1 = st.number_input("Enter first number:")
num2 = st.number_input("Enter second number:")

# Calculate the sum
sum = num1 + num2

# Display the sum
st.write(sum)

Breaking the Data Science Ceiling

As mentioned before, the use of session state and community components within a Streamlit app can expand the functionality beyond just a data science dashboard.

Session State: Managing Data Across Reruns

One of the challenges of creating functional applications in Streamlit is how to handle data across reruns of the app. Since Streamlit apps are essentially scripts that run from top to bottom every time the user interacts with the app, any variables that are defined or modified in the script will be reset on every rerun. This can cause problems when we want to implement features such as authentication, caching, counters, timers, etc.

Therefore, Streamlit introduced Session State in version 0.84.0. Session State allows us to store and modify data across reruns of our app using a simple API. Session State is essentially a dictionary-like object that is unique for each user session and persists until the user closes the browser tab or window.

The use of Session State unlocks advanced functionality that can be deployed into and app providing a fundamental key in the development of useful tools.

See more here: https://docs.streamlit.io/library/api-reference/session-state

Community Components: Extending Streamlit Functionality

Community Components is another Streamlit feature that gives us the ability to build sophisticated and useful apps. Community Components are third-party modules that expand the range of what Streamlit is capable of. These components takes Streamlit and treats it more like a GUI framework than a fully-fledged library.

There are many Community Components available:

Ag Grid: A powerful data grid component that supports sorting, filtering, grouping, editing, etc.

Awesome Streamlit Components: A collection of components for various purposes, such as authentication, file download, notification, etc.

Streamlit Ace: A code editor component that supports syntax highlighting, auto-completion, themes, etc.

Streamlit Webrtc: A component that enables real-time communication of audio and video via WebRTC.


If you are interested in Streamlit and want to learn more, we suggest checking out the following resources:

Streamlit Website: The official website of Streamlit, where you can find more information, tutorials, and examples.

Streamlit Docs: The comprehensive documentation of Streamlit, where you can find the API reference, guides, and tips.

Streamlit Forum: The community forum of Streamlit, where you can ask questions, share your work, and get feedback from other Streamlit users and developers.

Streamlit Gallery: The showcase of Streamlit apps, where you can find inspiration and see what others have built with Streamlit.

Streamlit Components: The collection of Streamlit components, where you can find and use third-party modules that extend Streamlit’s functionality.