Streamlit is an open-source Python framework used to build interactive web applications for data science, machine learning and AI. It provides Python APIs for creating user interfaces, displaying data, visualizing results and accepting user input without requiring separate frontend code. Commonly used for building:
- Data dashboards
- Machine learning applications
- Data visualization tools
- AI and LLM applications
- Interactive analytical applications
Installation
Before using Streamlit, make sure Python and pip are installed on your system. Streamlit can be installed using:
pip install streamlit
To check whether Streamlit is installed correctly, run:
streamlit hello
This starts a sample Streamlit application in the browser.
Creating a Streamlit Application
Streamlit application is generally written in a Python file. The streamlit package is imported using the commonly used alias st.
import streamlit as st
st.title("GeeksforGeeks")
st.write("Welcome to Streamlit!")
Then run the application using this command in bash:
streamlit run file_name.py
Output

Explanation:
- st.title() displays the main heading of the application.
- st.write() displays text and other supported Python objects.
- Streamlit converts these function calls into elements displayed in the browser.
- streamlit run file_name.py starts the streamlit application.
Text Elements in Streamlit
Provides different functions for displaying text and formatted content. The most commonly used text elements include titles, headers, Markdown, caption and plaint text.
Titile
st.title() function is used to display the main title of an application.
Syntax:
st.title(body)
Example:
import streamlit as st
st.title("Student Dashboard")
Output

Explanation:
- st.title() displays the specified text using title formatting.
- It is generally used for the main heading of an application.
Headings
st.header() function is used to create a major section heading.
Syntax:
st.header(body)
Example:
import streamlit as st
st.header("Student Information")
Output

Explanation:
- st.header() displays text using header formatting.
- It is useful for dividing an application into major sections.
Sub-Headings
st.subheader() function creates a smaller heading for a subsection.
Syntax:
st.subheader(body)
Example:
import streamlit as st
st.subheader("Academic Details") code
print("GFG")
Output

Explanation:
- st.subheader() creates a subsection heading.
- It is useful for organizing content below a header.
Write
st.write() function is a general-purpose method for displaying content in a Streamlit application. It can display text, numbers, data structures, DataFrames and supported chart objects.
Syntax:
st.write(*args, **kwargs)
Example:
import streamlit as st
st.write("Welcome to Streamlit")
st.write(100)
Output

Explanation:
- st.write() determines how supported objects should be rendered.
- It can be used for displaying many different types of content.
- More specific functions can be used when a particular display format is required.
Markdown
st.markdown() function displays text using Markdown formatting.
Syntax:
st.markdown(body)
Example:
import streamlit as st
st.markdown("# Streamlit")
st.markdown("**Interactive Python Applications**")
Output

Explanation:
- Markdown syntax can be used to format the displayed content.
- It can be used for headings, emphasis, lists and links.
Captions
st.caption() function displays smaller supporting text.
Syntax:
st.caption(body)
Example:
import streamlit as st
st.caption("Data updated on September 8, 2026")
Output

Explanation:
- st.caption() displays text in a smaller caption style.
- It is useful for descriptions, notes and supporting information.
Code
st.code() function displays code in a formatted code block.
Syntax:
st.code(body, language=None)
Example:
import streamlit as st
code = """
x = 10
y = 20
print(x + y)
"""
st.code(code, language="python")
Output

Explanation:
- st.code() renders the supplied code as a code block.
- Language argument can be used for syntax highlighting.
Displaying Data
Streamlit provides dedicated functions for displaying DataFrames, tables, metrics and other structured data.
Dataframe
st.dataframe() function displays data as an interactive table.
Syntax:
st.dataframe(data)
Example:
import streamlit as st
import pandas as pd
data = {
"Name": ["Aman", "Riya", "Karan"],
"Marks": [85, 92, 78]
}
df = pd.DataFrame(data)
st.dataframe(df)
Output

Explanation:
- A Pandas DataFrame is created using the sample data.
- st.dataframe() renders the DataFrame as an interactive table.
- This makes it suitable for exploring tabular data inside the application.
JSON
st.json() function displays a dictionary or JSON-compatible object in a formatted JSON representation.
Syntax:
st.json(body)
Example:
import streamlit as st
data = {
"name": "Aman",
"age": 22,
"skills": ["Python", "SQL"]
}
st.json(data)
Output

