Streamlit์ ์ฅ์
1. ๋ค๋ฅธ ์กฐ์ง์ ๋์ ์์ด ๋น ๋ฅด๊ฒ ์น์๋น์ค๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉ ๊ฐ๋ฅ
2. ๋ค์ํ Component๋ฅผ ์ ๊ณตํด ๋์๋ณด๋ UI๋ฅผ ๊ตฌ์ฑํ ์ ์์
Streamlit ์ค์นํ๊ธฐ
pip install streamlit==1.36.0
Streamlit ํ ์คํธ ์ ๋ ฅํ๊ธฐ
import streamlit as st
# Streamlit Text ์์ฑ
st.title("์ ๋ชฉ")
st.header("ํค๋")
st.subheader("์๋ธํค๋")
st.write("๊ทธ๋ฅ ๊ธ์จ")
์ฌ์ฉ์์ ์ ๋ ฅ ๋ฐ๊ธฐ
## ํ
์คํธ ์
๋ ฅ๋ฐ๊ธฐ
text = st.text_input("์ฌ์ฉ์์๊ฒ ๋์ธ ๋ฉ์ธ์ง") ## ex) ์
๋ ฅํด์ฃผ์ธ์
pw = st.text_input("ํจ์ค์๋ ์
๋ ฅ", type="password")
num = st.number_input("์ฌ์ฉ์์๊ฒ ๋์ธ ๋ฉ์ธ์ง") ## ์ซ์ ์
๋ ฅ ๋ฐ๊ธฐ
date = st.date_input("์ฌ์ฉ์์๊ฒ ๋์ธ ๋ฉ์ธ์ง") ## ๋ ์ง ์
๋ ฅ ๋ฐ๊ธฐ
time = st.time_input("์ฌ์ฉ์์๊ฒ ๋์ธ ๋ฉ์ธ์ง") ## ์๊ฐ ์
๋ ฅ ๋ฐ๊ธฐ
st.write(text)
## ํ์ผ ์
๋ก๋ ๋ฐ๊ธฐ
upload_file = st.file_uploader("์ฌ์ฉ์์๊ฒ ๋์ธ ๋ฉ์ธ์ง", type=["png", "jpg", "csv"]) ## ํ์ผ ํ์ฅ์
## ์
๋ ฅ์ ๊ทธ๋ฃนํ ํ์ฌ, ํ ๋ฒ์ ์ ์ถํ๋๋ก
with st.form(key="์
๋ ฅ form"):
id = st.text_input("ID")
pw = st.text_input("password", type="password")
st.form_submit_button("login")
๋ฐ์ค, ๋ฒํผ ๋ฑ ์ ํ ์ต์
# ์ฌ๋ฌ ์ต์
์ค ํ๋๋ง ์ ํ
## ๋ผ๋์ค ๋ฒํผ
selected_item = st.radio("ํ๋๋ฅผ ๊ณจ๋ผ์ฃผ์ธ์.", ("A", "B")) ## ("๋ณด์ฌ์ค ๋ฉ์ธ์ง", (์ ํ์ง))
### ์ ํ์ง์ ๋ฐ๋ผ ํ ํ๋
if selected_item == "A": st.write("A")
else : st.write("B")
### ์ ํ ๋ฐ์ค
sb = st.selectbox("ํ๋๋ฅผ ๊ณจ๋ผ์ฃผ์ธ์.", ("A", "B")) ## ("๋ณด์ฌ์ค ๋ฉ์ธ์ง", (์ ํ์ง))
# ์ฌ๋ฌ ์ต์
์ ํ ๊ฐ๋ฅ
ms = st.multiselect("ํ๋๋ฅผ ๊ณจ๋ผ์ฃผ์ธ์.", ["A", "B"])
# ์ฌ๋ผ์ด๋ : ์ฃผ์ด์ง ๋ฒ์ ๋ด์์ ๊ฐ ์ ํ
sl = st.slider("ํ๋๋ฅผ ๊ณจ๋ผ์ฃผ์ธ์.", ์ต์๊ฐ, ์ต๋๊ฐ, ์ด๊น๊ฐ)
# ์ฒดํฌ๋ฐ์ค : T,F๋ก ์
๋ ฅ์ ๋ฐ๋ ๊ฒฝ์ฐ
check = st.checkbox("๋์ ์, ์ ํํด์ฃผ์ธ์", value=False) ## ๋ณด์ฌ์ค ๋ฉ์ธ์ง, default๊ฐ
if check : st.write("๋์ํ์
จ์ต๋๋ค.") ## ์ฒดํฌ๊ฐ ๋๋ ธ์ ๋, ์คํํ ๊ฒ
# ๋ฒํผ : ํน์ ๋์์ ์คํํ๋๋ก ํ๋ ๊ฒฝ์ฐ
if st.button("๋ฒํผ ํด๋ฆญ"):
## ํด๋ฆญ์, ํ ๋์
st.column_config
: ๋ฐ์ดํฐ ํ์ ๋ฐ ์ํธ์์ฉ์ ๊ตฌ์ฑํ๋ ๊ฐ๋ ฅํ ๋๊ตฌ -> ํ ์คํธ, ์ซ์, ๋ชฉ๋ก, URL, ์ด๋ฏธ์ง ๋ฑ์ ๋ค์ํ ๋ฐ์ดํฐ์ ๋ง๋ ๋ง์ถค ๋ฉ์๋ ๋ชจ์ ์ ๊ณต
# ์ผ๋ฐ column ๊ตฌ์ฑํ๊ธฐ
## width : ์นผ๋ผ ๊ฐ๋กํญ -> "small", "medium", "large" ์ค ํ๋๋ก ์ ํ
## help : ์ฌ์ฉ์๊ฐ ๋ง์ฐ์ค๋ฅผ column ์์ ์ฌ๋ฆฌ๋ฉด, ๋ฌผ์ํ์ ๋งํ์ ์ผ๋ก ํ์๋ ๋ฉ์ธ์ง
Column("์ด ์ด๋ฆ", width="medium", help="")
# text column ๊ตฌ์ฑ
## validate : ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๋ฌธ์์ด์ด ์
๋ ฅํ ์ ๊ท์์ ๋ง์กฑํ๋์ง ํ์ธ
TextColumn("์ด ์ด๋ฆ", max_chars=์ต๋ ๊ธ์์, validate="์ ๊ท์")
# number column ๊ตฌ์ฑ
## format : ์ด๋ค ๋ฌธ์์ด ํํ๋ก ํฌ๋งทํ
ํ ์ง -> ex)"$%d" => $1000 ์์ผ๋ก ํ์
NumberColumn("์ด ์ด๋ฆ", min_value=0, format="format ์คํ์ผ")
# checkbox column ๊ตฌ์ฑ
CheckboxColumn("์ด ์ด๋ฆ", help="")
# selectbox column ๊ตฌ์ฑ
SelectboxColumn("์ด ์ด๋ฆ", options=["์ ํ์ง 1", "์ ํ์ง 2"])
# Multiselect Column ๊ตฌ์ฑ
MultiselectColumn("์ด ์ด๋ฆ", options=["์ ํ์ง 1", "์ ํ์ง 2"])
# Datetime column ๊ตฌ์ฑ
## min_value : ์ต์ ๋ ์ง -> ex) datetime(2023, 6, 1) -> 23๋
6์ 1์ผ ๋ถํฐ ์์
## format : ex) D MMM YYYY, h:mm a
DatetimeColumn("์ด ์ด๋ฆt", min_value=datetime(), format="")
# Date column ๊ตฌ์ฑ
DateColumn("์ด ์ด๋ฆ", max_value=date(), format="")
# Time column ๊ตฌ์ฑ
## min_value : -> ex) time(8, 0, 0)
TimeColumn("์ด ์ด๋ฆ", min_value=time(8, 0, 0), format="")
# JSON column ๊ตฌ์ฑ
JSONColumn("์ด ์ด๋ฆ", width="medium")
# List column ๊ตฌ์ฑ
ListColumn("์ด ์ด๋ฆ", width="medium")
# Link column ๊ตฌ์ฑ
LinkColumn("์ด ์ด๋ฆ", max_chars=100, validate="์ ๊ท์")
# Image column ๊ตฌ์ฑ
ImageColumn("์ด ์ด๋ฆ", help="")
# Area chart column ๊ตฌ์ฑ
AreaChartColumn("์ด ์ด๋ฆ" y_min=0, y_max=100)
# Line chart column ๊ตฌ์ฑ
LineChartColumn("์ด ์ด๋ฆ" y_min=0, y_max=100)
# Bar chart column ๊ตฌ์ฑ
BarChartColumn("์ด ์ด๋ฆ" y_min=0, y_max=100)
# Progress column ๊ตฌ์ฑ
ProgressColumn("์ด ์ด๋ฆ", min_value=0, max_value=1000, format="์ ๊ท์")
Streamlit tutorial : Get dataframe row-selections from users
์ ๋ด์ฉ๋ค์ ์ค์ต ํด๋ณผ ์ ์๋ ํํ ๋ฆฌ์ผ์ด ๊ณต์ ํ์ด์ง์ ์์ต๋๋ค !
์๋์ ํ๊ธ ์ฃผ์์ ์ต๋ํ ๊ผผ๊ผผํ ๋ฌ์๋์์ผ๋ ํ๋ฒ์ฉ ํด๋ณด์ธ์ :)
https://docs.streamlit.io/develop/tutorials/elements/dataframe-row-selections
Streamlit Docs
Join the community Streamlit is more than just a way to make data apps, it's also a community of creators that share their apps and ideas and help each other make their work better. Please come join us on the community forum. We love to hear your questions
docs.streamlit.io


app.py
import numpy as np
import pandas as pd
import streamlit as st
from faker import Faker # ๋ฌด์์๋ก ๋ฉค๋ฒ ์ด๋ฆ์ ์์ฑํด์ผํด์
# ํ์ ๋ฐ์ดํฐ ๋ฌด์์ ์์ฑ (number_of_items : ์์ฑํ ํ์ ์)
@st.cache_data ## ์บ์๋ ํจ์์ ์ถ๋ ฅ์ ์ ์ฅํด, ๋์ผํ ์
๋ ฅ์ผ๋ก ๋ค์ ํธ์ถ์,์ฌ์ฌ์ฉ
def get_profile_dataset(number_of_items: int = 20, seed: int = 0) -> pd.DataFrame:
## ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ธฐ ์ํจ
new_data = []
## ์ด๊ธฐํ
fake = Faker()
np.random.seed(seed)
Faker.seed(seed)
## ๋ฐ๋ณตํ๋ฉด์, ์๋ก์ด ํ์ ๋ฐ์ดํฐ๋ฅผ ์์ฑํ๊ณ new_data ๋ฆฌ์คํธ์ ์ ์ฅ
for i in range(number_of_items):
profile = fake.profile()
new_data.append(
{
"name": profile["name"],
"daily_activity": np.random.rand(25),
"activity": np.random.randint(2, 90, size=12),
}
)
## ์์ฑํ ๋ฐ์ดํฐ๋ฆฌ์คํธ๋ฅผ ๋ฐ์ดํฐํ๋ ์์ผ๋ก ๋ฐ๊ฟ์ค
profile_df = pd.DataFrame(new_data)
return profile_df
# ๋ค์ค ์ด ์ ํ์ด ๊ฐ๋ฅํ ๋ฐ์ดํฐ display
## ์นผ๋ผ ํํ ์ ์
column_configuration = {
"name": st.column_config.TextColumn(
"Name", help="The name of the user", max_chars=100, width="medium"
),
## ์ฐ๊ฐํ๋ -> ๊บพ์์ ๊ทธ๋ํ
"activity": st.column_config.LineChartColumn(
"Activity (1 year)",
help="The user's activity over the last 1 year",
width="large",
y_min=0,
y_max=100,
),
## ์ผ๋ณ ํ๋ -> ๋ง๋ ๊ทธ๋ํ
"daily_activity": st.column_config.BarChartColumn(
"Activity (daily)",
help="The user's activity in the last 25 days",
width="medium",
y_min=0,
y_max=1,
),
}
## ๋ฉค๋ฒ ์ ํ ํญ, ์ ํ ๋ฉค๋ฒ์ ํ๋ ๋น๊ต ํญ 2๊ฐ๋ก ๋ถ๋ฆฌ
select, compare = st.tabs(["๋ฉค๋ฒ ์ ํ", "์ ํ๋ ๋ฉค๋ฒ์ ํ๋ ๋น๊ต"])
with select: # Add select tab #############################################
st.header("All members")
df = get_profile_dataset() ## ๋ฉค๋ฒ ๋ฐ์ดํฐ ๋ฌด์์ ์์ฑํ๋ ํจ์ ํธ์ถ
# ์ ํ๋ ๋ฉค๋ฒ ๋ฐ์ดํฐํ๋ ์ ๋
ธ์ถ
## ์ฌ์ฉ์์ ์ ํ ํญ๋ชฉ์ ์ ์ฅ
event = st.dataframe(
df,
column_config=column_configuration,
use_container_width=True,
hide_index=True,
on_select="rerun", ## ์ฌ์ฉ์๊ฐ ์ ํ ์, ์ฆ์ ์ฑ ์ ์ฒด๋ฅผ ๋ค์ ์คํ
selection_mode="multi-row",
)
st.header("Selected members")
people = event.selection.rows ## ์ ํ๋ ํ์ ๋ชฉ๋ก
filtered_df = df.iloc[people] ## ํํฐ๋ง
## ์ ํ๋ ํ์ ๋ฐ์ดํฐ ํ๋ ์์ ํ์ํจ
st.dataframe(
filtered_df,
column_config=column_configuration,
use_container_width=True,
)
with compare: # Add compare tab ###########################################
# ์ฐ๊ฐ ํ๋ ๋ฐ์ดํฐ ์ ์ฅ
activity_df = {}
for person in people:
activity_df[df.iloc[person]["name"]] = df.iloc[person]["activity"]
activity_df = pd.DataFrame(activity_df)
## ์ผ๊ฐ ํ๋ ๋ฐ์ดํฐ ์ ์ฅ
daily_activity_df = {}
for person in people:
daily_activity_df[df.iloc[person]["name"]] = df.iloc[person]["daily_activity"]
daily_activity_df = pd.DataFrame(daily_activity_df)
## ์ ํ๋ ๋ฉค๋ฒ๊ฐ ์๋ ๊ฒฝ์ฐ
if len(people) > 0:
st.header("Daily activity comparison")
st.bar_chart(daily_activity_df) ## ๋ง๋ ์ฐจํธ
st.header("Yearly activity comparison")
st.line_chart(activity_df) ## ๊บฝ์์ ๊ทธ๋ํ
## ์ ํ๋ ๋ฉค๋ฒ๊ฐ ์๋ ๊ฒฝ์ฐ
else:
st.markdown("No members selected.")
app ์คํ
streamlit run app.py --server.runOnSave=true
reference
https://docs.streamlit.io/develop/api-reference/data/st.column_config
https://docs.streamlit.io/develop/quick-reference/cheat-sheet
'๋์๋ฆฌ,ํํ > Naver AI Tech' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| PyTorch ์๊ฐ (0) | 2025.09.01 |
|---|