Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import plotly.graph_objs as go
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Basic arithmetic functions
|
| 6 |
+
def add(x, y):
|
| 7 |
+
return x + y
|
| 8 |
+
|
| 9 |
+
def subtract(x, y):
|
| 10 |
+
return x - y
|
| 11 |
+
|
| 12 |
+
def multiply(x, y):
|
| 13 |
+
return x * y
|
| 14 |
+
|
| 15 |
+
def divide(x, y):
|
| 16 |
+
if y == 0:
|
| 17 |
+
return "Error! Division by zero."
|
| 18 |
+
return x / y
|
| 19 |
+
|
| 20 |
+
# Advanced mathematical functions
|
| 21 |
+
def power(x, y):
|
| 22 |
+
return x ** y
|
| 23 |
+
|
| 24 |
+
def square_root(x):
|
| 25 |
+
if x < 0:
|
| 26 |
+
return "Error! Cannot calculate square root of a negative number."
|
| 27 |
+
return x ** 0.5
|
| 28 |
+
|
| 29 |
+
def logarithm(x, base=10):
|
| 30 |
+
if x <= 0:
|
| 31 |
+
return "Error! Logarithm is undefined for non-positive numbers."
|
| 32 |
+
return np.log(x) / np.log(base)
|
| 33 |
+
|
| 34 |
+
# Trigonometric functions
|
| 35 |
+
def sin(x):
|
| 36 |
+
return np.sin(x)
|
| 37 |
+
|
| 38 |
+
def cos(x):
|
| 39 |
+
return np.cos(x)
|
| 40 |
+
|
| 41 |
+
def tan(x):
|
| 42 |
+
return np.tan(x)
|
| 43 |
+
|
| 44 |
+
# Graphing function
|
| 45 |
+
def plot_function(func, x_range):
|
| 46 |
+
x = np.linspace(x_range[0], x_range[1], 100)
|
| 47 |
+
y = func(x)
|
| 48 |
+
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'))
|
| 49 |
+
fig.update_layout(title=f'Graph of {func.__name__}(x)', xaxis_title='x', yaxis_title='y')
|
| 50 |
+
return fig
|
| 51 |
+
|
| 52 |
+
# Streamlit app
|
| 53 |
+
def calculator():
|
| 54 |
+
st.title("Enhanced Calculator with Graphs")
|
| 55 |
+
|
| 56 |
+
# Sidebar for basic calculations
|
| 57 |
+
st.sidebar.header("Basic Calculations")
|
| 58 |
+
num1 = st.sidebar.number_input("Enter the first number:", value=0.0, step=1.0)
|
| 59 |
+
num2 = st.sidebar.number_input("Enter the second number:", value=0.0, step=1.0)
|
| 60 |
+
operation = st.sidebar.selectbox("Choose an operation",
|
| 61 |
+
["Add", "Subtract", "Multiply", "Divide", "Power"])
|
| 62 |
+
|
| 63 |
+
if st.sidebar.button("Calculate"):
|
| 64 |
+
if operation == "Add":
|
| 65 |
+
result = add(num1, num2)
|
| 66 |
+
elif operation == "Subtract":
|
| 67 |
+
result = subtract(num1, num2)
|
| 68 |
+
elif operation == "Multiply":
|
| 69 |
+
result = multiply(num1, num2)
|
| 70 |
+
elif operation == "Divide":
|
| 71 |
+
result = divide(num1, num2)
|
| 72 |
+
elif operation == "Power":
|
| 73 |
+
result = power(num1, num2)
|
| 74 |
+
|
| 75 |
+
st.sidebar.success(f"The result is: {result}")
|
| 76 |
+
|
| 77 |
+
# Main area for advanced functions and graphing
|
| 78 |
+
st.header("Advanced Functions")
|
| 79 |
+
func_choice = st.selectbox("Choose a function to graph",
|
| 80 |
+
["Sin", "Cos", "Tan", "Square Root", "Logarithm"])
|
| 81 |
+
|
| 82 |
+
x_min = st.number_input("Enter the minimum x value:", value=-10.0, step=1.0)
|
| 83 |
+
x_max = st.number_input("Enter the maximum x value:", value=10.0, step=1.0)
|
| 84 |
+
|
| 85 |
+
if st.button("Generate Graph"):
|
| 86 |
+
if func_choice == "Sin":
|
| 87 |
+
fig = plot_function(sin, [x_min, x_max])
|
| 88 |
+
elif func_choice == "Cos":
|
| 89 |
+
fig = plot_function(cos, [x_min, x_max])
|
| 90 |
+
elif func_choice == "Tan":
|
| 91 |
+
fig = plot_function(tan, [x_min, x_max])
|
| 92 |
+
elif func_choice == "Square Root":
|
| 93 |
+
fig = plot_function(square_root, [0, x_max]) # Adjust range for square root
|
| 94 |
+
elif func_choice == "Logarithm":
|
| 95 |
+
fig = plot_function(logarithm, [0.1, x_max]) # Adjust range for logarithm
|
| 96 |
+
|
| 97 |
+
st.plotly_chart(fig)
|
| 98 |
+
|
| 99 |
+
# Additional calculations
|
| 100 |
+
st.header("Additional Calculations")
|
| 101 |
+
extra_num = st.number_input("Enter a number for additional calculations:", value=1.0, step=1.0)
|
| 102 |
+
|
| 103 |
+
col1, col2 = st.columns(2)
|
| 104 |
+
with col1:
|
| 105 |
+
if st.button("Calculate Square Root"):
|
| 106 |
+
result = square_root(extra_num)
|
| 107 |
+
st.write(f"Square root of {extra_num} is: {result}")
|
| 108 |
+
|
| 109 |
+
with col2:
|
| 110 |
+
if st.button("Calculate Natural Logarithm"):
|
| 111 |
+
result = logarithm(extra_num, base=np.e)
|
| 112 |
+
st.write(f"Natural logarithm of {extra_num} is: {result}")
|
| 113 |
+
|
| 114 |
+
# Run the calculator app
|
| 115 |
+
if __name__ == '__main__':
|
| 116 |
+
calculator()
|