Kaynağa Gözat

#782 support refreshable class methods

Falko Schindler 2 yıl önce
ebeveyn
işleme
075640f0ad
1 değiştirilmiş dosya ile 10 ekleme ve 3 silme
  1. 10 3
      nicegui/functions/refreshable.py

+ 10 - 3
nicegui/functions/refreshable.py

@@ -1,5 +1,7 @@
 from typing import Callable, List
 from typing import Callable, List
 
 
+from typing_extensions import Self
+
 from ..element import Element
 from ..element import Element
 
 
 
 
@@ -12,15 +14,20 @@ class refreshable:
         This method will automatically delete all elements created by the function and recreate them.
         This method will automatically delete all elements created by the function and recreate them.
         """
         """
         self.func = func
         self.func = func
+        self.instance = None
         self.containers: List[Element] = []
         self.containers: List[Element] = []
 
 
-    def __call__(self, *args, **kwargs) -> None:
+    def __get__(self, instance, _) -> Self:
+        self.instance = instance
+        return self
+
+    def __call__(self) -> None:
         with Element('div') as container:
         with Element('div') as container:
-            self.func(*args, **kwargs)
+            self.func() if self.instance is None else self.func(self.instance)
         self.containers.append(container)
         self.containers.append(container)
 
 
     def refresh(self) -> None:
     def refresh(self) -> None:
         for container in self.containers:
         for container in self.containers:
             container.clear()
             container.clear()
             with container:
             with container:
-                self.func()
+                self.func() if self.instance is None else self.func(self.instance)