Shahabmoin's picture
Update app.py
ea48bb6 verified
import streamlit as st
import math
# Function to perform mathematical operations
def calculate(expression):
try:
# Evaluate the mathematical expression safely
result = eval(expression)
return result
except Exception as e:
return f"Error: {str(e)}"
# Streamlit App UI
st.title("Scientific Calculator")
# Input for the mathematical expression
expression = st.text_input("Enter a mathematical expression", "")
# Create buttons below the input field for mathematical operations
col1, col2, col3 = st.columns(3)
with col1:
if st.button('sin(x)'):
expression += 'sin('
with col2:
if st.button('cos(x)'):
expression += 'cos('
with col3:
if st.button('tan(x)'):
expression += 'tan('
col4, col5, col6 = st.columns(3)
with col4:
if st.button('log(x)'):
expression += 'log('
with col5:
if st.button('sqrt(x)'):
expression += 'sqrt('
with col6:
if st.button('exp(x)'):
expression += 'exp('
col7, col8 = st.columns(2)
with col7:
if st.button('pi'):
expression += 'math.pi'
with col8:
if st.button('e'):
expression += 'math.e'
# Display the updated expression in the text box
st.text_input("Enter a mathematical expression", expression)
# Operations
if expression:
result = calculate(expression)
st.write(f"Result: {result}")