Data Display Elements in Streamlit
Key Concepts
Streamlit provides several data display elements that allow you to visualize and interact with data directly within your web application. Two of the most commonly used elements are:
- st.dataframe: Displays a DataFrame in a table format with interactive features like sorting and resizing columns.
- st.table: Displays a static table without interactive features.
st.dataframe
st.dataframe
is a powerful tool for displaying Pandas DataFrames in a dynamic and interactive manner. It allows users to sort columns, resize columns, and scroll through large datasets. This element is ideal for exploring and analyzing data directly within your Streamlit app.
Example:
import streamlit as st import pandas as pd st.title("Interactive DataFrame Example") data = { "Name": ["Alice", "Bob", "Charlie"], "Age": [25, 30, 35], "City": ["New York", "Los Angeles", "Chicago"] } df = pd.DataFrame(data) st.dataframe(df)
st.table
st.table
is used to display a static table without any interactive features. This element is useful when you want to present a snapshot of your data without allowing users to manipulate it. It is particularly handy for displaying summary tables or small datasets.
Example:
import streamlit as st import pandas as pd st.title("Static Table Example") data = { "Name": ["Alice", "Bob", "Charlie"], "Age": [25, 30, 35], "City": ["New York", "Los Angeles", "Chicago"] } df = pd.DataFrame(data) st.table(df)
Analogies
Think of st.dataframe
as a dynamic spreadsheet where you can sort, filter, and resize columns to explore your data. On the other hand, st.table
is like a printed table in a book, where the data is presented as-is without any interactive features.
Conclusion
Understanding and using st.dataframe
and st.table
effectively can greatly enhance the user experience of your Streamlit app. By choosing the right element for your data, you can provide users with the tools they need to explore and understand your data more effectively.