nice_gui.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import traceback
  2. import justpy as jp
  3. from icecream import ic
  4. from contextlib import contextmanager
  5. class NiceGui():
  6. def __init__(self):
  7. self.context = [jp.WebPage(delete_flag=False)]
  8. def build():
  9. return self.context[0]
  10. jp.justpy(build, start_server=False)
  11. @property
  12. def current(self):
  13. return self.context[-1]
  14. def label(self, text):
  15. p = jp.P(text=text, a=self.current, classes='w-48 text-xl p-1 m-2')
  16. def button(self, text, on_click=None):
  17. def click(self, _):
  18. try:
  19. on_click(self)
  20. except:
  21. traceback.print_exc()
  22. d = jp.Div(text=text, a=self.current, classes='w-48 text-xl m-2 p-1 bg-blue-700 text-white text-center')
  23. d.on('click', click)
  24. @contextmanager
  25. def column(self):
  26. d = jp.Div(a=self.current, classes='flex flex-wrap')
  27. self.context.append(d)
  28. yield
  29. self.context.pop()
  30. nice_gui = NiceGui()
  31. ui = jp.app
  32. # bind methods to simplify API -- justpy creates an app which must be found by uvicorn via string "module:attribute"
  33. ui.label = nice_gui.label
  34. ui.button = nice_gui.button
  35. ui.column = nice_gui.column