streamlit_large_dataframe.py
· 1.0 KiB · Python
Неформатований
import streamlit as st
import pandas as pd
import numpy as np
st.write("Got lots of data? Great! Streamlit can show [dataframes](https://docs.streamlit.io/develop/api-reference/data) with hundred thousands of rows, images, sparklines – and even supports editing! ✍️")
num_rows = st.slider("Number of rows", 1, 10000, 500)
np.random.seed(42)
data = []
for i in range(num_rows):
data.append(
{
"Preview": f"https://picsum.photos/400/200?lock={i}",
"Views": np.random.randint(0, 1000),
"Active": np.random.choice([True, False]),
"Category": np.random.choice(["🤖 LLM", "📊 Data", "⚙️ Tool"]),
"Progress": np.random.randint(1, 100),
}
)
data = pd.DataFrame(data)
config = {
"Preview": st.column_config.ImageColumn(),
"Progress": st.column_config.ProgressColumn(),
}
if st.toggle("Enable editing"):
edited_data = st.data_editor(data, column_config=config, use_container_width=True)
else:
st.dataframe(data, column_config=config, use_container_width=True)
| 1 | import streamlit as st |
| 2 | import pandas as pd |
| 3 | import numpy as np |
| 4 | |
| 5 | st.write("Got lots of data? Great! Streamlit can show [dataframes](https://docs.streamlit.io/develop/api-reference/data) with hundred thousands of rows, images, sparklines – and even supports editing! ✍️") |
| 6 | |
| 7 | num_rows = st.slider("Number of rows", 1, 10000, 500) |
| 8 | np.random.seed(42) |
| 9 | data = [] |
| 10 | for i in range(num_rows): |
| 11 | data.append( |
| 12 | { |
| 13 | "Preview": f"https://picsum.photos/400/200?lock={i}", |
| 14 | "Views": np.random.randint(0, 1000), |
| 15 | "Active": np.random.choice([True, False]), |
| 16 | "Category": np.random.choice(["🤖 LLM", "📊 Data", "⚙️ Tool"]), |
| 17 | "Progress": np.random.randint(1, 100), |
| 18 | } |
| 19 | ) |
| 20 | data = pd.DataFrame(data) |
| 21 | |
| 22 | config = { |
| 23 | "Preview": st.column_config.ImageColumn(), |
| 24 | "Progress": st.column_config.ProgressColumn(), |
| 25 | } |
| 26 | |
| 27 | if st.toggle("Enable editing"): |
| 28 | edited_data = st.data_editor(data, column_config=config, use_container_width=True) |
| 29 | else: |
| 30 | st.dataframe(data, column_config=config, use_container_width=True) |
| 31 |