A Beginners Guide To Streamlit
发布时间:2026-09-13 | 浏览:2
Python Tutorial
Interview Questions
Streamlit is an open-source Python library for building interactive web apps using only Python. It's ideal for creating dashboards, data-driven web apps, reporting tools and interactive user interfaces without needing HTML, CSS or JavaScript.
This article introduces key Streamlit features, shows how to build a simple app and explains how to run it on a local server using minimal code.
Streamlit Installation
Make sure Python and pip are already installed on your system. To install Streamlit, run the following command in the command prompt or terminal:
pip install streamlit
How to run Streamlit file?
To run a Streamlit app, open the command prompt or Anaconda prompt and type:
streamlit run filename.py
For example, if file name is sample.py, run:
streamlit run sample.py
After running the command, Streamlit starts a local development server and displays a URL (http://localhost:8501). Open this URL in your browser to view and interact with the app.
Understanding Streamlit basic functions
Streamlit offers simple built-in functions to create interactive web apps using Python. These functions help display text, add widgets, visualize data and handle user input.
Let’s explore them one by one.
This basic Streamlit example displays a title on the web page, confirming that Streamlit is set up and running correctly.
2. Header and Subheader
This example demonstrates how to add a header and subheader in a Streamlit app.
It shows how to display plain text in a Streamlit app using st.text() function.
This example uses st.markdown() to show formatted text. The ### creates a level 3 header, used for medium-sized headings in the app.
5. Success, Info, Warning, Error and Exception
This example shows how to display different types of messages and alerts in a Streamlit app using built-in functions like st.success(), st.info(), st.warning(), st.error() and st.exception(). These functions help communicate status, information, warnings, errors and exceptions to users clearly.
This example uses st.write() that can display text, numbers, data structures and even charts. Here, it's used to show plain text and the output of Python’s built-in range() function.
7. Display Images
This example demonstrates how to display an image using Pillow library. The image is opened with Image.open() and displayed with st.image(), where the width parameter controls its size.
This example uses a checkbox in Streamlit to toggle content visibility. When the checkbox labeled "Show/Hide" is checked, it displays a text message on the screen.
9. Radio Button
This example demonstrates how to use radio buttons to let users select one option from a list. Based on the selected gender, the app displays the result using st.success()
10. Selection Box
This example uses a select box in Streamlit to let users choose one option from a dropdown list. The selected item is then displayed on the screen.
11. Multi-Selectbox
This example demonstrates how to use a multiselect box in Streamlit, allowing users to choose multiple options from a list. The app then displays the number of selected items.
This example shows how to use buttons in Streamlit. Buttons can trigger specific actions when clicked, such as displaying a message or running a function.
Text input fields allow users to enter custom data. This example collects a user's name, formats it with proper capitalization and displays it when Submit button is clicked, simulating a basic form interaction.
Sliders provide a way to select numeric values within a range. This example lets users choose a level between 1 and 5 and displays the selected value instantly. It is useful for settings like ratings, difficulty levels or thresholds.
Let’s put everything we've learned so far into practice by building a BMI Calculator web app using Streamlit. The formula to calculate Body Mass Index (BMI) when weight is in kilograms and height is in meters is:
bmi = weight/height 2
After entering all the required fields:
Introduction 1 min read
Input & Output 2 min read
Variables 4 min read
Operators 4 min read
Keywords 2 min read
Data Types 4 min read
Conditional Statements 3 min read
Loops 3 min read
Functions 4 min read
String 4 min read
List 3 min read
Tuples 3 min read
Dictionary 3 min read
Arrays 5 min read
OOP Concepts 3 min read
Exception Handling 5 min read
File Handling 4 min read
Python Database 4 min read
MongoDB 3 min read
MySQL 7 min read
Packages 4 min read
Modules 3 min read
DSA Libraries 5 min read
Python GUI 2 min read
Numpy 3 min read
Pandas 4 min read
Matplotlib 3 min read
Seaborn 3 min read
StatsModel 2 min read
Model Building 6 min read
TensorFlow 2 min read
PyTorch 5 min read
Flask 3 min read
Django 5 min read
Django ORM 4 min read
Jinja2 Templating 5 min read
Django Templates 5 min read
REST API 3 min read
Build API with DRF 4 min read
Quiz 1 min read
Practice Problems 2 min read
Interview Q & A 15+ min read
Placement 360 Course 2 min read
Data Science 360 Course 2 min read
AI Engg Course 2 min read