Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,38 @@ import gradio as gr
|
|
| 3 |
|
| 4 |
|
| 5 |
# ============================================================
|
| 6 |
-
# Scientific Calculator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# ============================================================
|
| 8 |
|
| 9 |
def format_number(value):
|
| 10 |
-
"""Format numbers neatly for display."""
|
| 11 |
if isinstance(value, int):
|
| 12 |
return str(value)
|
| 13 |
|
|
@@ -23,75 +50,81 @@ def format_number(value):
|
|
| 23 |
return str(value)
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def calculate(operation, num1, num2):
|
| 27 |
-
"""Perform the selected mathematical operation."""
|
| 28 |
|
| 29 |
-
# Check first number
|
| 30 |
if num1 is None:
|
| 31 |
return "Please enter the first number.", ""
|
| 32 |
|
| 33 |
try:
|
| 34 |
num1 = float(num1)
|
| 35 |
except (TypeError, ValueError):
|
| 36 |
-
return "Error:
|
| 37 |
|
| 38 |
# --------------------------------------------------------
|
| 39 |
-
#
|
| 40 |
# --------------------------------------------------------
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
if num2 is None:
|
| 44 |
return "Please enter the second number.", ""
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
return (
|
| 49 |
format_number(result),
|
| 50 |
-
f"{format_number(num1)} + {format_number(
|
| 51 |
f"{format_number(result)}"
|
| 52 |
)
|
| 53 |
|
| 54 |
# --------------------------------------------------------
|
| 55 |
# Subtraction
|
| 56 |
# --------------------------------------------------------
|
| 57 |
-
elif operation == "Subtraction (-)":
|
| 58 |
|
| 59 |
-
|
| 60 |
-
return "Please enter the second number.", ""
|
| 61 |
|
| 62 |
-
result = num1 -
|
| 63 |
|
| 64 |
return (
|
| 65 |
format_number(result),
|
| 66 |
-
f"{format_number(num1)} - {format_number(
|
| 67 |
f"{format_number(result)}"
|
| 68 |
)
|
| 69 |
|
| 70 |
# --------------------------------------------------------
|
| 71 |
# Multiplication
|
| 72 |
# --------------------------------------------------------
|
| 73 |
-
elif operation == "Multiplication (×)":
|
| 74 |
|
| 75 |
-
|
| 76 |
-
return "Please enter the second number.", ""
|
| 77 |
|
| 78 |
-
result = num1 *
|
| 79 |
|
| 80 |
return (
|
| 81 |
format_number(result),
|
| 82 |
-
f"{format_number(num1)} × {format_number(
|
| 83 |
f"{format_number(result)}"
|
| 84 |
)
|
| 85 |
|
| 86 |
# --------------------------------------------------------
|
| 87 |
# Division
|
| 88 |
# --------------------------------------------------------
|
| 89 |
-
elif operation == "Division (÷)":
|
| 90 |
-
|
| 91 |
-
if num2 is None:
|
| 92 |
-
return "Please enter the second number.", ""
|
| 93 |
|
| 94 |
-
|
| 95 |
|
| 96 |
if num2 == 0:
|
| 97 |
return "Error: Cannot divide by zero.", ""
|
|
@@ -107,12 +140,8 @@ def calculate(operation, num1, num2):
|
|
| 107 |
# --------------------------------------------------------
|
| 108 |
# Power
|
| 109 |
# --------------------------------------------------------
|
| 110 |
-
elif operation == "Power (xʸ)":
|
| 111 |
|
| 112 |
-
|
| 113 |
-
return "Please enter the exponent.", ""
|
| 114 |
-
|
| 115 |
-
num2 = float(num2)
|
| 116 |
|
| 117 |
try:
|
| 118 |
result = num1 ** num2
|
|
@@ -135,10 +164,14 @@ def calculate(operation, num1, num2):
|
|
| 135 |
# --------------------------------------------------------
|
| 136 |
# Square Root
|
| 137 |
# --------------------------------------------------------
|
| 138 |
-
|
|
|
|
| 139 |
|
| 140 |
if num1 < 0:
|
| 141 |
-
return
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
result = math.sqrt(num1)
|
| 144 |
|
|
@@ -150,53 +183,65 @@ def calculate(operation, num1, num2):
|
|
| 150 |
# --------------------------------------------------------
|
| 151 |
# Logarithm Base 10
|
| 152 |
# --------------------------------------------------------
|
| 153 |
-
|
|
|
|
| 154 |
|
| 155 |
if num1 <= 0:
|
| 156 |
-
return
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
result = math.log10(num1)
|
| 159 |
|
| 160 |
return (
|
| 161 |
format_number(result),
|
| 162 |
-
f"log₁₀({format_number(num1)}) =
|
|
|
|
| 163 |
)
|
| 164 |
|
| 165 |
# --------------------------------------------------------
|
| 166 |
# Natural Logarithm
|
| 167 |
# --------------------------------------------------------
|
| 168 |
-
|
|
|
|
| 169 |
|
| 170 |
if num1 <= 0:
|
| 171 |
-
return
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
result = math.log(num1)
|
| 174 |
|
| 175 |
return (
|
| 176 |
format_number(result),
|
| 177 |
-
f"ln({format_number(num1)}) =
|
|
|
|
| 178 |
)
|
| 179 |
|
| 180 |
# --------------------------------------------------------
|
| 181 |
# Sine
|
| 182 |
# --------------------------------------------------------
|
| 183 |
-
|
|
|
|
| 184 |
|
| 185 |
result = math.sin(math.radians(num1))
|
| 186 |
|
| 187 |
-
# Avoid displaying very small floating-point errors
|
| 188 |
if abs(result) < 1e-12:
|
| 189 |
result = 0.0
|
| 190 |
|
| 191 |
return (
|
| 192 |
format_number(result),
|
| 193 |
-
f"sin({format_number(num1)}°) =
|
|
|
|
| 194 |
)
|
| 195 |
|
| 196 |
# --------------------------------------------------------
|
| 197 |
# Cosine
|
| 198 |
# --------------------------------------------------------
|
| 199 |
-
|
|
|
|
| 200 |
|
| 201 |
result = math.cos(math.radians(num1))
|
| 202 |
|
|
@@ -205,17 +250,18 @@ def calculate(operation, num1, num2):
|
|
| 205 |
|
| 206 |
return (
|
| 207 |
format_number(result),
|
| 208 |
-
f"cos({format_number(num1)}°) =
|
|
|
|
| 209 |
)
|
| 210 |
|
| 211 |
# --------------------------------------------------------
|
| 212 |
# Tangent
|
| 213 |
# --------------------------------------------------------
|
| 214 |
-
|
|
|
|
| 215 |
|
| 216 |
radians = math.radians(num1)
|
| 217 |
|
| 218 |
-
# Tangent is undefined when cos(x) = 0
|
| 219 |
if abs(math.cos(radians)) < 1e-12:
|
| 220 |
return (
|
| 221 |
"Error: Tangent is undefined at this angle.",
|
|
@@ -229,53 +275,52 @@ def calculate(operation, num1, num2):
|
|
| 229 |
|
| 230 |
return (
|
| 231 |
format_number(result),
|
| 232 |
-
f"tan({format_number(num1)}°) =
|
|
|
|
| 233 |
)
|
| 234 |
|
| 235 |
# --------------------------------------------------------
|
| 236 |
# Factorial
|
| 237 |
# --------------------------------------------------------
|
| 238 |
-
|
|
|
|
| 239 |
|
| 240 |
if num1 < 0:
|
| 241 |
-
return
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
if not num1.is_integer():
|
| 244 |
-
return
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
if num1 > 170:
|
| 247 |
-
return
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
result = math.factorial(int(num1))
|
| 250 |
|
| 251 |
return (
|
| 252 |
format_number(result),
|
| 253 |
-
f"{format_number(num1)}! =
|
|
|
|
| 254 |
)
|
| 255 |
|
| 256 |
-
|
| 257 |
-
# Invalid operation
|
| 258 |
-
# --------------------------------------------------------
|
| 259 |
-
else:
|
| 260 |
-
return "Error: Please select a valid operation.", ""
|
| 261 |
|
| 262 |
|
| 263 |
# ============================================================
|
| 264 |
-
#
|
| 265 |
# ============================================================
|
| 266 |
|
| 267 |
def update_second_number(operation):
|
| 268 |
-
"""Show second number only for operations requiring two numbers."""
|
| 269 |
|
| 270 |
-
|
| 271 |
-
"Addition (+)",
|
| 272 |
-
"Subtraction (-)",
|
| 273 |
-
"Multiplication (×)",
|
| 274 |
-
"Division (÷)",
|
| 275 |
-
"Power (xʸ)"
|
| 276 |
-
]
|
| 277 |
-
|
| 278 |
-
if operation in binary_operations:
|
| 279 |
return gr.Number(
|
| 280 |
label="Second Number",
|
| 281 |
placeholder="Enter second number",
|
|
@@ -289,11 +334,10 @@ def update_second_number(operation):
|
|
| 289 |
|
| 290 |
|
| 291 |
# ============================================================
|
| 292 |
-
# Clear
|
| 293 |
# ============================================================
|
| 294 |
|
| 295 |
def clear_calculator():
|
| 296 |
-
"""Reset calculator fields."""
|
| 297 |
|
| 298 |
return (
|
| 299 |
None,
|
|
@@ -308,7 +352,8 @@ def clear_calculator():
|
|
| 308 |
# Custom CSS
|
| 309 |
# ============================================================
|
| 310 |
|
| 311 |
-
|
|
|
|
| 312 |
body {
|
| 313 |
background: #f5f7fb;
|
| 314 |
}
|
|
@@ -377,16 +422,16 @@ body {
|
|
| 377 |
padding: 18px;
|
| 378 |
}
|
| 379 |
}
|
|
|
|
| 380 |
"""
|
| 381 |
|
| 382 |
|
| 383 |
# ============================================================
|
| 384 |
-
# Gradio
|
| 385 |
# ============================================================
|
| 386 |
|
| 387 |
with gr.Blocks(
|
| 388 |
-
title="Scientific Calculator"
|
| 389 |
-
css=custom_css
|
| 390 |
) as app:
|
| 391 |
|
| 392 |
# --------------------------------------------------------
|
|
@@ -398,8 +443,8 @@ with gr.Blocks(
|
|
| 398 |
<div class="header">
|
| 399 |
<h1>Scientific Calculator</h1>
|
| 400 |
<p>
|
| 401 |
-
Perform basic and scientific mathematical
|
| 402 |
-
quickly and easily.
|
| 403 |
</p>
|
| 404 |
</div>
|
| 405 |
"""
|
|
@@ -411,29 +456,16 @@ with gr.Blocks(
|
|
| 411 |
|
| 412 |
with gr.Row():
|
| 413 |
|
| 414 |
-
#
|
| 415 |
-
# Calculator
|
| 416 |
-
#
|
| 417 |
|
| 418 |
with gr.Column(elem_classes="calc-card"):
|
| 419 |
|
| 420 |
gr.Markdown("### Calculator")
|
| 421 |
|
| 422 |
operation = gr.Dropdown(
|
| 423 |
-
choices=
|
| 424 |
-
"Addition (+)",
|
| 425 |
-
"Subtraction (-)",
|
| 426 |
-
"Multiplication (×)",
|
| 427 |
-
"Division (÷)",
|
| 428 |
-
"Power (xʸ)",
|
| 429 |
-
"Square Root (√x)",
|
| 430 |
-
"Logarithm (log₁₀)",
|
| 431 |
-
"Natural Log (ln)",
|
| 432 |
-
"Sine (sin)",
|
| 433 |
-
"Cosine (cos)",
|
| 434 |
-
"Tangent (tan)",
|
| 435 |
-
"Factorial (n!)"
|
| 436 |
-
],
|
| 437 |
value="Addition (+)",
|
| 438 |
label="Select Operation"
|
| 439 |
)
|
|
@@ -460,9 +492,9 @@ with gr.Blocks(
|
|
| 460 |
variant="secondary"
|
| 461 |
)
|
| 462 |
|
| 463 |
-
#
|
| 464 |
-
# Result
|
| 465 |
-
#
|
| 466 |
|
| 467 |
with gr.Column(elem_classes="calc-card"):
|
| 468 |
|
|
@@ -486,15 +518,23 @@ with gr.Blocks(
|
|
| 486 |
"""
|
| 487 |
### Supported Operations
|
| 488 |
|
| 489 |
-
**Basic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
|
| 491 |
-
|
| 492 |
|
| 493 |
-
**
|
| 494 |
|
| 495 |
-
|
| 496 |
|
| 497 |
-
**Note:** Trigonometric functions use
|
| 498 |
"""
|
| 499 |
)
|
| 500 |
|
|
@@ -505,7 +545,7 @@ with gr.Blocks(
|
|
| 505 |
gr.HTML(
|
| 506 |
"""
|
| 507 |
<div class="footer">
|
| 508 |
-
Scientific Calculator •
|
| 509 |
</div>
|
| 510 |
"""
|
| 511 |
)
|
|
@@ -548,5 +588,7 @@ if __name__ == "__main__":
|
|
| 548 |
app.launch(
|
| 549 |
server_name="0.0.0.0",
|
| 550 |
server_port=7860,
|
| 551 |
-
share=False
|
|
|
|
|
|
|
| 552 |
)
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
# ============================================================
|
| 6 |
+
# Scientific Calculator
|
| 7 |
+
# ============================================================
|
| 8 |
+
|
| 9 |
+
OPERATIONS = [
|
| 10 |
+
"Addition (+)",
|
| 11 |
+
"Subtraction (-)",
|
| 12 |
+
"Multiplication (×)",
|
| 13 |
+
"Division (÷)",
|
| 14 |
+
"Power (xʸ)",
|
| 15 |
+
"Square Root (√x)",
|
| 16 |
+
"Logarithm (log₁₀)",
|
| 17 |
+
"Natural Log (ln)",
|
| 18 |
+
"Sine (sin)",
|
| 19 |
+
"Cosine (cos)",
|
| 20 |
+
"Tangent (tan)",
|
| 21 |
+
"Factorial (n!)",
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
BINARY_OPERATIONS = {
|
| 25 |
+
"Addition (+)",
|
| 26 |
+
"Subtraction (-)",
|
| 27 |
+
"Multiplication (×)",
|
| 28 |
+
"Division (÷)",
|
| 29 |
+
"Power (xʸ)",
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# ============================================================
|
| 34 |
+
# Number Formatting
|
| 35 |
# ============================================================
|
| 36 |
|
| 37 |
def format_number(value):
|
|
|
|
| 38 |
if isinstance(value, int):
|
| 39 |
return str(value)
|
| 40 |
|
|
|
|
| 50 |
return str(value)
|
| 51 |
|
| 52 |
|
| 53 |
+
# ============================================================
|
| 54 |
+
# Calculator
|
| 55 |
+
# ============================================================
|
| 56 |
+
|
| 57 |
def calculate(operation, num1, num2):
|
|
|
|
| 58 |
|
|
|
|
| 59 |
if num1 is None:
|
| 60 |
return "Please enter the first number.", ""
|
| 61 |
|
| 62 |
try:
|
| 63 |
num1 = float(num1)
|
| 64 |
except (TypeError, ValueError):
|
| 65 |
+
return "Error: Invalid first number.", ""
|
| 66 |
|
| 67 |
# --------------------------------------------------------
|
| 68 |
+
# Binary Operations
|
| 69 |
# --------------------------------------------------------
|
| 70 |
+
|
| 71 |
+
if operation in BINARY_OPERATIONS:
|
| 72 |
|
| 73 |
if num2 is None:
|
| 74 |
return "Please enter the second number.", ""
|
| 75 |
|
| 76 |
+
try:
|
| 77 |
+
num2 = float(num2)
|
| 78 |
+
except (TypeError, ValueError):
|
| 79 |
+
return "Error: Invalid second number.", ""
|
| 80 |
+
|
| 81 |
+
# --------------------------------------------------------
|
| 82 |
+
# Addition
|
| 83 |
+
# --------------------------------------------------------
|
| 84 |
+
|
| 85 |
+
if operation == "Addition (+)":
|
| 86 |
+
|
| 87 |
+
result = num1 + num2
|
| 88 |
|
| 89 |
return (
|
| 90 |
format_number(result),
|
| 91 |
+
f"{format_number(num1)} + {format_number(num2)} = "
|
| 92 |
f"{format_number(result)}"
|
| 93 |
)
|
| 94 |
|
| 95 |
# --------------------------------------------------------
|
| 96 |
# Subtraction
|
| 97 |
# --------------------------------------------------------
|
|
|
|
| 98 |
|
| 99 |
+
if operation == "Subtraction (-)":
|
|
|
|
| 100 |
|
| 101 |
+
result = num1 - num2
|
| 102 |
|
| 103 |
return (
|
| 104 |
format_number(result),
|
| 105 |
+
f"{format_number(num1)} - {format_number(num2)} = "
|
| 106 |
f"{format_number(result)}"
|
| 107 |
)
|
| 108 |
|
| 109 |
# --------------------------------------------------------
|
| 110 |
# Multiplication
|
| 111 |
# --------------------------------------------------------
|
|
|
|
| 112 |
|
| 113 |
+
if operation == "Multiplication (×)":
|
|
|
|
| 114 |
|
| 115 |
+
result = num1 * num2
|
| 116 |
|
| 117 |
return (
|
| 118 |
format_number(result),
|
| 119 |
+
f"{format_number(num1)} × {format_number(num2)} = "
|
| 120 |
f"{format_number(result)}"
|
| 121 |
)
|
| 122 |
|
| 123 |
# --------------------------------------------------------
|
| 124 |
# Division
|
| 125 |
# --------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
if operation == "Division (÷)":
|
| 128 |
|
| 129 |
if num2 == 0:
|
| 130 |
return "Error: Cannot divide by zero.", ""
|
|
|
|
| 140 |
# --------------------------------------------------------
|
| 141 |
# Power
|
| 142 |
# --------------------------------------------------------
|
|
|
|
| 143 |
|
| 144 |
+
if operation == "Power (xʸ)":
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
try:
|
| 147 |
result = num1 ** num2
|
|
|
|
| 164 |
# --------------------------------------------------------
|
| 165 |
# Square Root
|
| 166 |
# --------------------------------------------------------
|
| 167 |
+
|
| 168 |
+
if operation == "Square Root (√x)":
|
| 169 |
|
| 170 |
if num1 < 0:
|
| 171 |
+
return (
|
| 172 |
+
"Error: Square root of a negative number is not real.",
|
| 173 |
+
""
|
| 174 |
+
)
|
| 175 |
|
| 176 |
result = math.sqrt(num1)
|
| 177 |
|
|
|
|
| 183 |
# --------------------------------------------------------
|
| 184 |
# Logarithm Base 10
|
| 185 |
# --------------------------------------------------------
|
| 186 |
+
|
| 187 |
+
if operation == "Logarithm (log₁₀)":
|
| 188 |
|
| 189 |
if num1 <= 0:
|
| 190 |
+
return (
|
| 191 |
+
"Error: Logarithm requires a positive number.",
|
| 192 |
+
""
|
| 193 |
+
)
|
| 194 |
|
| 195 |
result = math.log10(num1)
|
| 196 |
|
| 197 |
return (
|
| 198 |
format_number(result),
|
| 199 |
+
f"log₁₀({format_number(num1)}) = "
|
| 200 |
+
f"{format_number(result)}"
|
| 201 |
)
|
| 202 |
|
| 203 |
# --------------------------------------------------------
|
| 204 |
# Natural Logarithm
|
| 205 |
# --------------------------------------------------------
|
| 206 |
+
|
| 207 |
+
if operation == "Natural Log (ln)":
|
| 208 |
|
| 209 |
if num1 <= 0:
|
| 210 |
+
return (
|
| 211 |
+
"Error: Natural logarithm requires a positive number.",
|
| 212 |
+
""
|
| 213 |
+
)
|
| 214 |
|
| 215 |
result = math.log(num1)
|
| 216 |
|
| 217 |
return (
|
| 218 |
format_number(result),
|
| 219 |
+
f"ln({format_number(num1)}) = "
|
| 220 |
+
f"{format_number(result)}"
|
| 221 |
)
|
| 222 |
|
| 223 |
# --------------------------------------------------------
|
| 224 |
# Sine
|
| 225 |
# --------------------------------------------------------
|
| 226 |
+
|
| 227 |
+
if operation == "Sine (sin)":
|
| 228 |
|
| 229 |
result = math.sin(math.radians(num1))
|
| 230 |
|
|
|
|
| 231 |
if abs(result) < 1e-12:
|
| 232 |
result = 0.0
|
| 233 |
|
| 234 |
return (
|
| 235 |
format_number(result),
|
| 236 |
+
f"sin({format_number(num1)}°) = "
|
| 237 |
+
f"{format_number(result)}"
|
| 238 |
)
|
| 239 |
|
| 240 |
# --------------------------------------------------------
|
| 241 |
# Cosine
|
| 242 |
# --------------------------------------------------------
|
| 243 |
+
|
| 244 |
+
if operation == "Cosine (cos)":
|
| 245 |
|
| 246 |
result = math.cos(math.radians(num1))
|
| 247 |
|
|
|
|
| 250 |
|
| 251 |
return (
|
| 252 |
format_number(result),
|
| 253 |
+
f"cos({format_number(num1)}°) = "
|
| 254 |
+
f"{format_number(result)}"
|
| 255 |
)
|
| 256 |
|
| 257 |
# --------------------------------------------------------
|
| 258 |
# Tangent
|
| 259 |
# --------------------------------------------------------
|
| 260 |
+
|
| 261 |
+
if operation == "Tangent (tan)":
|
| 262 |
|
| 263 |
radians = math.radians(num1)
|
| 264 |
|
|
|
|
| 265 |
if abs(math.cos(radians)) < 1e-12:
|
| 266 |
return (
|
| 267 |
"Error: Tangent is undefined at this angle.",
|
|
|
|
| 275 |
|
| 276 |
return (
|
| 277 |
format_number(result),
|
| 278 |
+
f"tan({format_number(num1)}°) = "
|
| 279 |
+
f"{format_number(result)}"
|
| 280 |
)
|
| 281 |
|
| 282 |
# --------------------------------------------------------
|
| 283 |
# Factorial
|
| 284 |
# --------------------------------------------------------
|
| 285 |
+
|
| 286 |
+
if operation == "Factorial (n!)":
|
| 287 |
|
| 288 |
if num1 < 0:
|
| 289 |
+
return (
|
| 290 |
+
"Error: Factorial requires a non-negative number.",
|
| 291 |
+
""
|
| 292 |
+
)
|
| 293 |
|
| 294 |
if not num1.is_integer():
|
| 295 |
+
return (
|
| 296 |
+
"Error: Factorial requires a whole number.",
|
| 297 |
+
""
|
| 298 |
+
)
|
| 299 |
|
| 300 |
if num1 > 170:
|
| 301 |
+
return (
|
| 302 |
+
"Error: Number is too large for factorial.",
|
| 303 |
+
""
|
| 304 |
+
)
|
| 305 |
|
| 306 |
result = math.factorial(int(num1))
|
| 307 |
|
| 308 |
return (
|
| 309 |
format_number(result),
|
| 310 |
+
f"{format_number(num1)}! = "
|
| 311 |
+
f"{format_number(result)}"
|
| 312 |
)
|
| 313 |
|
| 314 |
+
return "Error: Invalid operation.", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
|
| 317 |
# ============================================================
|
| 318 |
+
# Update Second Number Visibility
|
| 319 |
# ============================================================
|
| 320 |
|
| 321 |
def update_second_number(operation):
|
|
|
|
| 322 |
|
| 323 |
+
if operation in BINARY_OPERATIONS:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
return gr.Number(
|
| 325 |
label="Second Number",
|
| 326 |
placeholder="Enter second number",
|
|
|
|
| 334 |
|
| 335 |
|
| 336 |
# ============================================================
|
| 337 |
+
# Clear
|
| 338 |
# ============================================================
|
| 339 |
|
| 340 |
def clear_calculator():
|
|
|
|
| 341 |
|
| 342 |
return (
|
| 343 |
None,
|
|
|
|
| 352 |
# Custom CSS
|
| 353 |
# ============================================================
|
| 354 |
|
| 355 |
+
CUSTOM_CSS = """
|
| 356 |
+
|
| 357 |
body {
|
| 358 |
background: #f5f7fb;
|
| 359 |
}
|
|
|
|
| 422 |
padding: 18px;
|
| 423 |
}
|
| 424 |
}
|
| 425 |
+
|
| 426 |
"""
|
| 427 |
|
| 428 |
|
| 429 |
# ============================================================
|
| 430 |
+
# Gradio Application
|
| 431 |
# ============================================================
|
| 432 |
|
| 433 |
with gr.Blocks(
|
| 434 |
+
title="Scientific Calculator"
|
|
|
|
| 435 |
) as app:
|
| 436 |
|
| 437 |
# --------------------------------------------------------
|
|
|
|
| 443 |
<div class="header">
|
| 444 |
<h1>Scientific Calculator</h1>
|
| 445 |
<p>
|
| 446 |
+
Perform basic and scientific mathematical
|
| 447 |
+
calculations quickly and easily.
|
| 448 |
</p>
|
| 449 |
</div>
|
| 450 |
"""
|
|
|
|
| 456 |
|
| 457 |
with gr.Row():
|
| 458 |
|
| 459 |
+
# ====================================================
|
| 460 |
+
# Calculator
|
| 461 |
+
# ====================================================
|
| 462 |
|
| 463 |
with gr.Column(elem_classes="calc-card"):
|
| 464 |
|
| 465 |
gr.Markdown("### Calculator")
|
| 466 |
|
| 467 |
operation = gr.Dropdown(
|
| 468 |
+
choices=OPERATIONS,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
value="Addition (+)",
|
| 470 |
label="Select Operation"
|
| 471 |
)
|
|
|
|
| 492 |
variant="secondary"
|
| 493 |
)
|
| 494 |
|
| 495 |
+
# ====================================================
|
| 496 |
+
# Result
|
| 497 |
+
# ====================================================
|
| 498 |
|
| 499 |
with gr.Column(elem_classes="calc-card"):
|
| 500 |
|
|
|
|
| 518 |
"""
|
| 519 |
### Supported Operations
|
| 520 |
|
| 521 |
+
**Basic**
|
| 522 |
+
|
| 523 |
+
Addition, Subtraction, Multiplication, Division
|
| 524 |
+
|
| 525 |
+
**Advanced**
|
| 526 |
+
|
| 527 |
+
Power, Square Root, Logarithm, Natural Log
|
| 528 |
+
|
| 529 |
+
**Trigonometry**
|
| 530 |
|
| 531 |
+
Sine, Cosine, Tangent
|
| 532 |
|
| 533 |
+
**Other**
|
| 534 |
|
| 535 |
+
Factorial
|
| 536 |
|
| 537 |
+
**Note:** Trigonometric functions use degrees.
|
| 538 |
"""
|
| 539 |
)
|
| 540 |
|
|
|
|
| 545 |
gr.HTML(
|
| 546 |
"""
|
| 547 |
<div class="footer">
|
| 548 |
+
Scientific Calculator • Python + Gradio
|
| 549 |
</div>
|
| 550 |
"""
|
| 551 |
)
|
|
|
|
| 588 |
app.launch(
|
| 589 |
server_name="0.0.0.0",
|
| 590 |
server_port=7860,
|
| 591 |
+
share=False,
|
| 592 |
+
ssr_mode=False,
|
| 593 |
+
css=CUSTOM_CSS
|
| 594 |
)
|