File size: 3,239 Bytes
05f267c
 
deab7d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05f267c
deab7d8
 
05f267c
deab7d8
 
 
 
 
 
 
05f267c
deab7d8
 
 
 
 
 
 
 
 
 
 
 
 
 
05f267c
deab7d8
05f267c
 
deab7d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05f267c
 
deab7d8
 
05f267c
deab7d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05f267c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Calculator & Function Plotter</title>
    <script type="module" src="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css" />
</head>
<body>
    <gradio-lite>
<gradio-requirements>
numpy
matplotlib
</gradio-requirements>

<gradio-file name="app.py" entrypoint>
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt

# ---------- Basic Calculator ----------
def calculate(num1, operation, num2):
    try:
        num1 = float(num1)
        num2 = float(num2)
    except (TypeError, ValueError):
        return "Enter valid numbers"

    if operation == "+":
        result = num1 + num2
    elif operation == "-":
        result = num1 - num2
    elif operation == "×":
        result = num1 * num2
    elif operation == "÷":
        if num2 == 0:
            return "Error: Division by zero"
        result = num1 / num2
    elif operation == "^":
        result = num1 ** num2
    else:
        return "Unknown operation"

    return f"Result: {result}"


# ---------- Function Plotter ----------
def plot_function(expression, x_min, x_max):
    try:
        x = np.linspace(float(x_min), float(x_max), 400)

        allowed_names = {
            "x": x, "np": np, "sin": np.sin, "cos": np.cos, "tan": np.tan,
            "exp": np.exp, "log": np.log, "sqrt": np.sqrt, "pi": np.pi,
            "abs": np.abs
        }
        y = eval(expression, {"__builtins__": {}}, allowed_names)

        fig, ax = plt.subplots(figsize=(6, 4))
        ax.plot(x, y, color="#2563eb", linewidth=2)
        ax.axhline(0, color="black", linewidth=0.5)
        ax.axvline(0, color="black", linewidth=0.5)
        ax.set_title(f"y = {expression}")
        ax.grid(True, alpha=0.3)
        return fig
    except Exception as e:
        fig, ax = plt.subplots(figsize=(6, 4))
        ax.text(0.5, 0.5, f"Error: {e}", ha="center", va="center", color="red")
        ax.axis("off")
        return fig


with gr.Blocks(title="Calculator + Graph Plotter") as demo:
    gr.Markdown("# 🧮 Calculator & Function Plotter")

    with gr.Tab("Basic Calculator"):
        with gr.Row():
            num1 = gr.Number(label="First Number")
            operation = gr.Dropdown(["+", "-", "×", "÷", "^"], label="Operation", value="+")
            num2 = gr.Number(label="Second Number")
        calc_btn = gr.Button("Calculate", variant="primary")
        calc_output = gr.Textbox(label="Output")
        calc_btn.click(calculate, inputs=[num1, operation, num2], outputs=calc_output)

    with gr.Tab("Function Plotter"):
        expr = gr.Textbox(label="Function of x (e.g. sin(x), x**2 + 3*x - 1)", value="sin(x)")
        with gr.Row():
            x_min = gr.Number(label="x min", value=-10)
            x_max = gr.Number(label="x max", value=10)
        plot_btn = gr.Button("Plot", variant="primary")
        plot_output = gr.Plot(label="Graph")
        plot_btn.click(plot_function, inputs=[expr, x_min, x_max], outputs=plot_output)

demo.launch()
</gradio-file>
    </gradio-lite>
</body>
</html>