Advertisement
differen71

Margin_Calculator_Web_App

May 20th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. import streamlit as st
  2.  
  3. # ---- TITLE ----
  4. st.title("Margin Calculator")
  5. st.markdown("To calculate profit margin, enter cost and revenue. "
  6.             "Alternatively, enter cost and profit margin, to calculate revenue.")
  7. st.write("---")
  8.  
  9. # ---- COST ----
  10. cost = st.number_input(label="COST ($)",
  11.                        key="cost",
  12.                        min_value=0.00,
  13.                        help="Cost is the total price associated "
  14.                             "with the purchase of an asset. The original cost "
  15.                             "of an asset takes into consideration all of the "
  16.                             "items that can be attributed to its purchase and "
  17.                             "to putting the asset to use."
  18.                        )
  19.  
  20.  
  21. # ---- FUNCTIONALITY ----
  22. def calculate_margin():
  23.     try:
  24.         st.session_state.margin_percent = (
  25.                                                   st.session_state.sell_price - st.session_state.cost) / \
  26.                                           st.session_state.sell_price * 100
  27.     except ZeroDivisionError:
  28.         return None
  29.  
  30.  
  31. def calculate_selling_price():
  32.     st.session_state.sell_price = st.session_state.cost / (1 - (st.session_state.margin_percent / 100))
  33.  
  34.  
  35. # ---- SELLING PRICE (OPTIONAL) ----
  36. revenue = st.number_input(label="REVENUE ($)",
  37.                           key="sell_price",
  38.                           on_change=calculate_margin,
  39.                           min_value=0.00,
  40.                           help="Revenue is the price the product is sold for.")
  41.  
  42. # ---- MARGIN (OPTIONAL) ----
  43. profit_margin = st.number_input(label="PROFIT MARGIN (%)",
  44.                                 key="margin_percent",
  45.                                 on_change=calculate_selling_price,
  46.                                 min_value=0.00,
  47.                                 max_value=99.99,
  48.                                 help="Profit margin is the money a company makes from each sale after subtracting "
  49.                                      "the costs. It shows how much profit the company keeps out of the total revenue."
  50.                                 )
  51.  
  52. # ---- GROSS PROFIT ----
  53. profit = revenue - cost
  54.  
  55. if profit < 0:
  56.     st.subheader(f"GROSS PROFIT: :red[${profit:.2f}]",
  57.                  help="Gross profit is sales minus the cost of goods sold.")
  58. else:
  59.     st.subheader(f"GROSS PROFIT: :green[${profit:.2f}]",
  60.                  help="Gross profit is sales minus the cost of goods sold.")
  61.  
  62.  
  63. # ---- BACKGROUND IMAGE ----
  64. def add_bg_from_url():
  65.     st.markdown(
  66.         f"""
  67.             <style>
  68.             .stApp {{
  69.                 background-image: url("https://w0.peakpx.com/wallpaper/274/758/HD-wallpaper-stock-of-abstract-accounting-aircraft.jpg");
  70.                 background-attachment: fixed;
  71.                 background-size: cover
  72.             }}
  73.             </style>
  74.             """,
  75.         unsafe_allow_html=True
  76.     )
  77.  
  78.  
  79. add_bg_from_url()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement