Update app.py
Browse files
app.py
CHANGED
|
@@ -1,189 +1,80 @@
|
|
| 1 |
-
import
|
| 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 |
try:
|
| 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 |
-
toolbar = NavigationToolbar2Tk(canvas, graph_frame)
|
| 70 |
-
|
| 71 |
-
toolbar.update()
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
canvas.get_tk_widget().pack()
|
| 76 |
-
|
| 77 |
-
solution_label.config(text = "Solution : x₁ = {0}, x₂ = {1}".format(eqn.solveQuad()[0], eqn.solveQuad()[1]))
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
except:
|
| 82 |
-
|
| 83 |
-
Messagebox.show_error(title = "Error", message = "User entered wrong value")
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
# Base window widget
|
| 92 |
-
|
| 93 |
-
root = tb.Window(themename="vapor")
|
| 94 |
-
|
| 95 |
-
root.geometry("720x720")
|
| 96 |
-
|
| 97 |
-
root.title("Quadratic Equation Solver")
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
# Font data
|
| 102 |
-
|
| 103 |
-
font = ("Nunito", 12)
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
# Frame containing the entry for the three arguments
|
| 108 |
-
|
| 109 |
-
top_frame = tb.Frame(root)
|
| 110 |
-
|
| 111 |
-
top_frame.grid(row = 0, column = 0,padx = 10, pady = 20)
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
# Entry for the three arguments
|
| 116 |
-
|
| 117 |
-
a_frame = tb.Frame(top_frame)
|
| 118 |
-
|
| 119 |
-
a_frame.grid(row = 0, column = 0, padx=5)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
a_label = tb.Label(a_frame, text= "a =", font = font)
|
| 124 |
-
|
| 125 |
-
a_label.grid(row = 0, column = 0)
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
a_entry = tb.Entry(a_frame, width = 30, font = font)
|
| 130 |
-
|
| 131 |
-
a_entry.grid(row = 0, column = 1)
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
b_frame = tb.Frame(top_frame)
|
| 136 |
-
|
| 137 |
-
b_frame.grid(row = 0, column = 1, padx=5)
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
b_label = tb.Label(b_frame, text = "b =", font = font)
|
| 142 |
-
|
| 143 |
-
b_label.grid(row = 0, column = 0)
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
b_entry = tb.Entry(b_frame, width = 30, font = font)
|
| 148 |
-
|
| 149 |
-
b_entry.grid(row = 0, column = 1)
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
c_frame = tb.Frame(top_frame)
|
| 154 |
-
|
| 155 |
-
c_frame.grid(row = 0, column = 2, padx=5)
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
c_label = tb.Label(c_frame, text = "c =", font = font)
|
| 160 |
-
|
| 161 |
-
c_label.grid(row = 0, column = 0)
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
c_entry = tb.Entry(c_frame, width = 30, font = font)
|
| 166 |
-
|
| 167 |
-
c_entry.grid(row = 0, column = 1)
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
# Button to plot the matplotlib graph
|
| 174 |
-
|
| 175 |
-
plot_button = tb.Button(top_frame, width = 70, text = "Plot", command = plot)
|
| 176 |
-
|
| 177 |
-
plot_button.grid(row = 1, column = 1, pady = 15)
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
# Label containing the solution to the equation
|
| 182 |
-
|
| 183 |
-
solution_label = tb.Label(root,font = (font[0], 15), text = "")
|
| 184 |
-
|
| 185 |
-
solution_label.grid(row = 2, column = 0, pady = 10)
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
root.mainloop()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cmath
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class Quadratic:
|
| 8 |
+
"""Representing a quadratic expression in the form of ax² + bx + c"""
|
| 9 |
+
|
| 10 |
+
def __init__(self, a: float, b: float, c: float) -> None:
|
| 11 |
+
if all(isinstance(i, (int, float)) for i in (a, b, c)):
|
| 12 |
+
if a == 0:
|
| 13 |
+
raise ValueError("Coefficient 'a' cannot be 0 for a quadratic equation.")
|
| 14 |
+
self.a = a
|
| 15 |
+
self.b = b
|
| 16 |
+
self.c = c
|
| 17 |
+
else:
|
| 18 |
+
raise ValueError("Arguments must be int or float")
|
| 19 |
+
|
| 20 |
+
def __repr__(self) -> str:
|
| 21 |
+
return f"{self.a}x² + {self.b}x + {self.c}"
|
| 22 |
+
|
| 23 |
+
def solveQuad(self):
|
| 24 |
+
"""Returns roots (real or complex)"""
|
| 25 |
+
discriminant = self.b**2 - 4 * self.a * self.c
|
| 26 |
+
root1 = (-self.b + cmath.sqrt(discriminant)) / (2 * self.a)
|
| 27 |
+
root2 = (-self.b - cmath.sqrt(discriminant)) / (2 * self.a)
|
| 28 |
+
return root1, root2
|
| 29 |
+
|
| 30 |
+
def evaluate(self, value):
|
| 31 |
+
return (self.a * value**2) + (self.b * value) + self.c
|
| 32 |
+
|
| 33 |
+
def drawFigure(self):
|
| 34 |
+
x_axis = np.linspace(-10, 10, 200)
|
| 35 |
+
y_axis = self.evaluate(x_axis)
|
| 36 |
+
|
| 37 |
+
fig, ax = plt.subplots()
|
| 38 |
+
ax.plot(x_axis, y_axis)
|
| 39 |
+
ax.axhline(0)
|
| 40 |
+
ax.axvline(0)
|
| 41 |
+
ax.grid(True)
|
| 42 |
+
ax.set_title(str(self))
|
| 43 |
+
|
| 44 |
+
return fig
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# -------- Gradio Function --------
|
| 48 |
+
def solve_and_plot(a, b, c):
|
| 49 |
try:
|
| 50 |
+
quad = Quadratic(a, b, c)
|
| 51 |
+
root1, root2 = quad.solveQuad()
|
| 52 |
+
fig = quad.drawFigure()
|
| 53 |
+
|
| 54 |
+
return (
|
| 55 |
+
f"Equation: {quad}\n\nRoot 1: {root1}\nRoot 2: {root2}",
|
| 56 |
+
fig
|
| 57 |
+
)
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Error: {str(e)}", None
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# -------- Gradio UI --------
|
| 63 |
+
interface = gr.Interface(
|
| 64 |
+
fn=solve_and_plot,
|
| 65 |
+
inputs=[
|
| 66 |
+
gr.Number(label="Coefficient a"),
|
| 67 |
+
gr.Number(label="Coefficient b"),
|
| 68 |
+
gr.Number(label="Coefficient c"),
|
| 69 |
+
],
|
| 70 |
+
outputs=[
|
| 71 |
+
gr.Textbox(label="Solution"),
|
| 72 |
+
gr.Plot(label="Graph"),
|
| 73 |
+
],
|
| 74 |
+
title="Quadratic Equation Solver",
|
| 75 |
+
description="Solve ax² + bx + c = 0 and visualize its graph."
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# -------- Launch --------
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|