AnnasBlackHat commited on
Commit
dc19751
1 Parent(s): 2e71512

finish request api

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -18,6 +18,12 @@ def make_api_request(product_name, outlet_name):
18
  st.error(f"Error: {response.status_code}")
19
  return None
20
 
 
 
 
 
 
 
21
  # Streamlit app
22
  st.title("Stock Movement Data")
23
 
@@ -36,23 +42,27 @@ if st.button("Apply"):
36
  if api_data:
37
  # Convert API response to DataFrame
38
  df = pd.json_normalize(api_data["data"])
 
 
 
 
39
 
40
  # Group by stock_price_id and aggregate details
41
  grouped_data = df.groupby("stock_price_id").agg(
42
  {"product_name": "first", "outlet_name": "first", "price": "first",
43
- "stock_in": "sum", "stock_out_total": "first", "stock_in_source": "first",
44
- "stock_in_id": "first"}
45
  ).reset_index()
46
 
47
  # Iterate through groups and display main and detail tables
48
  for index, row in grouped_data.iterrows():
49
  # Display main table
50
- st.table(pd.DataFrame([row], columns=["product_name", "outlet_name", "stock_in_source", "stock_in_id", "price", "stock_in", "stock_out_total"]))
51
 
52
  # Display detail table with specific columns if the checkbox is checked
53
  if show_detail:
54
  detail_data = df[df["stock_price_id"] == row["stock_price_id"]]
55
- detail_table = detail_data[["stock_out", "stock_out_source", "stock_out_id"]]
56
 
57
  # Use HTML to set margin-bottom to zero for the second table
58
  st.markdown(f"""<style>
@@ -63,3 +73,7 @@ if st.button("Apply"):
63
 
64
  st.table(detail_table)
65
  st.write("------")
 
 
 
 
 
18
  st.error(f"Error: {response.status_code}")
19
  return None
20
 
21
+ # Function to format date
22
+ def format_date(date_str):
23
+ # Convert date to datetime, add 7 hours, and format it
24
+ formatted_date = pd.to_datetime(date_str, unit='ms') + pd.to_timedelta("7 hours")
25
+ return formatted_date.strftime("%d/%m/%Y %H:%M")
26
+
27
  # Streamlit app
28
  st.title("Stock Movement Data")
29
 
 
42
  if api_data:
43
  # Convert API response to DataFrame
44
  df = pd.json_normalize(api_data["data"])
45
+ df["stock in"] = df["stock_in_source"] + " #" + df["stock_in_id"].astype(str)
46
+ df["stock out"] = df["stock_out_source"] + " #" + df["stock_out_id"].astype(str)
47
+ df["date_in"] = df["created_at_in"].apply(format_date)
48
+ df["date_out"] = df["created_at"].apply(format_date)
49
 
50
  # Group by stock_price_id and aggregate details
51
  grouped_data = df.groupby("stock_price_id").agg(
52
  {"product_name": "first", "outlet_name": "first", "price": "first",
53
+ "stock_in": "first", "stock_out_total": "first", "stock_in_source": "first",
54
+ "stock_in_id": "first", "stock in": "first", "date_in":"first"}
55
  ).reset_index()
56
 
57
  # Iterate through groups and display main and detail tables
58
  for index, row in grouped_data.iterrows():
59
  # Display main table
60
+ st.table(pd.DataFrame([row], columns=["stock in", "price", "stock_in", "stock_out_total", "date_in"]))
61
 
62
  # Display detail table with specific columns if the checkbox is checked
63
  if show_detail:
64
  detail_data = df[df["stock_price_id"] == row["stock_price_id"]]
65
+ detail_table = detail_data[["stock out", "stock_out", "date_out"]]
66
 
67
  # Use HTML to set margin-bottom to zero for the second table
68
  st.markdown(f"""<style>
 
73
 
74
  st.table(detail_table)
75
  st.write("------")
76
+
77
+ # Display the result
78
+ # st.write("API Response:")
79
+ # st.json(api_data)