File size: 3,107 Bytes
f5ee066
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319f930
 
 
 
f5ee066
 
ec44c48
f5ee066
 
 
ec44c48
 
 
 
 
 
 
f5ee066
 
 
 
 
 
 
319f930
f5ee066
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import streamlit as st
import pandas as pd

# Initialize an empty DataFrame to hold ingredients
if 'ingredients_data' not in st.session_state:
    st.session_state.ingredients_data = pd.DataFrame(columns=['Ingredient', 'Quantity (grams)', 'Price per kg/unit', 'Type', 'Price'])
if 'ingredient_list' not in st.session_state:
    st.session_state.ingredient_list = []  # List to keep track of ingredient names

# Function to add ingredients
def add_ingredient(ingredient_name, quantity, price_per_kg_unit, type_):
    # Calculate price based on the type of ingredient
    price = (quantity / 1000) * price_per_kg_unit if type_ == "kg" else quantity * price_per_kg_unit
    # Create a new row with the ingredient data
    new_row = pd.DataFrame({
        'Ingredient': [ingredient_name],
        'Quantity (grams)': [quantity],
        'Price per kg/unit': [price_per_kg_unit],
        'Type': [type_],
        'Price': [price]
    })
    # Append new row to ingredients data
    st.session_state.ingredients_data = pd.concat([st.session_state.ingredients_data, new_row], ignore_index=True)
    
    # Update ingredient list for dropdown
    st.session_state.ingredient_list.append(ingredient_name)

# Function to remove ingredients
def remove_ingredient(selected_ingredient):
    # Remove the selected ingredient if any
    if selected_ingredient in st.session_state.ingredients_data['Ingredient'].values:
        st.session_state.ingredients_data = st.session_state.ingredients_data[st.session_state.ingredients_data['Ingredient'] != selected_ingredient]
        # Remove from the ingredient list
        st.session_state.ingredient_list.remove(selected_ingredient)

# Streamlit app
st.title("Recipe Cost Calculator")

# Inputs for adding ingredients
ingredient_name = st.text_input("Ingredient Name")
quantity = st.number_input("Quantity (grams)", min_value=0.0, format="%.3f")
price_per_kg_unit = st.number_input("Price per kg/unit", min_value=0.0, format="%.3f")
type_ = st.radio("Type", options=["kg", "unit"])

# Add Ingredient button
if st.button("Add Ingredient"):
    if ingredient_name and quantity > 0 and price_per_kg_unit >= 0:
        add_ingredient(ingredient_name, quantity, price_per_kg_unit, type_)
    else:
        st.error("Please fill out all fields correctly!")

# Displaying the ingredient table
output_table = st.session_state.ingredients_data.copy()
st.dataframe(output_table)

# Show total price
if not output_table.empty:
    total_price = output_table['Price'].sum()
    st.subheader("Total Price")
    st.write(f"{total_price:.3f} INR")
else:
    st.subheader("Total Price")
    st.write("0.000 INR")

# Dropdown for removing ingredients
if st.session_state.ingredient_list:
    ingredient_to_remove = st.selectbox("Select ingredient to remove", options=st.session_state.ingredient_list)
    # Remove Ingredient button
    if st.button("Remove Ingredient"):
        remove_ingredient(ingredient_to_remove)
        st.success(f"Removed ingredient: {ingredient_to_remove}")
else:
    st.selectbox("Select ingredient to remove", options=["No ingredients to remove"], disabled=True)