Displaying Tables
Displaying tables in Streamlit is a crucial aspect of data presentation. Streamlit provides several methods to display tabular data, each with its own use cases and advantages. Understanding these methods will help you present data effectively in your web applications.
Key Concepts
1. st.table()
The st.table()
function is used to display a static table. This function is ideal for small datasets where the data does not change frequently. The table is rendered as a static image, meaning it does not support sorting, filtering, or other interactive features.
2. st.dataframe()
The st.dataframe()
function is used to display a dynamic table. This function is ideal for larger datasets where interactivity is desired. The table supports features like sorting, filtering, and resizing columns, making it more interactive and user-friendly.
3. Pandas Integration
Streamlit seamlessly integrates with Pandas, a powerful data manipulation library in Python. You can directly pass a Pandas DataFrame to st.table()
or st.dataframe()
to display the data. This integration allows you to leverage Pandas' data manipulation capabilities within your Streamlit app.
Examples
Example 1: Using st.table()
import streamlit as st import pandas as pd data = { "Name": ["Alice", "Bob", "Charlie"], "Age": [25, 30, 35], "City": ["New York", "Los Angeles", "Chicago"] } df = pd.DataFrame(data) st.table(df)
Example 2: Using st.dataframe()
import streamlit as st import pandas as pd data = { "Name": ["Alice", "Bob", "Charlie"], "Age": [25, 30, 35], "City": ["New York", "Los Angeles", "Chicago"] } df = pd.DataFrame(data) st.dataframe(df)
Example 3: Pandas Integration
import streamlit as st import pandas as pd data = { "Name": ["Alice", "Bob", "Charlie"], "Age": [25, 30, 35], "City": ["New York", "Los Angeles", "Chicago"] } df = pd.DataFrame(data) df = df.sort_values(by="Age", ascending=False) st.dataframe(df)
By mastering these methods, you can effectively display tabular data in your Streamlit applications, making your data presentation clear, interactive, and user-friendly.