Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit d76bc8a

Browse files
committed
feat: added gui for the computer algebra app using dearpygui
1 parent cd42479 commit d76bc8a

3 files changed

Lines changed: 128 additions & 1 deletion

File tree

projects/computer-algebra/main.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
from newton_command import NEWTON_CMDS_DICT
2+
3+
import dearpygui.dearpygui as dpg
4+
5+
6+
def selection_cb(sender, app_data, user_data):
7+
if user_data[1]:
8+
print("User selected 'Ok'")
9+
else:
10+
print("User selected 'Cancel'")
11+
12+
# delete window
13+
dpg.delete_item(user_data[0])
14+
15+
16+
def show_info(title, message, selection_callback):
17+
# Reference: https://github.com/hoffstadt/DearPyGui/discussions/1002
18+
19+
# guarantee these commands happen in the same frame
20+
with dpg.mutex():
21+
viewport_width = dpg.get_viewport_client_width()
22+
viewport_height = dpg.get_viewport_client_height()
23+
24+
with dpg.window(tag='popup-window', label=title, modal=True, no_close=True) as modal_id:
25+
dpg.add_text(message)
26+
dpg.add_button(label="Ok", width=75, user_data=(modal_id, True), callback=selection_callback)
27+
dpg.add_same_line()
28+
dpg.add_button(label="Cancel", width=75, user_data=(modal_id, False), callback=selection_callback)
29+
30+
# guarantee these commands happen in another frame
31+
dpg.split_frame()
32+
width = dpg.get_item_width(modal_id)
33+
height = dpg.get_item_height(modal_id)
34+
dpg.set_item_pos(modal_id, [viewport_width // 2 - width // 2, viewport_height // 2 - height // 2])
35+
36+
37+
# Callbacks and Helpers
38+
def on_evaluate(sender, app_data, user_data):
39+
# Get the Command
40+
cmd = dpg.get_value('radio-cmds')
41+
cmd_func = NEWTON_CMDS_DICT[cmd]
42+
43+
# Get the Expression
44+
expr = dpg.get_value('inp-expr')
45+
46+
if expr.strip() in ['']:
47+
show_info(
48+
'Error',
49+
'Please use valid mathematical expressions.',
50+
selection_cb
51+
)
52+
# Clear Expression
53+
dpg.set_value('inp-expr', '')
54+
return
55+
56+
# Evaluate
57+
response = cmd_func(expr)
58+
result = response.result
59+
60+
dpg.set_value('label-output', result)
61+
62+
63+
dpg.create_context()
64+
dpg.create_viewport(title='Computer Algebra', width=1300, height=750)
65+
66+
with dpg.window(tag='inp-window',
67+
label="Input",
68+
pos=[0, 0],
69+
autosize=True,
70+
# width=1150,
71+
# height=350,
72+
no_collapse=True,
73+
no_close=True,
74+
):
75+
# Radio Button for Commands
76+
dpg.add_radio_button(
77+
horizontal=True,
78+
tag='radio-cmds',
79+
items=[cmd for cmd in NEWTON_CMDS_DICT.keys()]
80+
)
81+
82+
# Text Area for Mathematical Expression
83+
dpg.add_input_text(
84+
tag='inp-expr',
85+
width=int(1150 * 0.8),
86+
)
87+
88+
# Button for Evaluating Command and Expression
89+
dpg.add_button(label="Evaluate", callback=on_evaluate)
90+
91+
with dpg.window(tag='out-window',
92+
pos=[0, 100],
93+
label="Output",
94+
# width=700,
95+
# height=350,
96+
autosize=True,
97+
no_collapse=True,
98+
no_close=True,
99+
):
100+
# Use Label for Output
101+
dpg.add_text(tag='label-output',
102+
label='Result',
103+
show_label=True,
104+
)
105+
106+
dpg.setup_dearpygui()
107+
dpg.show_viewport()
108+
dpg.start_dearpygui()
109+
dpg.destroy_context()

projects/computer-algebra/newton_command.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,21 @@ def command(self, expr: str) -> NewtonResponse:
7474
newton_arc_tan = NewtonCommand('arctan').command
7575
newton_abs = NewtonCommand('abs').command
7676
newton_log = NewtonCommand('log').command
77+
78+
NEWTON_CMDS_DICT = {
79+
'simplify': newton_simplify,
80+
'factor': newton_factor,
81+
'derive': newton_derive,
82+
'integrate': newton_integrate,
83+
'zeroes': newton_zeroes,
84+
'tangent': newton_tangent,
85+
'area': newton_area,
86+
'cos': newton_cos,
87+
'sin': newton_sin,
88+
'tan': newton_tan,
89+
'arccos': newton_arc_cos,
90+
'arcsin': newton_arc_sin,
91+
'arctan': newton_arc_tan,
92+
'abs': newton_abs,
93+
'log': newton_log
94+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
requests==2.31.0
2-
beautifulsoup4==4.12.3
2+
dearpygui==1.11.1

0 commit comments

Comments
 (0)