CALCULATOR
To upload files, please first save the app
import streamlit as st
import numpy as np
st.title("Simple Calculator")
# Create a container for the calculator
calc_container = st.container()
with calc_container:
# Two columns for input values
col1, col2 = st.columns(2)
with col1:
num1 = st.number_input("First Number", value=0.0)
with col2:
num2 = st.number_input("Second Number", value=0.0)
# Operation selection
operation = st.selectbox(
"Select Operation",
["Addition", "Subtraction", "Multiplication", "Division", "Power", "Square Root", "Sine", "Cosine", "Tangent", "Logarithm"]
)
# Calculate button
calculate = st.button("Calculate")
# Result container
result_container = st.container()
if calculate:
if operation == "Addition":
result = num1 + num2
expression = f"{num1} + {num2}"
elif operation == "Subtraction":
result = num1 - num2
expression = f"{num1} - {num2}"
elif operation == "Multiplication":
result = num1 * num2
expression = f"{num1} × {num2}"
elif operation == "Division":
if num2 == 0:
result = "Error: Division by zero"
expression = f"{num1} ÷ {num2}"
else:
result = num1 / num2
expression = f"{num1} ÷ {num2}"
elif operation == "Power":
result = num1 ** num2
expression = f"{num1}^{num2}"
elif operation == "Square Root":
if num1 < 0:
result = "Error: Cannot calculate square root of negative number"
expression = f"√{num1}"
else:
result = np.sqrt(num1)
expression = f"√{num1}"
elif operation == "Sine":
result = np.sin(num1)
expression = f"sin({num1})"
elif operation == "Cosine":
result = np.cos(num1)
expression = f"cos({num1})"
elif operation == "Tangent":
result = np.tan(num1)
expression = f"tan({num1})"
elif operation == "Logarithm":
if num1 <= 0:
result = "Error: Cannot calculate logarithm of non-positive number"
expression = f"log({num1})"
else:
result = np.log(num1)
expression = f"log({num1})"
with result_container:
st.write("### Result")
if isinstance(result, str):
st.error(result)
else:
st.success(f"{expression} = {result}")
# Display LaTeX for mathematical expression
latex_expression = ""
if operation == "Addition":
latex_expression = f"{num1} + {num2} = {result}"
elif operation == "Subtraction":
latex_expression = f"{num1} - {num2} = {result}"
elif operation == "Multiplication":
latex_expression = f"{num1} \\times {num2} = {result}"
elif operation == "Division":
latex_expression = f"{num1} \\div {num2} = {result}"
elif operation == "Power":
latex_expression = f"{num1}^{{{num2}}} = {result}"
elif operation == "Square Root":
latex_expression = f"\\sqrt{{{num1}}} = {result}"
elif operation == "Sine":
latex_expression = f"\\sin({num1}) = {result}"
elif operation == "Cosine":
latex_expression = f"\\cos({num1}) = {result}"
elif operation == "Tangent":
latex_expression = f"\\tan({num1}) = {result}"
elif operation == "Logarithm":
latex_expression = f"\\log({num1}) = {result}"
st.latex(latex_expression)
# History section
st.write("---")
st.write("### History")
st.write("Recent calculations will appear here.")
# Add information about the calculator
st.write("---")
st.write("### About This Calculator")
st.write("""
This simple calculator supports basic arithmetic operations and some mathematical functions:
- Addition, Subtraction, Multiplication, Division
- Power calculation
- Square Root
- Trigonometric functions (Sin, Cos, Tan)
- Logarithm (natural log)
For trigonometric functions and logarithm, only the first number input is used.
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?