Explanation:
- The dictionary is passed directly to st.json().
- Streamlit displays the object in a formatted JSON view.
Creating Charts
Library includes simple chart functions for quickly visualizing data. It also supports visualization libraries such as Matplotlib, Altair, Plotly and PyDeck.
Line Chart
st.line_chart() function creates a line chart.
Syntax:
st.line_chart(data)
Example:
import streamlit as st
import pandas as pd
data = pd.DataFrame({
"Sales": [100, 150, 130, 200]
})
st.line_chart(data)
Output

Explanation:
- Sales values are stored in a Pandas DataFrame.
- st.line_chart() plots the values as a line chart.
- Line charts are useful for showing changes or trends.
Bar Chart
st.bar_chart() function displays data using bars.
Syntax:
st.bar_chart(data)Example:
import streamlit as st
import pandas as pd
data = pd.DataFrame({
"Product": ["A", "B", "C"],
"Sales": [120, 180, 150]
})
st.bar_chart(data, x="Product", y="Sales")
Output

Explanation:
- DataFrame contains product-wise sales.
- x="Product" specifies the categories.
- Chart makes it easier to compare sales between products.
Map
st.map() function displays geographic points on a map. The input data should contain latitude and longitude information.
Syntax:
st.map(data)
Example:
import streamlit as st
import pandas as pd
data = pd.DataFrame({
"lat": [28.6139, 19.0760],
"lon": [77.2090, 72.8777]
})
st.map(data)
Output

Explanation:
- DataFrame contains latitude and longitude columns.
- Each row represents a location.
- st.map() plots these locations on a map.
Input Widgets
Widgets allow users to interact with a Streamlit application. A widget returns a value that can be assigned to a Python variable and used by the application.
Button
st.button() function creates a button that users can click.
Syntax:
st.button(label)
Example:
import streamlit as st
if st.button("Click Me"):
st.write("Button clicked!")
Output

Explanation:
- st.button() creates a clickable button.
- Function returns a Boolean value.
- Message is displayed when the button is clicked.
Checkbox
st.checkbox() function creates a checkbox widget.
Syntax:
st.checkbox(label)
Example:
import streamlit as st
show_data = st.checkbox("Show Data")
if show_data:
st.write("Data is visible.")
Output

Explanation:
- Checkbox allows the user to select or clear an option.
- Returned value is
Truewhen selected andFalseotherwise. - Value can be used in conditional logic.
Radio
st.radio() function allows the user to choose one option from a group.
Syntax
st.radio(label, options)
Example:
import streamlit as st
language = st.radio(
"Choose a language",
["Python", "Java", "C++"]
)
st.write("Selected:", language)
Output

Explanation:
- Supplied options are displayed as radio buttons.
- User can select one option.
- Selected value is returned and stored in
language.
Select Box
st.selectbox() function creates a dropdown selection widget.
Syntax:
st.selectbox(label, options)
Example:
import streamlit as st
city = st.selectbox(
"Choose a city",
["Delhi", "Mumbai", "Bengaluru"]
)
st.write("Selected city:", city)
Output

Explanation:
- Options are displayed in a dropdown menu.
- User selects one option.
- Selected option is returned by st.selectbox().
Multi Select
st.multiselect() function allows users to select multiple options.
Syntax:
st.multiselect(label, options)
Example:
import streamlit as st
skills = st.multiselect(
"Choose your skills",
["Python", "SQL", "Machine Learning", "Power BI"]
)
st.write("Selected Skills:", skills)
Output:

Explanation:
- Multiple options can be selected.
- Selected values are returned as a collection.
- It is useful when more than one choice is allowed.
Numerical Input
st.number_input() function accepts numerical input.
Syntax:
st.number_input(label, min_value, max_value)
Example:
import streamlit as st
age = st.number_input(
"Enter your age",
min_value=1,
max_value=100
)
st.write("Age:", age)
Output:

Explanation:
- Widget accepts a numerical value.
- Minimum and maximum values can be used to restrict the allowed range.
- Entered value can be used in further calculations.
Text Input
st.text_input() function creates a single-line text input field.
Syntax:
st.text_input(label)
Example:
import streamlit as st
name = st.text_input("Enter your name")
if name:
st.write("Hello", name)
Output

