Skip to content

Commit

Permalink
Added NiceGUI samples
Browse files Browse the repository at this point in the history
  • Loading branch information
mafshin committed Oct 29, 2023
1 parent 8481c6c commit 83a4800
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
Empty file added problems/P001/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions problems/P001/p001_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from nicegui import ui

from P001.p001 import solution

class Options:
def __init__(self):
self.visible = False
self.first = True

options = Options()

def show_ui(visible):
options.visible = visible
if options.first:
options.first = False
root = create_ui()

def create_ui():
with ui.column().bind_visibility_from(options, 'visible') as root:
numberInput = ui.input('Number')
ui.button('Check Number', on_click=lambda: check_number())
output = ui.label()

def check_number():
number = int(numberInput.value)
result = solution(number)
output.text = result

return root

if __name__ in {"__main__", "__mp_main__"}:
show_ui(True)
ui.run()
Empty file added problems/P002/__init__.py
Empty file.
47 changes: 47 additions & 0 deletions problems/P002/p002_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from nicegui import ui
from P001.p001 import *
from P002.p002 import add, divide, multiply, subtract

class Options:
def __init__(self):
self.visible = False
self.first = True

options = Options()

def show_ui(visible):
options.visible = visible
if options.first:
options.first = False
create_ui()

def create_ui():
with ui.column().bind_visibility_from(options, 'visible') as root:
numberInput1 = ui.input('Number 1')
numberInput2 = ui.input('Number 2')
with ui.row():
ui.button('+', on_click=lambda e: calculate(e))
ui.button('-', on_click=lambda e: calculate(e))
ui.button('/', on_click=lambda e: calculate(e))
ui.button('*', on_click=lambda e: calculate(e))
output = ui.label()

def calculate(e):
operation = e.sender.text
number1 = int(numberInput1.value)
number2 = int(numberInput2.value)

if operation == '+':
result = add(number1, number2)
elif operation == '-':
result = subtract(number1, number2)
elif operation == '/':
result = divide(number1, number2)
elif operation == '*':
result = multiply(number1, number2)

output.text = result

if __name__ in {"__main__", "__mp_main__"}:
show_ui(True)
ui.run()
29 changes: 29 additions & 0 deletions problems/problems_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from nicegui import ui
from P001 import p001_ui
from P002 import p002_ui

class Problem:
def __init__(self):
self.number = 1

def gen_dict(a, b):
return dict(map(lambda x: (x, str(x)),range(a, b + 1)))

def show_problem(problem_number):
problem.visible = problem_number > 0

p001_ui.show_ui(problem_number == 1)
p002_ui.show_ui(problem_number == 2)


problem = Problem()

with ui.row():
with ui.column():
ui.label('Select a problem:')
problems = gen_dict(1, 50)
ui.select(problems, on_change=lambda e: show_problem(e.value)).bind_value(problem, 'number')
ui.separator()


ui.run()

0 comments on commit 83a4800

Please sign in to comment.