To upload files, please first save the app
import streamlit as st
import pandas as pd
# 과일 목록 및 가격 사전 설정
FRUITS = {
"Apple": 1.50,
"Banana": 0.75,
"Orange": 1.25,
"Grapes": 2.00,
"Mango": 2.50,
"Pineapple": 3.00
}
def initialize_orders():
"""세션 상태에서 주문 목록을 초기화"""
if "orders" not in st.session_state:
st.session_state.orders = []
def add_product():
"""상품 추가 UI 및 주문 목록에 추가하는 기능"""
st.write("### Add Product")
fruit = st.selectbox("Select a Fruit", list(FRUITS.keys()))
price = FRUITS[fruit] # 과일 가격 자동 설정
quantity = st.number_input("Quantity", min_value=1, value=1, step=1) # 개수 설정
priority = st.selectbox("Priority", ["high", "medium", "low"])
st.write(f"**Price per unit: ${price:.2f}**") # 선택한 과일 가격 표시
st.write(f"**Total Price: ${price * quantity:.2f}**") # 개수에 따른 총 가격 표시
if st.button("Add Product"):
st.session_state.orders.append({"Product": fruit, "Price": price,
"Quantity": quantity, "Total": price * quantity, "Priority": priority})
def display_orders():
"""총 가격 및 주문 목록을 표시하는 기능"""
st.write("### Total Price: $", f"{sum([item['Total'] for item in st.session_state.orders]):.2f}")
st.write("### Order Summary")
if st.session_state.orders:
priority_order = {"high": 1, "medium": 2, "low": 3}
df = pd.DataFrame(st.session_state.orders)
df = df.sort_values(by="Priority", key=lambda x: x.map(priority_order))
for i, row in df.iterrows():
col1, col2, col3, col4, col5, col6 = st.columns([3, 1, 1, 1, 2, 1])
col1.write(row["Product"])
col2.write(f"${row['Price']:.2f}")
col3.write(row["Quantity"])
col4.write(f"${row['Total']:.2f}")
col5.write(row["Priority"].capitalize())
if col6.button("❌", key=f"del_{i}"):
st.session_state.orders.pop(i)
st.rerun()
def main():
"""Streamlit 메인 함수"""
st.title("Fruit Order Management System")
initialize_orders()
col_input, col_table = st.columns([2, 5])
with col_input:
add_product()
with col_table:
display_orders()
if __name__ == "__main__":
main()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?