Explanation:
- Widget accepts text entered by the user.
- Entered value is stored in
name. - Value can then be used by normal Python code.
Multi Line Text Input
st.text_area() function creates a multi-line text input field.
Syntax:
st.text_area(label)
Example:
import streamlit as st
message = st.text_area("Enter your message")
st.write("Message:", message)
Output

Explanation:
- Unlike st.text_input(), st.text_area() is intended for multiple lines of text.
- Entered text is returned by the widget.
Toggle
st.toggle() function creates a switch that can be turned on or off.
Syntax
st.toggle(label)
Example
import streamlit as st
dark_mode = st.toggle("Dark Mode")
st.write("Enabled:", dark_mode)
Output

Explanation:
- Widget provides an on/off selection.
- It returns a Boolean value representing the current state.
Uploading Files
It also provides st.file_uploader() for accepting files from users. It can be used with applications that process datasets, images and other supported files.
Syntax:
st.file_uploader(label)
Example:
import streamlit as st
import pandas as pd
file = st.file_uploader(
"Upload a CSV file",
type=["csv"]
)
if file is not None:
df = pd.read_csv(file)
st.dataframe(df)
Output

Explanation:
- st.file_uploader() creates a file-selection widget.
- Type argument limits the accepted file types.
- Uploaded CSV can be passed directly to Pandas.
- Resulting DataFrame is displayed using st.dataframe().
Downloading Files
st.download_button() function provides a button that allows users to download data or files from the application.
Syntax:
st.download_button(label, data)
Example:
import streamlit as st
data = "name,marks\nAman,85\nRiya,92"
st.download_button(
"Download CSV",
data,
file_name="students.csv"
)
Output

Explanation:
- Button provides the supplied data as a downloadable file.
- file_name specifies the name used for the downloaded file.
Displaying Images
st.image() function displays an image in a Streamlit application.
Syntax:
st.image(image)
Example:
import streamlit as st
st.image(
"image.png",
caption="Sample Image"
)
Output
You will get an image displayed in your browser.
Explanation:
- Image is loaded from the specified source.
- C
aptionargument provides descriptive text. - Streamlit also provides functions for displaying audio and video content.
Layouts and Containers
Streamlit provides layouts and containers to organize application elements. Common options include columns, containers, tabs, expanders, popovers and sidebars.
Columns
st.columns() function creates side-by-side columns.
Syntax:
st.columns(spec)
Example:
import streamlit as st
col1, col2 = st.columns(2)
with col1:
st.metric("Users", 1200)
with col2:
st.metric("Revenue", "$25K")
Output

Explanation:
- st.columns(2) creates two columns.
- Elements placed inside each column appear side by side.
- Columns are useful for dashboards containing multiple metrics or visualizations.
Sidebar
It provides a separate area for application controls.
Syntax:
st.sidebar.element(...)
Example:
import streamlit as st
st.sidebar.title("Filters")
category = st.sidebar.selectbox(
"Choose Category",
["All", "Python", "AI"]
)
st.write("Selected:", category)
Output

Explanation:
- Streamlit elements can be placed in the sidebar.
- Sidebars are commonly used for filters and navigation controls.
- Selected value can still be used in the main application.
Tabs
st.tabs() function creates multiple tabbed sections
Syntax:
st.tabs(tabs)
Example:
import streamlit as st
tab1, tab2 = st.tabs([
"Overview",
"Details"
])
with tab1:
st.write("Overview")
with tab2:
st.write("Details")
Output

Explanation:
- st.tabs() creates separate labeled tabs.
- Content can be added to each tab using the returned objects.
- Tabs are useful for grouping related information.
Expander
st.expander() function creates a collapsible section.
Syntax:
st.expander(label)
Example:
import streamlit as st
with st.expander("View Details"):
st.write("Additional information is displayed here.")
Output

Explanation:
- Content inside the expander can be opened or collapsed.
- It is useful for secondary information or advanced options.
Container
st.container() function creates a container that can hold multiple elements.
Syntax:
st.container()
Example:
import streamlit as st
container = st.container()
container.write("First element")
container.write("Second element")
Output

Explanation:
- A container groups multiple Streamlit elements.
- Elements can be written into the same container.
Divider
st.divider() function adds a horizontal divider between sections.
Syntax:
st.divider()
Example:
import streamlit as st
st.header("Section 1")
st.write("Content")
st.divider()
st.header("Section 2")
st.write("More Content")
Output

