Learn Streamlit: Displaying Data with Streamlit

Updated on Jan 04,2024

Learn Streamlit: Displaying Data with Streamlit

Table of Contents

  1. Introduction
  2. What is Streamlit?
  3. Getting Started with Streamlit
  4. Installation
  5. Creating a Streamlit Application
  6. Displaying Data with Streamlit
  7. Using Markdown for Enhanced Text Formatting
  8. Displaying Different Types of Data
  9. Plotting Data with Streamlit
  10. Interactive Data Visualization with Streamlit
  11. Conclusion

Introduction

Welcome to this tutorial on Streamlit! In this article, we will explore the powerful Python library called Streamlit, which enables developers to quickly build interactive web applications without the need for HTML, CSS, or JavaScript. We will cover everything from installation to creating a Streamlit application and displaying various types of data. By the end of this tutorial, You'll be able to confidently use Streamlit to Create your own interactive web applications.

What is Streamlit?

Streamlit is a Python library that simplifies the process of building interactive web applications. It provides a wide range of widgets such as user inputs, sliders, histograms, and data uploaders, allowing developers to easily craft machine learning projects without extensive knowledge of web development technologies. With Streamlit, you can focus on your machine learning algorithms and let the library Take Care of the web interface. It also offers integration with popular plotting libraries like Altair, Plotly, Bokeh, and Matplotlib.

Getting Started with Streamlit

To get started with Streamlit, you first need to install the library on your machine. The installation process is straightforward and can be done using the pip Package manager. However, it is recommended to use a virtual environment to keep your projects clean and independent from each other. You can use pipenv to create virtual environments and manage dependencies.

Installation

To install Streamlit using pip, you can run the following command:

pip install streamlit

However, it is recommended to use pipenv to create a virtual environment for your project. Here's the step-by-step process:

  1. Install pipenv globally on your computer by running the following command:
pip install pipenv
  1. Create an empty project folder and navigate to it using the command line.

  2. Initialize a new virtual environment by running the following command:

pipenv install

This will create a new virtual environment for your project, isolated from your system's global environment.

  1. Install Streamlit and other dependencies by running the following command:
pipenv install streamlit pandas black

This command will install Streamlit along with the Pandas library for data manipulation and Black for code formatting. You can add other dependencies as per your requirements.

  1. Activate the virtual environment by running the following command:
pipenv shell

Now you are ready to start prototyping and developing your Streamlit application.

Creating a Streamlit Application

Once you have Streamlit installed and your virtual environment set up, you can start building your first Streamlit application. You need to create a Python file (e.g., app.py) inside your project folder, which will handle the code of your application. You can structure your project by creating additional folders for storing data, models, and other resources.

To launch your Streamlit application, run the following command inside your project folder:

streamlit run app.py

This will start a local web server and open a web view where you can see your application. Any changes you make to the code will be hot-reloaded, giving you an Instant preview of your application.

Displaying Data with Streamlit

One of the main features of Streamlit is its ability to display data directly in the browser. You can use various methods provided by Streamlit to enhance the styling and formatting of your text data. For example, you can use the markdown method to add headings, paragraphs, lists, and even text formatting like italics and bold.

import streamlit as st

# Display a title
st.title("This is my first application")

# Display a header
st.header("A Machine Learning Application to Detect Types of Flowers")

# Display a subheader
st.subheader("Built by Me")

# Use markdown to add descriptions
st.markdown("This is a description.")
st.markdown("""
    This is a bigger paragraph.
    It can contain multiple lines of text.
""")

# Style portions of the text
st.markdown("_This is an italic text_")
st.markdown("**This is a bold text**")

# Display lists using markdown
st.markdown("""
- First element
- Second element
""")

Streamlit also provides methods to display different types of data, such as info messages, warnings, errors, and JSON responses. You can use the info method to display informative messages, the warning method to Show warnings, and the error method to highlight errors.

# Display an info message
st.info("This is an informational message")

# Display a warning
st.warning("This is a warning")

# Display an error
st.error("Oops! Something went wrong")

If you have a JSON response that you want to display in a formatted way, you can use the st.json method. This will render the JSON response nicely in the browser.

response = {
    "status": 200,
    "text": "Succeeded"
}

# Display a JSON response
st.json(response)

Plotting Data with Streamlit

Streamlit makes it easy to Visualize and plot data directly in your web application. It provides built-in plotting capabilities and also supports integration with popular plotting libraries like Altair, Plotly, Bokeh, and Matplotlib.

You can use the st.plot_chart method to plot data using Streamlit's native plotting capabilities. For example, to plot the distribution of classes in a dataset, you can use the value_counts method of a Pandas DataFrame and pass the resulting values to the st.plot_chart method.

import pandas as pd

# Load the Iris dataset
iris_data = pd.read_csv("data/iris.csv")

# Plot the distribution of classes
class_dist = iris_data["class"].value_counts()
st.plot_chart(class_dist)

Streamlit also supports interactive data visualization using external plotting libraries. You can use the respective methods provided by Streamlit to plot interactive charts using libraries like Altair, Plotly, Bokeh, or Matplotlib.

Interactive Data Visualization with Streamlit

In addition to static plots, you can create interactive visualizations with Streamlit. By using Streamlit widgets, you can add user inputs, sliders, dropdowns, and other interactive components to your web application. These widgets allow users to manipulate the data in real-time and observe the effects of their changes.

To make your Streamlit application interactive, you can use the various widget methods provided by the library. For example, you can use the st.slider method to create a slider widget that allows users to select a range of values.

# Create a slider widget
range_slider = st.slider("Select a range of values", min_value=0, max_value=100, value=(25, 75))

You can also use other widget methods like st.text_input, st.selectbox, and st.button to capture user inputs and perform actions Based on their selections.

Conclusion

In this tutorial, we covered the basics of Streamlit and how you can use it to build interactive web applications for your machine learning projects. We explored the installation process, creating a Streamlit application, displaying various types of data, and plotting data using the library's built-in and external plotting capabilities. We also touched upon the interactive features of Streamlit and how to add user inputs to make your application more engaging.

With Streamlit, you have a powerful tool at your disposal to quickly and easily build web applications without the need for extensive web development knowledge. Experiment with the features of Streamlit and explore its documentation to unleash the full potential of this library. Have fun building your own interactive web applications!

Most people like