instaui 0.1.16__py2.py3-none-any.whl → 0.1.18__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  from __future__ import annotations
2
- from typing import TYPE_CHECKING, Optional, Union
2
+ from typing import TYPE_CHECKING, Optional
3
3
  from instaui.components.element import Element
4
4
  from instaui.components.value_element import ValueElement
5
5
  from ._mixins import InputEventMixin
instaui/js/fn.py ADDED
@@ -0,0 +1,42 @@
1
+ from instaui.common.jsonable import Jsonable
2
+ from instaui.vars.mixin_types.py_binding import CanInputMixin
3
+ from instaui.runtime._app import get_app_slot
4
+
5
+
6
+ class JsFn(Jsonable, CanInputMixin):
7
+ """
8
+ Creates a JavaScript function object from a raw code string.
9
+ Valid targets include: `js_computed`, `js_watch`, and similar JS-bound methods.
10
+
11
+ Args:
12
+ code (str): Valid JavaScript function definition string.
13
+
14
+ Example:
15
+ .. code-block:: python
16
+ a = ui.state(1)
17
+ add = ui.js_fn(code="(a,b)=> a+b ")
18
+ result = ui.js_computed(inputs=[add, a], code="(add,a)=> add(a,10) ")
19
+
20
+ html.number(a)
21
+ ui.label(result)
22
+ """
23
+
24
+ def __init__(self, code: str):
25
+ self.code = code
26
+ self.__type = "jsFn"
27
+ app = get_app_slot()
28
+ app.register_js_fn(self)
29
+ self.__id = app.generate_js_fn_id()
30
+
31
+ def _to_input_config(self):
32
+ return {
33
+ "type": self.__type,
34
+ "id": self.__id,
35
+ }
36
+
37
+ def _to_json_dict(self):
38
+ data = super()._to_json_dict()
39
+ data["type"] = self.__type
40
+ data["id"] = self.__id
41
+
42
+ return data
instaui/runtime/_app.py CHANGED
@@ -17,6 +17,7 @@ if TYPE_CHECKING:
17
17
  from instaui.dependencies.component_dependency import ComponentDependencyInfo
18
18
  from instaui.dependencies.plugin_dependency import PluginDependencyInfo
19
19
  from instaui.spa_router._route_model import RouteCollector
20
+ from instaui.js.fn import JsFn
20
21
 
21
22
 
22
23
  class App(Jsonable):
@@ -28,6 +29,7 @@ class App(Jsonable):
28
29
  self._scope_id_counter = 0
29
30
  self._vfor_id_counter = 0
30
31
  self._slot_id_counter = 0
32
+ self._js_fn_id_counter = 0
31
33
  self.mode: _T_App_Mode = mode
32
34
  self.items: List[Component] = []
33
35
  self.meta = meta
@@ -45,6 +47,7 @@ class App(Jsonable):
45
47
  self._page_params: Dict[str, Any] = {}
46
48
  self._query_params: Dict[str, Any] = {}
47
49
  self._route_collector: Optional[RouteCollector] = None
50
+ self._js_fns: List[JsFn] = []
48
51
 
49
52
  @property
50
53
  def page_path(self) -> str:
@@ -72,6 +75,13 @@ class App(Jsonable):
72
75
  self._slot_id_counter += 1
73
76
  return str(self._slot_id_counter)
74
77
 
78
+ def generate_js_fn_id(self) -> str:
79
+ self._js_fn_id_counter += 1
80
+ return str(self._js_fn_id_counter)
81
+
82
+ def register_js_fn(self, fn: JsFn) -> None:
83
+ self._js_fns.append(fn)
84
+
75
85
  def reset_html_resource(self):
76
86
  self._html_resource = HtmlResource()
77
87
 
@@ -127,6 +137,9 @@ class App(Jsonable):
127
137
  if self._web_server_info is not None:
128
138
  data["webInfo"] = self._web_server_info
129
139
 
140
+ if self._js_fns:
141
+ data["jsFn"] = self._js_fns
142
+
130
143
  return data
131
144
 
132
145
  @classmethod