Spaces:
Sleeping
Sleeping
Download app.py from Edisonpaul/bak_sample: direct link, hf CLI and curl.
- Browser
- Download file 3.11 kB
-
https://huggingface.co/spaces/Edisonpaul/bak_sample/resolve/main/app.py
- Command line
-
hf download hf://spaces/Edisonpaul/bak_sample/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Edisonpaul/bak_sample/resolve/main/app.py
3.11 kB
| 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) | |