Spaces:
Build error
Build error
| import gradio as gr | |
| import math | |
| import re | |
| history = [] | |
| def calculate(expression): | |
| try: | |
| if not re.match(r'^[\d\s\+\-\*/\(\)\^\.\w]+$', expression): | |
| raise ValueError("Invalid characters in expression") | |
| expression = expression.replace('^', '**') | |
| safe_dict = { | |
| 'sin': math.sin, 'cos': math.cos, 'tan': math.tan, | |
| 'log': math.log10, 'ln': math.log, 'sqrt': math.sqrt, | |
| 'pi': math.pi, 'e': math.e | |
| } | |
| result = eval(expression, {"__builtins__": None}, safe_dict) | |
| result_str = f"{result:.8f}".rstrip('0').rstrip('.') | |
| global history | |
| history.append(f"{expression} = {result_str}") | |
| history = history[-5:] | |
| return result_str, "\n".join(reversed(history)) | |
| except Exception as e: | |
| return f"Error: {str(e)}", "\n".join(reversed(history)) | |
| def update_expression(expression, value): | |
| if expression == "0" and value not in ['.', '(', ')']: | |
| return value | |
| if value in ['sin', 'cos', 'tan', 'log', 'ln', 'sqrt']: | |
| return expression + value + '(' | |
| return expression + value | |
| def backspace(expression): | |
| return expression[:-1] if len(expression) > 0 else "0" | |
| def clear(expression): | |
| return "0" | |
| css = """ | |
| .calculator-display { font-family: 'Digital-7', 'Courier New', monospace; font-size: 24px; } | |
| .calculator-button { font-family: 'Arial', sans-serif; font-size: 18px; } | |
| .number-button { background-color: #e0e0e0; } | |
| .basic-op-button { background-color: #ffffff; } | |
| .advanced-op-button { background-color: #ffa500; } | |
| .calculator-box { border: 1px solid #ccc; padding: 10px; margin: 5px; border-radius: 5px; } | |
| """ | |
| with gr.Blocks(css=css) as iface: | |
| gr.Markdown("# Scientific Calculator") | |
| with gr.Row(): | |
| history_display = gr.Textbox(label="History (Last 5 Calculations)", lines=5) | |
| with gr.Row(): | |
| display = gr.Textbox(value="0", label="Display", elem_classes=["calculator-display"]) | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| with gr.Column(elem_classes=["calculator-box"]): | |
| # Number pad | |
| for i in range(3): | |
| with gr.Row(): | |
| for j in range(3): | |
| num = 7 - 3*i + j | |
| globals()[f"btn_{num}"] = gr.Button(str(num), elem_classes=["calculator-button", "number-button"]) | |
| with gr.Row(): | |
| btn_0 = gr.Button("0", elem_classes=["calculator-button", "number-button"]) | |
| btn_dot = gr.Button(".", elem_classes=["calculator-button", "number-button"]) | |
| btn_equals = gr.Button("=", elem_classes=["calculator-button", "basic-op-button"]) | |
| with gr.Column(scale=2): | |
| with gr.Column(elem_classes=["calculator-box"]): | |
| # Basic operations | |
| for op, symbol in [("add", "+"), ("sub", "-"), ("mul", "*"), ("div", "/")]: | |
| globals()[f"btn_{op}"] = gr.Button(symbol, elem_classes=["calculator-button", "basic-op-button"]) | |
| btn_left_paren = gr.Button("(", elem_classes=["calculator-button", "basic-op-button"]) | |
| btn_right_paren = gr.Button(")", elem_classes=["calculator-button", "basic-op-button"]) | |
| btn_clear = gr.Button("C", elem_classes=["calculator-button", "basic-op-button"]) | |
| btn_backspace = gr.Button("⌫", elem_classes=["calculator-button", "basic-op-button"]) | |
| with gr.Column(scale=2): | |
| with gr.Column(elem_classes=["calculator-box"]): | |
| # Advanced operations | |
| for func in ["sin", "cos", "tan", "log", "ln", "sqrt"]: | |
| globals()[f"btn_{func}"] = gr.Button(func, elem_classes=["calculator-button", "advanced-op-button"]) | |
| btn_power = gr.Button("^", elem_classes=["calculator-button", "advanced-op-button"]) | |
| btn_pi = gr.Button("π", elem_classes=["calculator-button", "advanced-op-button"]) | |
| btn_e = gr.Button("e", elem_classes=["calculator-button", "advanced-op-button"]) | |
| # Define button click events | |
| for btn in [btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, | |
| btn_dot, btn_add, btn_sub, btn_mul, btn_div, btn_left_paren, btn_right_paren, | |
| btn_power, btn_sin, btn_cos, btn_tan, btn_log, btn_ln, btn_sqrt, btn_pi, btn_e]: | |
| btn.click(update_expression, inputs=[display, btn], outputs=display) | |
| btn_equals.click(calculate, inputs=display, outputs=[display, history_display]) | |
| btn_clear.click(clear, inputs=display, outputs=display) | |
| btn_backspace.click(backspace, inputs=display, outputs=display) | |
| iface.launch() |