Explorar o código

proof-of-concept for new reloading approach

Falko Schindler %!s(int64=4) %!d(string=hai) anos
pai
achega
f4ad79f14a
Modificáronse 2 ficheiros con 26 adicións e 39 borrados
  1. 4 29
      main.py
  2. 22 10
      nice_gui.py

+ 4 - 29
main.py

@@ -1,32 +1,7 @@
+#!/usr/bin/env python3
 from nice_gui import ui
-import matplotlib.pyplot as plt
-from datetime import datetime
 
-ui.label('Hello, Nice GUI!')
+ui.label('Hello')
+ui.label('world!')
 
-with ui.row() as row:
-    with row.column() as left:
-        left.label('Add an element:')
-        left.button('Button 1', on_click=lambda: left.label('Nice!'))
-    with row.column() as right:
-        right.label("Update itself:")
-        right.button('Button 2', on_click=lambda e: setattr(e.sender, 'text', e.sender.text + ' :)'))
-
-ui.select(['one', 'two', 'three'], 'one', on_change=lambda e: output.label(e.value))
-ui.radio(['alpha', 'beta', 'gamma'], 'alpha', on_change=lambda e: output.label(e.value), vertical=True)
-with ui.row() as output:
-    pass
-
-with ui.card() as card:
-
-    with card.plot():
-        plt.title('Some plot')
-        plt.plot(range(10), [x**2 for x in range(10)])
-
-    with card.row() as row:
-        row.checkbox('Let''s check...', on_change=lambda e: row.label('Check!' if e.checked else 'Uncheck.'))
-
-time = ui.label('Time:')
-def update_time():
-    time.text = f'Time: {datetime.now().strftime("%H:%M:%S")}'
-ui.timer(1.0, update_time)
+ui.run()

+ 22 - 10
nice_gui.py

@@ -1,14 +1,26 @@
+#!/usr/bin/env python3
 import justpy as jp
-from elements import Column, Page
+from starlette.applications import Starlette
+import uvicorn
+import inspect
 
-page = Page()
-content = Column(page)
+wp = jp.WebPage(delete_flag=False)
+jp.justpy(lambda: wp, start_server=False)
 
-jp.justpy(lambda: page.view, start_server=False)
-ui = jp.app
+class Ui(Starlette):
 
-# bind methods to simplify API -- justpy creates an app which must be found by uvicorn via string "module:attribute"
-for field in dir(content):
-    if field[0] != '_' and callable(attr := getattr(content, field)):
-        setattr(ui, field, attr)
-ui.timer = page.timer
+    def label(self, text):
+
+        jp.Div(text=text, a=wp)
+
+    def run(self):
+
+        # NOTE: prevent reloader to restart uvicorn
+        if inspect.stack()[-2].filename.endswith('spawn.py'):
+            return
+
+        uvicorn.run('nice_gui:ui', host='0.0.0.0', port=80, lifespan='on', reload=True)
+
+# NOTE: instantiate our own ui object with all capabilities of jp.app
+ui = Ui()
+ui.__dict__.update(jp.app.__dict__)