Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import numpy as np | |
| import sympy as sp | |
| import math | |
| # Define calculator operations | |
| def calculate(expression): | |
| try: | |
| # Evaluate the expression safely | |
| result = eval(expression, {"__builtins__": None}, { | |
| "sin": math.sin, "cos": math.cos, "tan": math.tan, | |
| "asin": math.asin, "acos": math.acos, "atan": math.atan, | |
| "sinh": math.sinh, "cosh": math.cosh, "tanh": math.tanh, | |
| "log": math.log, "ln": math.log, "sqrt": math.sqrt, | |
| "pi": math.pi, "e": math.e, "pow": math.pow, | |
| "abs": abs, "factorial": math.factorial, | |
| "radians": math.radians, "degrees": math.degrees | |
| }) | |
| return result | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Streamlit app UI | |
| st.title("Scientific Calculator") | |
| # User input for mathematical expression | |
| input_expression = st.text_input("Enter Expression:", value="", placeholder="e.g., sin(pi/4) + log(10)") | |
| # Buttons grid similar to the calculator image | |
| buttons = [ | |
| ["7", "8", "9", "/", "AC"], | |
| ["4", "5", "6", "*", "π"], | |
| ["1", "2", "3", "-", "e"], | |
| ["0", ".", "=", "+", "^"], | |
| ["sin", "cos", "tan", "log", "ln"], | |
| ["sqrt", "(", ")", "!", "deg→rad"] | |
| ] | |
| # Store the current expression | |
| if "expression" not in st.session_state: | |
| st.session_state.expression = "" | |
| # Display buttons in a grid | |
| for row in buttons: | |
| cols = st.columns(len(row)) | |
| for i, button in enumerate(row): | |
| if cols[i].button(button): | |
| if button == "=": | |
| # Calculate the result | |
| st.session_state.expression = str(calculate(st.session_state.expression)) | |
| elif button == "AC": | |
| # Clear the input | |
| st.session_state.expression = "" | |
| elif button == "π": | |
| st.session_state.expression += "pi" | |
| elif button == "e": | |
| st.session_state.expression += "e" | |
| elif button == "deg→rad": | |
| st.session_state.expression += "*pi/180" | |
| elif button == "!": | |
| st.session_state.expression += "!" | |
| else: | |
| st.session_state.expression += button | |
| # Display the current expression | |
| st.text_input("Expression", st.session_state.expression, key="display", disabled=True) | |