Streamlit

Niall McNulty
6 min readJul 2, 2024

Streamlit is revolutionising the way developers build and share interactive web applications.

Using Streamlit’s open-source Python library, programmers can turn data scripts into fully functional web apps in just minutes.

This means data scientists and engineers can quickly create dashboards, generate reports, or even build chat apps without worrying about complex web development tasks.

What makes Streamlit stand out is its simplicity and ease of use.

Users can display and style data, draw charts and maps, and add interactive widgets with minimal coding.

For those looking to share their applications, Streamlit’s Community Cloud platform provides a seamless way to deploy, manage, and share projects with others.

Streamlit fosters a strong community where users can explore and run apps built by fellow developers.

They can learn from the source code of others, share their own projects, and continually discover new features and components.

This collaborative environment encourages learning and innovation, making Streamlit a preferred choice for many Python developers looking to build impressive interactive web apps efficiently.

Getting Started With Streamlit

Streamlit is a powerful tool that allows users to quickly create interactive data apps with just a few lines of Python code.

This guide will help you install Streamlit, set up your first app, and explore basic Streamlit commands and widgets.

Installation

To begin using Streamlit, you first need to install it.

It’s recommended to use pip, the Python package installer.

Open your terminal and run:

pip install streamlit

This command will download and install Streamlit along with its dependencies.

Make sure you have Python installed on your system before running the command. Python version 3.6 or higher is required.

Setting Up Your First Streamlit App

Creating your first Streamlit app is quite simple.

Open your preferred code editor and create a new Python script file, such as app.py.

In this file, you can start by importing the Streamlit library:

import streamlit as st

Next, add some basic UI elements:

st.title("Hello, Streamlit!")
st.write("This is your first Streamlit app.")

Save your script and run the following command in your terminal:

streamlit run app.py

This will launch your app in a new browser window. Your “Hello, Streamlit!” title and introductory text will be displayed.

Streamlit Commands and Widgets

Streamlit comes with several built-in functions and widgets that make creating interactive apps easy.

Text and Display Elements:

  • st.text(): Displays plain text.
  • st.markdown(): Supports Markdown format for more control over text styling.

Input Widgets:

  • st.button(): Adds a button widget.
  • st.slider(): Creates a slider input for numeric data.
  • st.text_input(): Provides a text input field.

Data Display:

  • st.dataframe(): Displays a Pandas DataFrame.
  • st.line_chart(): Plots a line chart using data from a Pandas DataFrame or a NumPy array.

Here is an example of using a slider and displaying its value:

age = st.slider("Select your age:", 0, 100, 25)
st.write("Your age is:", age)

Mixing and matching these commands and widgets allows for the creation of interactive and dynamic apps suited to various data science tasks.

Designing and Layout

Creating a well-designed app with Streamlit involves attention to visual details and effective layout management. The right layout and design choices can make your app more intuitive, functional, and visually appealing for users.

Enhancing App Appearance

Visual appeal is essential for engaging users.

Start by selecting suitable theme colours that provide good contrast and readability.

Streamlit allows users to customise appearances using the st.set_page_config to set titles and themes.

Text sizing and organisation play crucial roles.

Use headers like st.title, st.header, and st.subheader to structure the content.

This makes the interface more navigable and aids in highlighting important sections.

For adding visual data, charts can significantly enhance understanding.

Streamlit supports libraries such as Matplotlib and Altair for drawing charts.

Incorporate these elements to deliver data visually, making the information more accessible and engaging.

Managing Layouts and States

Layouts are foundational to a user’s interaction with your app.

Streamlit offers several layout primitives such as columns, containers, and expanders.

Use st.columns to create side-by-side components, making better use of horizontal space.

The st.container is the fundamental building block for grouping related components. This helps in maintaining an organised structure.

Sidebars are another critical layout feature.

Use st.sidebar to place navigation elements, filters, and other interactive widgets without cluttering the main interface.

Managing app states is crucial for dynamic interaction.

Components like expander allow you to show or hide content dynamically, keeping the app clean and focused.

Data Handling and Machine Learning

Streamlit simplifies working with data and integrating machine learning models. It allows users to create interactive data apps with minimal coding effort.

Working with DataFrames

Streamlit supports DataFrames from popular libraries like Pandas and NumPy.

Users can easily display DataFrames in a clean and interactive table format.

For example, using st.dataframe(df) will render a DataFrame in the app, where df is your DataFrame.

You can also allow users to interact with the data.

For instance, enabling users to filter or sort the rows directly within the app. This makes it easier for users to explore data without needing to write complex code.

Uploading, analysing, and displaying data becomes straightforward.

Streamlit provides upload widgets (st.file_uploader), so users can load files like CSVs directly into DataFrames.

This seamless integration helps streamline data preparation and exploration, crucial steps in data science projects.

Integrating Machine Learning Models

Streamlit offers tools to integrate and visualise machine learning models.

Users can run models and display results within the app.

The st.write function can show model predictions, allowing users to see the outcomes instantly.

Users can also create input widgets like sliders, text inputs, and buttons to adjust model parameters.

This makes it interactive and helps in tuning model performance directly from the app.

For example, you can use st.slider to adjust hyperparameters and observe the effect on predictions.

Visualising model performance is simple.

By using libraries like Matplotlib or Seaborn with Streamlit, users can create plots and charts that display training results, accuracy rates, confusion matrices, and more.

This helps in understanding the effectiveness of the machine learning models in a visual and interactive way.

Streamlit Deployment

Deploying Streamlit apps allows you to share your innovative web applications with a broader audience. You can use GitHub repos or community cloud platforms to streamline this process. Here’s how you can get started.

Sharing Your Apps

Sharing your Streamlit apps is straightforward and can be done in just a few steps.

You can use Streamlit Community Cloud to deploy your app for free.

This platform lets you deploy your app with one click, making it easy to share your work with users around the world.

To start, upload your app code to a GitHub repo.

Make sure your code includes an Apache 2.0 License if you want others to use or modify it.

Once your app is ready, you can link your GitHub repo to Streamlit Community Cloud. This allows you to deploy your app directly from the repository.

Streamlit Deployment Options

There are several options for deploying Streamlit apps, depending on your needs and resources.

One popular approach is using Streamlit Community Cloud. This platform not only simplifies deployment but also provides community cloud resources where you can interact with other developers.

If you prefer more control over the deployment environment, you can use Heroku or Docker.

Hosting multiple Streamlit apps on Heroku with nginx-based authentication is possible. Docker offers another robust solution, especially if you’re looking to containerize your apps for consistent performance across different systems.

These deployment methods ensure your apps are accessible, maintainable, and can scale according to user demands.

Whether you’re sharing a simple app or a complex web application, Streamlit provides the tools you need to deploy effectively.

--

--