Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import streamlit as st
- # ---- TITLE ----
- st.title("Margin Calculator")
- st.markdown("To calculate profit margin, enter cost and revenue. "
- "Alternatively, enter cost and profit margin, to calculate revenue.")
- st.write("---")
- # ---- COST ----
- cost = st.number_input(label="COST ($)",
- key="cost",
- min_value=0.00,
- help="Cost is the total price associated "
- "with the purchase of an asset. The original cost "
- "of an asset takes into consideration all of the "
- "items that can be attributed to its purchase and "
- "to putting the asset to use."
- )
- # ---- FUNCTIONALITY ----
- def calculate_margin():
- try:
- st.session_state.margin_percent = (
- st.session_state.sell_price - st.session_state.cost) / \
- st.session_state.sell_price * 100
- except ZeroDivisionError:
- return None
- def calculate_selling_price():
- st.session_state.sell_price = st.session_state.cost / (1 - (st.session_state.margin_percent / 100))
- # ---- SELLING PRICE (OPTIONAL) ----
- revenue = st.number_input(label="REVENUE ($)",
- key="sell_price",
- on_change=calculate_margin,
- min_value=0.00,
- help="Revenue is the price the product is sold for.")
- # ---- MARGIN (OPTIONAL) ----
- profit_margin = st.number_input(label="PROFIT MARGIN (%)",
- key="margin_percent",
- on_change=calculate_selling_price,
- min_value=0.00,
- max_value=99.99,
- help="Profit margin is the money a company makes from each sale after subtracting "
- "the costs. It shows how much profit the company keeps out of the total revenue."
- )
- # ---- GROSS PROFIT ----
- profit = revenue - cost
- if profit < 0:
- st.subheader(f"GROSS PROFIT: :red[${profit:.2f}]",
- help="Gross profit is sales minus the cost of goods sold.")
- else:
- st.subheader(f"GROSS PROFIT: :green[${profit:.2f}]",
- help="Gross profit is sales minus the cost of goods sold.")
- # ---- BACKGROUND IMAGE ----
- def add_bg_from_url():
- st.markdown(
- f"""
- <style>
- .stApp {{
- background-image: url("https://w0.peakpx.com/wallpaper/274/758/HD-wallpaper-stock-of-abstract-accounting-aircraft.jpg");
- background-attachment: fixed;
- background-size: cover
- }}
- </style>
- """,
- unsafe_allow_html=True
- )
- add_bg_from_url()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement