Pārlūkot izejas kodu

switching from classes to methods for api

Rodja Trappe 4 gadi atpakaļ
vecāks
revīzija
f83088fe99
3 mainītis faili ar 31 papildinājumiem un 30 dzēšanām
  1. 15 0
      .vscode/settings.json
  2. 6 4
      main.py
  3. 10 26
      nice_gui.py

+ 15 - 0
.vscode/settings.json

@@ -0,0 +1,15 @@
+{
+  "editor.formatOnSave": true,
+  "editor.defaultFormatter": "esbenp.prettier-vscode",
+  "editor.minimap.enabled": false,
+  "python.formatting.provider": "autopep8",
+  "[python]": {
+    "editor.defaultFormatter": "ms-python.python"
+  },
+  "python.formatting.autopep8Args": ["--max-line-length", "120"],
+  "python.testing.pytestArgs": ["."],
+  "python.testing.unittestEnabled": false,
+  "python.testing.nosetestsEnabled": false,
+  "python.testing.pytestEnabled": false,
+  "python.testing.promptToConfigure": false
+}

+ 6 - 4
main.py

@@ -1,8 +1,10 @@
-import nice_gui as ui
+from nice_gui import NiceGui
 from icecream import ic
 
-app = ui.app()
+ui = NiceGui()
 
-ui.Label('Hello Nice GUI!')
+ui.label('Hello, Nice GUI!')
 
-ui.Button('BUTTON', on_click=lambda _: ui.Label('Nice!'))
+ui.button('BUTTON', on_click=lambda _: ui.label('Nice!'))
+
+app = ui.app

+ 10 - 26
nice_gui.py

@@ -2,39 +2,23 @@ import traceback
 import justpy as jp
 from icecream import ic
 
-pages = []
 
-
-def app():
-
-    pages.append(jp.WebPage(delete_flag=False))
-
-    def build():
-        return pages[0]
-
-    jp.justpy(build, start_server=False)
-    return jp.app
-
-
-class Widget:
+class NiceGui():
 
     def __init__(self):
-        pass
-
-
-class Label(Widget):
-
-    def __init__(self, text):
+        self.index = jp.WebPage(delete_flag=False)
 
-        super().__init__()
-        p = jp.P(text=text, a=pages[0], classes='w-48 text-xl p-1 m-2')
+        def build():
+            return self.index
 
+        jp.justpy(build, start_server=False)
+        self.app = jp.app
 
-class Button(Widget):
+    def label(self, text):
 
-    def __init__(self, text, on_click=None):
+        p = jp.P(text=text, a=self.index, classes='w-48 text-xl p-1 m-2')
 
-        super().__init__()
+    def button(self, text, on_click=None):
 
         def click(self, _):
             try:
@@ -42,5 +26,5 @@ class Button(Widget):
             except:
                 traceback.print_exc()
 
-        d = jp.Div(text=text, a=pages[0], classes='w-48 text-xl m-2 p-1 bg-blue-700 text-white text-center')
+        d = jp.Div(text=text, a=self.index, classes='w-48 text-xl m-2 p-1 bg-blue-700 text-white text-center')
         d.on('click', click)