nice_gui.py 1.1 KB

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