-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This MR implements the Inventory endpoints as described in the API Design. Specifically, GET endpoints for retrieving all stock (/inventory), specific stock items (/inventory/stock/{stock_id}) and ingredients (/inventory/ingredient/{ingredient_id}). A POST endpoint for creating new stock items (/inventory), ensuring that ingredients exist before allowing stock creation. Basic Streamlit pages have been generated. closes: #10
- Loading branch information
1 parent
e811b5e
commit 7706671
Showing
8 changed files
with
261 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from datetime import datetime | ||
|
||
import requests | ||
import streamlit as st | ||
|
||
|
||
def create_stock_item(payload): | ||
try: | ||
# Post data to FastAPI endpoint | ||
response = requests.post("http://fastapi:8000/inventory", json=payload) | ||
response.raise_for_status() | ||
return response.json(), None | ||
except requests.exceptions.RequestException as e: | ||
return None, str(e) | ||
|
||
|
||
def add_stock_page(): | ||
st.title("Add Stock Item") | ||
|
||
# Define the form for stock creation | ||
with st.form(key="add_stock_form"): | ||
st.header("Stock Information") | ||
|
||
# Input fields for stock data | ||
ingredient_id = st.number_input("Ingredient ID", min_value=1, step=1) | ||
stock_quantity = st.number_input( | ||
"Quantity", min_value=0.0, format="%.2f" | ||
) # Float with 2 decimal places | ||
stock_unit = st.selectbox( | ||
"Unit", ["liter", "deciliter", "centiliter", "milliliter"] | ||
) # Example units | ||
cost = st.number_input( | ||
"Cost per Unit", min_value=0.0, format="%.2f" | ||
) # Float with 2 decimal places | ||
delivery_date = st.date_input( | ||
"Delivery Date", min_value=datetime(2000, 1, 1) | ||
) # Date input | ||
|
||
# Create the stock data payload | ||
payload = { | ||
"stock": { | ||
"ingredient_id": ingredient_id, | ||
"unit": stock_unit, | ||
"quantity": stock_quantity, | ||
"cost": cost, | ||
"delivery_date": delivery_date.isoformat(), | ||
# Convert date to ISO format string | ||
} | ||
} | ||
|
||
# Submit button | ||
submit_button = st.form_submit_button(label="Add Stock") | ||
|
||
if submit_button: | ||
result, error = create_stock_item(payload) | ||
if error: | ||
st.error(f"Failed to add stock: {error}") | ||
else: | ||
st.success(f"Stock item added successfully: {result}") | ||
|
||
|
||
if __name__ == "__main__": | ||
add_stock_page() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pandas as pd | ||
import requests | ||
import streamlit as st | ||
|
||
|
||
def display_ingredient_items(): | ||
st.title("Ingredient Items Report") | ||
|
||
ingredient_id = st.number_input("Enter Ingredient ID:", min_value=1, step=1) | ||
|
||
if st.button("Get Ingredient"): | ||
try: | ||
# Fetch ingredient data from the FastAPI endpoint | ||
response = requests.get( | ||
f"http://fastapi:8000/inventory/ingredient/{ingredient_id}" | ||
) | ||
response.raise_for_status() | ||
data = response.json() | ||
|
||
# Extract items from the response data | ||
ingredient_items = data.get("items", []) | ||
df_ingredient = pd.DataFrame(ingredient_items) | ||
|
||
# Check if the DataFrame is empty | ||
if df_ingredient.empty: | ||
st.write(f"No data found for Ingredient ID {ingredient_id}.") | ||
else: | ||
# Ensure 'delivery_date' is treated as a datetime object for sorting | ||
df_ingredient["delivery_date"] = pd.to_datetime( | ||
df_ingredient["delivery_date"] | ||
) | ||
# Sort the DataFrame by 'delivery_date' | ||
df_ingredient_sorted = df_ingredient.sort_values(by="delivery_date") | ||
st.table(df_ingredient_sorted) | ||
|
||
except requests.exceptions.RequestException as e: | ||
st.write("Failed to connect to FastAPI:", e) | ||
|
||
|
||
if __name__ == "__main__": | ||
display_ingredient_items() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pandas as pd | ||
import requests | ||
import streamlit as st | ||
|
||
|
||
def display_stock_items(): | ||
st.title("Stock Items Report") | ||
|
||
try: | ||
# Fetch stock data from the FastAPI endpoint | ||
response = requests.get("http://fastapi:8000/inventory/") | ||
response.raise_for_status() | ||
data = response.json() | ||
|
||
# Extract items from the response data | ||
stock_items = data.get("items", []) | ||
df_stock = pd.DataFrame(stock_items) | ||
|
||
# Check if the DataFrame is empty | ||
if df_stock.empty: | ||
st.write("No stock data available.") | ||
else: | ||
st.table(df_stock) | ||
|
||
except requests.exceptions.RequestException as e: | ||
st.write("Failed to connect to FastAPI:", e) | ||
|
||
|
||
if __name__ == "__main__": | ||
display_stock_items() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters