Spaces:
Sleeping
Sleeping
Create frontend.py
Browse files- frontend.py +112 -0
frontend.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
BACKEND_URL = "http://127.0.0.1:8000" # Update this if your backend is on a different host/port
|
| 5 |
+
|
| 6 |
+
# Global variable to store calculation history
|
| 7 |
+
history = []
|
| 8 |
+
|
| 9 |
+
def calculate(expression):
|
| 10 |
+
try:
|
| 11 |
+
response = requests.post(f"{BACKEND_URL}/calculate", json={"expr": expression})
|
| 12 |
+
if response.status_code == 200:
|
| 13 |
+
result = response.json()["result"]
|
| 14 |
+
result_str = f"{result:.8f}".rstrip('0').rstrip('.')
|
| 15 |
+
global history
|
| 16 |
+
history.append(f"{expression} = {result_str}")
|
| 17 |
+
history = history[-5:]
|
| 18 |
+
return "0", result_str, "\n".join(reversed(history))
|
| 19 |
+
else:
|
| 20 |
+
error_data = response.json()
|
| 21 |
+
error_message = error_data.get("detail", "An unknown error occurred")
|
| 22 |
+
return expression, f"Error: {error_message}", "\n".join(reversed(history))
|
| 23 |
+
except requests.RequestException as e:
|
| 24 |
+
return expression, f"Network Error: {str(e)}", "\n".join(reversed(history))
|
| 25 |
+
|
| 26 |
+
def update_expression(expression, value):
|
| 27 |
+
if expression == "0" and value not in ['.', '(', ')']:
|
| 28 |
+
return value
|
| 29 |
+
if value in ['sin', 'cos', 'tan', 'log', 'ln', 'sqrt']:
|
| 30 |
+
return expression + value + '('
|
| 31 |
+
return expression + value
|
| 32 |
+
|
| 33 |
+
def backspace(expression):
|
| 34 |
+
return expression[:-1] if len(expression) > 1 else "0"
|
| 35 |
+
|
| 36 |
+
def clear(expression):
|
| 37 |
+
return "0"
|
| 38 |
+
|
| 39 |
+
css = """
|
| 40 |
+
.calculator-display { font-family: 'Digital-7', 'Courier New', monospace; font-size: 24px; }
|
| 41 |
+
.calculator-result { font-family: 'Digital-7', 'Courier New', monospace; font-size: 72px; }
|
| 42 |
+
.calculator-button { font-family: 'Arial', sans-serif; font-size: 18px; }
|
| 43 |
+
.number-button { background-color: #e0e0e0; }
|
| 44 |
+
.basic-op-button { background-color: #f0f0f0; }
|
| 45 |
+
.advanced-op-button { background-color: #ffa500; }
|
| 46 |
+
.calculator-box { border: 1px solid #ccc; padding: 10px; margin: 5px; border-radius: 5px; }
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
def create_interface():
|
| 50 |
+
with gr.Blocks(css=css) as iface:
|
| 51 |
+
gr.Markdown("# Scientific Calculator")
|
| 52 |
+
|
| 53 |
+
with gr.Row():
|
| 54 |
+
with gr.Column(scale=2):
|
| 55 |
+
display = gr.Textbox(value="0", label="Display", elem_classes=["calculator-display"])
|
| 56 |
+
with gr.Column(scale=1):
|
| 57 |
+
result_display = gr.Textbox(value="", label="Result", elem_classes=["calculator-result"])
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
history_display = gr.Textbox(label="History (Last 5 Calculations)", lines=5)
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column(scale=2):
|
| 64 |
+
with gr.Column(elem_classes=["calculator-box"]):
|
| 65 |
+
# Number pad (3x4 grid)
|
| 66 |
+
for i in range(3):
|
| 67 |
+
with gr.Row():
|
| 68 |
+
for j in range(3):
|
| 69 |
+
num = str(7 - 3*i + j)
|
| 70 |
+
gr.Button(num, elem_classes=["calculator-button", "number-button"]).click(
|
| 71 |
+
lambda x, v=num: update_expression(x, v), inputs=display, outputs=display)
|
| 72 |
+
with gr.Row():
|
| 73 |
+
gr.Button("0", elem_classes=["calculator-button", "number-button"]).click(
|
| 74 |
+
lambda x: update_expression(x, "0"), inputs=display, outputs=display)
|
| 75 |
+
gr.Button(".", elem_classes=["calculator-button", "number-button"]).click(
|
| 76 |
+
lambda x: update_expression(x, "."), inputs=display, outputs=display)
|
| 77 |
+
gr.Button("=", elem_classes=["calculator-button", "basic-op-button"]).click(
|
| 78 |
+
calculate, inputs=display, outputs=[display, result_display, history_display])
|
| 79 |
+
|
| 80 |
+
with gr.Column(scale=1):
|
| 81 |
+
with gr.Column(elem_classes=["calculator-box"]):
|
| 82 |
+
# Basic operations
|
| 83 |
+
for op, symbol in [("add", "+"), ("sub", "-"), ("mul", "*"), ("div", "/")]:
|
| 84 |
+
gr.Button(symbol, elem_classes=["calculator-button", "basic-op-button"]).click(
|
| 85 |
+
lambda x, v=symbol: update_expression(x, v), inputs=display, outputs=display)
|
| 86 |
+
gr.Button("(", elem_classes=["calculator-button", "basic-op-button"]).click(
|
| 87 |
+
lambda x: update_expression(x, "("), inputs=display, outputs=display)
|
| 88 |
+
gr.Button(")", elem_classes=["calculator-button", "basic-op-button"]).click(
|
| 89 |
+
lambda x: update_expression(x, ")"), inputs=display, outputs=display)
|
| 90 |
+
gr.Button("C", elem_classes=["calculator-button", "basic-op-button"]).click(
|
| 91 |
+
clear, inputs=display, outputs=display)
|
| 92 |
+
gr.Button("⌫", elem_classes=["calculator-button", "basic-op-button"]).click(
|
| 93 |
+
backspace, inputs=display, outputs=display)
|
| 94 |
+
|
| 95 |
+
with gr.Column(scale=1):
|
| 96 |
+
with gr.Column(elem_classes=["calculator-box"]):
|
| 97 |
+
# Advanced operations
|
| 98 |
+
for func in ["sin", "cos", "tan", "log", "ln", "sqrt"]:
|
| 99 |
+
gr.Button(func, elem_classes=["calculator-button", "advanced-op-button"]).click(
|
| 100 |
+
lambda x, v=func: update_expression(x, v), inputs=display, outputs=display)
|
| 101 |
+
gr.Button("^", elem_classes=["calculator-button", "advanced-op-button"]).click(
|
| 102 |
+
lambda x: update_expression(x, "^"), inputs=display, outputs=display)
|
| 103 |
+
gr.Button("π", elem_classes=["calculator-button", "advanced-op-button"]).click(
|
| 104 |
+
lambda x: update_expression(x, "pi"), inputs=display, outputs=display)
|
| 105 |
+
gr.Button("e", elem_classes=["calculator-button", "advanced-op-button"]).click(
|
| 106 |
+
lambda x: update_expression(x, "e"), inputs=display, outputs=display)
|
| 107 |
+
|
| 108 |
+
return iface
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
iface = create_interface()
|
| 112 |
+
iface.launch()
|