Explanation:
- st.divider() inserts a horizontal line.
- It can visually separate sections of an application.
Caching in Streamlit
Streamlit reruns the script when users interact with widgets. Expensive operations can therefore be repeated. Caching stores the results of suitable functions so that Streamlit can reuse them instead of recomputing them unnecessarily.
Streamlit provides two main caching decorators:
- st.cache_data for functions that return data.
- st.cache_resource for reusable resources such as models or connections.
Syntax
@st.cache_datadef
function():
...
Example:
import streamlit as st
import pandas as pd
@st.cache_data
def load_data():
return pd.read_csv("data.csv")
df = load_data()
st.dataframe(df)
Explanation:
- load_data() function reads the dataset.
- @st.cache_data tells Streamlit to cache the returned data.
- This helps avoid repeating the same expensive data operation during reruns.
st.cache_resource is designed for resources that should be reused, such as machine learning models or database connections.
Syntax
@st.cache_resource
def function():
...
Example:
import streamlit as st
@st.cache_resource
def load_model():
model = load_machine_learning_model()
return model
model = load_model()
Explanation:
- Model is created inside the cached function.
- Streamlit can reuse the resource across reruns.
- This avoids repeatedly initializing expensive resources.
Session State in Streamlit
Reruns the script when the user interacts with a widget. st.session_state allows values to be preserved between these reruns for each user's session.
Syntax
st.session_state["key"]
or
st.session_state.key
Example:
import streamlit as st
if "count" not in st.session_state:
st.session_state.count = 0
if st.button("Increment"):
st.session_state.count += 1
st.write("Count:", st.session_state.count)
Output

Explanation:
- Counter is initialized only when it does not already exist.
- Clicking the button increases the stored value.
- Value remains available when Streamlit reruns the script.
- Session State is maintained separately for each user session.
Creating Multipage Applications
Streamlit supports multipage applications, which allow a larger application to be divided into separate pages. Modern Streamlit applications can define pages using st.Page() and navigation using st.navigation().
Syntax
pages = [
st.Page("page1.py"),
st.Page("page2.py")
]
pg = st.navigation(pages)
pg.run()
Example:
import streamlit as st
pages = [
st.Page("home.py", title="Home"),
st.Page("dashboard.py", title="Dashboard")
]
pg = st.navigation(pages)
pg.run()
Output

Explanation:
- st.Page() defines the pages of the application.
- st.navigation() creates the navigation structure.
- pg.run() runs the selected page.
- Multipage applications are useful for organizing larger Streamlit projects.
Creating a Small Streamlit Application
Combining the Streamlit components discussed above to create a simple Student Performance Dashboard.
The application will:
- Accept the student's name.
- Allow the user to select a subject.
- Accept marks using a slider.
- Display the result using a metric.
- Show a simple chart.
- Organize controls using the sidebar and columns.
Complete Code
import streamlit as st
import pandas as pd
# Title
st.title("Student Performance Dashboard")
# Sidebar inputs
st.sidebar.header("Student Details")
name = st.sidebar.text_input("Enter your name")
subject = st.sidebar.selectbox(
"Select Subject",
["Python", "SQL", "Machine Learning"]
)
marks = st.sidebar.slider(
"Select Marks",
0,
100,
50
)
# Main content
if name:
st.header(f"Welcome, {name}!")
col1, col2 = st.columns(2)
with col1:
st.metric("Selected Marks", marks)
with col2:
if marks >= 50:
st.success("Result: Pass")
else:
st.error("Result: Fail")
st.subheader("Performance")
data = pd.DataFrame({
"Subject": ["Python", "SQL", "Machine Learning"],
"Marks": [marks, 70, 85]
})
st.dataframe(data)
st.subheader("Marks Visualization")
st.bar_chart(
data,
x="Subject",
y="Marks"
)
else:
st.info("Enter your name from the sidebar to view the dashboard.")
Output

Advantages
- Simple development: Applications can be created primarily using Python.
- Interactive: Built-in widgets allow users to interact with application data and logic.
- Data-friendly: Streamlit works directly with DataFrames and common visualization libraries.
- Fast prototyping: Data and ML applications can be developed without creating a separate frontend.
- Built-in optimization: Caching and Session State help handle reruns and application state.