nice_gui.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. import traceback
  3. import justpy as jp
  4. from starlette.applications import Starlette
  5. import uvicorn
  6. import inspect
  7. import asyncio
  8. pad = '*' * 80
  9. if not inspect.stack()[-2].filename.endswith('spawn.py'):
  10. print(pad, "START UVICORN")
  11. uvicorn.run('nicegui:ui', host='0.0.0.0', port=80, lifespan='on', reload=True)
  12. wp = jp.WebPage(delete_flag=False, head_html='<script>confirm = () => true;</script>')
  13. main = jp.Div(a=wp, text='Hello JustPy!')
  14. main.add_page(wp)
  15. jp.justpy(lambda: wp, start_server=False)
  16. class Ui(Starlette):
  17. def __init__(self):
  18. self.__dict__.update(jp.app.__dict__)
  19. self.tasks = []
  20. @self.on_event('startup')
  21. def startup():
  22. print(pad, __name__, "startup()")
  23. [jp.run_task(t) for t in self.tasks]
  24. def label(self, text):
  25. print(pad, __name__, "label()")
  26. view = jp.Div(text=text)
  27. main.add(view)
  28. view.add_page(wp)
  29. def timer(self):
  30. print(pad, __name__, "timer()")
  31. async def loop():
  32. while True:
  33. print(pad, __name__, "loop()", flush=True)
  34. await asyncio.sleep(1.0)
  35. self.tasks.append(loop())
  36. ui = Ui()