Creating Interactive Websites with Streamlit: A Beginner’s Guide

Soumodeep Das
2 min readDec 25, 2023

--

Streamlit web application

In recent years, the demand for user-friendly and interactive web applications has skyrocketed. Luckily, there are several tools available that simplify the process of building such applications, and one of the standout choices is Streamlit.

What is Streamlit?

Streamlit is an open-source Python library designed for rapid development of web applications. It enables developers to create data-centric web apps with minimal effort, using familiar Python scripting. Streamlit’s simplicity lies in its ability to convert Python scripts into web apps, allowing for quick prototyping and deployment.

Getting Started with Streamlit

Installation

To get started with Streamlit, you’ll need to install it using `pip`, Python’s package manager:

!pip install streamlit

Hello World Example

Let’s dive into a simple “Hello, World!” example to get a feel for how Streamlit works:

import streamlit as st
def main():
st.title('Hello, Streamlit!')
st.write("Welcome to your first Streamlit web app.")
if __name__ == "__main__":
main()

Save this code in a file (let’s say `hello_streamlit.py`) and run it using:

streamlit run hello_streamlit.py

If you are using Conda then open CondaPrompt to run this file.

This will launch a local server, and you’ll see your first Streamlit web app running at `http://localhost:8501`.

Building an Interactive Website with Streamlit

Layouts and Widgets

Streamlit provides a wide range of widgets (such as sliders, buttons, text inputs, etc.) and layout options to create an interactive user interface.

import streamlit as st
def main():
st.title('Interactive Streamlit App')

# Add widgets
name = st.text_input("Enter your name:")
button = st.button("Submit")

# Handle user input
if button:
st.write(f"Hello, {name}! Welcome to Streamlit.")
if __name__ == "__main__":
main()

Data Visualization

Streamlit makes it simple to visualize data using popular libraries like Matplotlib, Plotly, and Pandas.

import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
def main():
st.title('Data Visualization with Streamlit')

# Load sample data
data = pd.read_csv("sample_data.csv")

# Display a line chart
st.line_chart(data)
if __name__ == "__main__":
main()

Deployment

Once you’ve developed your Streamlit app locally, deploying it to a web server is straightforward. Streamlit Sharing, for instance, offers free hosting for Streamlit apps.

1. Install the Streamlit Sharing CLI:

!pip install streamlit

2. Deploy your app:

streamlit deploy app_name.py

Conclusion

Streamlit is a powerful and user-friendly tool for creating interactive web applications using Python. Its simplicity, extensive library support, and ease of deployment make it an ideal choice for developers looking to build data-driven websites and applications.

Start exploring Streamlit today and unleash your creativity in crafting engaging and dynamic web experiences!

Happy coding!

--

--

Soumodeep Das
Soumodeep Das

Written by Soumodeep Das

Advance Analytics consultant in Big 4.

No responses yet