instaui 0.1.19__py2.py3-none-any.whl → 0.2.0__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.
@@ -20,7 +20,7 @@ class Component(Jsonable):
20
20
  "Not allowed to create element outside of ui.page"
21
21
  )
22
22
 
23
- self.tag = (
23
+ self._tag = (
24
24
  "div"
25
25
  if tag is None or tag == ""
26
26
  else (
@@ -41,7 +41,6 @@ class Component(Jsonable):
41
41
  self._slot_manager.default.__exit__(*_)
42
42
 
43
43
  def _to_json_dict(self) -> Dict:
44
- data = super()._to_json_dict()
45
- data["tag"] = self.tag
46
-
47
- return data
44
+ return {
45
+ "tag": self._tag,
46
+ }
@@ -5,15 +5,15 @@ from typing import (
5
5
  Optional,
6
6
  )
7
7
  from instaui.components.element import Element
8
-
9
8
  from instaui.event.event_mixin import EventMixin
10
9
  from instaui.vars.types import TMaybeRef
10
+ from instaui.components.mixins import CanDisabledMixin
11
11
 
12
12
  if TYPE_CHECKING:
13
13
  pass
14
14
 
15
15
 
16
- class Button(Element):
16
+ class Button(Element, CanDisabledMixin):
17
17
  def __init__(
18
18
  self,
19
19
  text: Optional[TMaybeRef[str]] = None,
@@ -2,14 +2,14 @@ from __future__ import annotations
2
2
  from typing import TYPE_CHECKING, Optional, Union
3
3
  from instaui.components.element import Element
4
4
  from instaui.components.value_element import ValueElement
5
-
5
+ from instaui.components.mixins import CanDisabledMixin
6
6
  from ._mixins import InputEventMixin
7
7
 
8
8
  if TYPE_CHECKING:
9
9
  from instaui.vars.types import TMaybeRef
10
10
 
11
11
 
12
- class Input(InputEventMixin, ValueElement[str]):
12
+ class Input(InputEventMixin, CanDisabledMixin, ValueElement[str]):
13
13
  def __init__(
14
14
  self,
15
15
  value: Union[str, TMaybeRef[str], None] = None,
@@ -9,11 +9,12 @@ from instaui.components.element import Element
9
9
  from instaui.event.event_mixin import EventMixin
10
10
  from instaui.vars.types import TMaybeRef
11
11
  from instaui.components.vfor import VFor
12
+ from instaui.components.mixins import CanDisabledMixin
12
13
 
13
14
  _T_Select_Value = Union[List[str], str]
14
15
 
15
16
 
16
- class Select(ValueElement[Union[List[str], str]]):
17
+ class Select(CanDisabledMixin, ValueElement[Union[List[str], str]]):
17
18
  def __init__(
18
19
  self,
19
20
  value: Union[_T_Select_Value, TMaybeRef[_T_Select_Value], None] = None,
@@ -48,7 +49,7 @@ class Select(ValueElement[Union[List[str], str]]):
48
49
 
49
50
  return select
50
51
 
51
- class Option(Element):
52
+ class Option(Element, CanDisabledMixin):
52
53
  def __init__(
53
54
  self,
54
55
  text: Optional[TMaybeRef[str]] = None,
@@ -2,14 +2,14 @@ from __future__ import annotations
2
2
  from typing import TYPE_CHECKING, Optional, Union
3
3
  from instaui.components.element import Element
4
4
  from instaui.components.value_element import ValueElement
5
-
5
+ from instaui.components.mixins import CanDisabledMixin
6
6
  from ._mixins import InputEventMixin
7
7
 
8
8
  if TYPE_CHECKING:
9
9
  from instaui.vars.types import TMaybeRef
10
10
 
11
11
 
12
- class Textarea(InputEventMixin, ValueElement[str]):
12
+ class Textarea(InputEventMixin, CanDisabledMixin, ValueElement[str]):
13
13
  def __init__(
14
14
  self,
15
15
  value: Union[str, TMaybeRef[str], None] = None,
@@ -0,0 +1,12 @@
1
+ from typing import Dict, Protocol
2
+ from typing_extensions import Self
3
+ from instaui.vars.types import TMaybeRef
4
+
5
+
6
+ class PropsProtocol(Protocol):
7
+ def props(self, props: Dict) -> Self: ...
8
+
9
+
10
+ class CanDisabledMixin:
11
+ def disabled(self: PropsProtocol, disabled: TMaybeRef[bool] = True):
12
+ return self.props({"disabled": disabled})
@@ -5,15 +5,15 @@ from instaui.common.jsonable import Jsonable
5
5
  from instaui.runtime._app import get_current_scope, get_app_slot
6
6
  from instaui.vars.mixin_types.py_binding import CanInputMixin, CanOutputMixin
7
7
  from instaui.handlers import event_handler
8
+ from instaui import pre_setup as _pre_setup
8
9
  from .event_mixin import EventMixin
9
10
 
11
+
10
12
  _SYNC_TYPE = "sync"
11
13
  _ASYNC_TYPE = "async"
12
14
 
13
15
  P = ParamSpec("P")
14
16
  R = typing.TypeVar("R")
15
- _T_input = typing.TypeVar("_T_input")
16
- _T_output = typing.TypeVar("_T_output")
17
17
 
18
18
 
19
19
  class WebEvent(Jsonable, EventMixin, typing.Generic[P, R]):
@@ -22,10 +22,15 @@ class WebEvent(Jsonable, EventMixin, typing.Generic[P, R]):
22
22
  fn: typing.Callable[P, R],
23
23
  inputs: typing.Sequence[CanInputMixin],
24
24
  outputs: typing.Sequence[CanOutputMixin],
25
+ pre_setup: typing.Optional[typing.Dict] = None,
25
26
  ):
27
+ if pre_setup:
28
+ _pre_setup._check_args(pre_setup)
29
+
26
30
  self._inputs = inputs
27
31
  self._outputs = outputs
28
32
  self._fn = fn
33
+ self._pre_setup = pre_setup
29
34
 
30
35
  scope = get_current_scope()
31
36
  self._sid = scope.id
@@ -73,6 +78,9 @@ class WebEvent(Jsonable, EventMixin, typing.Generic[P, R]):
73
78
  if self._outputs:
74
79
  data["set"] = [ref._to_output_config() for ref in self._outputs]
75
80
 
81
+ if self._pre_setup:
82
+ data["preSetup"] = _pre_setup.convert_config(self._pre_setup)
83
+
76
84
  return data
77
85
 
78
86
 
@@ -80,6 +88,7 @@ def event(
80
88
  *,
81
89
  inputs: typing.Optional[typing.Sequence] = None,
82
90
  outputs: typing.Optional[typing.Sequence] = None,
91
+ pre_setup: typing.Optional[typing.Dict] = None,
83
92
  ):
84
93
  """
85
94
  Creates an event handler decorator for binding reactive logic to component events.
@@ -92,6 +101,8 @@ def event(
92
101
  outputs (typing.Optional[typing.Sequence], optional): Targets (state variables, UI elements) that should
93
102
  update when this handler executes. Used for coordinating
94
103
  interface updates after the event is processed.
104
+ pre_setup (typing.Optional[typing.Dict], optional): A dictionary of pre-setup actions to be executed before the event executes.
105
+
95
106
 
96
107
  # Example:
97
108
  .. code-block:: python
@@ -106,6 +117,17 @@ def event(
106
117
  html.button("click me").on_click(plus_one)
107
118
  html.paragraph(a)
108
119
 
120
+ use pre_setup:
121
+ .. code-block:: python
122
+ a = ui.state(0)
123
+ task_running = ui.state(False)
124
+
125
+ @ui.event(inputs=[a], outputs=[a], pre_setup={task_running: True})
126
+ async def long_running_task(a):
127
+ await asyncio.sleep(3)
128
+ return a + 1
129
+
130
+ html.button("click me").on_click(long_running_task).disabled(task_running)
109
131
  """
110
132
 
111
133
  def wrapper(func: typing.Callable[P, R]):
@@ -113,6 +135,7 @@ def event(
113
135
  func,
114
136
  inputs or [],
115
137
  outputs=outputs or [],
138
+ pre_setup=pre_setup,
116
139
  )
117
140
 
118
141
  return wrapper
@@ -17,3 +17,15 @@ class RequestContextMiddleware(BaseHTTPMiddleware):
17
17
  reset_app_slot(system_slot_token)
18
18
 
19
19
  return response
20
+
21
+
22
+ class NoCacheDebugModeMiddleware(BaseHTTPMiddleware):
23
+ async def dispatch(self, request: Request, call_next: Callable) -> Any:
24
+ response = await call_next(request)
25
+
26
+ if request.url.path.endswith((".js", ".css")):
27
+ response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
28
+ response.headers["Pragma"] = "no-cache"
29
+ response.headers["Expires"] = "0"
30
+
31
+ return response
@@ -22,6 +22,7 @@ from instaui.page_info import PageInfo
22
22
 
23
23
  from instaui import consts
24
24
  from instaui.runtime._app import get_app_slot, get_default_app_slot
25
+ from instaui.runtime.context import get_context
25
26
  from instaui.runtime.dataclass import JsLink, VueAppComponent
26
27
  from instaui.template import web_template
27
28
 
@@ -30,7 +31,7 @@ from . import dependency_router
30
31
  from . import event_router
31
32
  from . import watch_router
32
33
  from . import debug_mode_router
33
- from .middlewares import RequestContextMiddleware
34
+ from .middlewares import RequestContextMiddleware, NoCacheDebugModeMiddleware
34
35
  from ._uvicorn import UvicornServer
35
36
  from . import resource
36
37
  from instaui.version import __version__ as _INSTA_VERSION
@@ -55,6 +56,8 @@ class Server:
55
56
  ):
56
57
  self.app = FastAPI()
57
58
  self.app.add_middleware(RequestContextMiddleware)
59
+ if get_context().debug_mode:
60
+ self.app.add_middleware(NoCacheDebugModeMiddleware)
58
61
 
59
62
  if use_gzip:
60
63
  self.app.add_middleware(
@@ -130,6 +133,7 @@ class Server:
130
133
  favicon_url = resource.record_resource(default_html_resource.favicon)
131
134
 
132
135
  model = web_template.WebTemplateModel(
136
+ version=_INSTA_VERSION,
133
137
  vue_js_link=VUE_JS_HASH_LINK,
134
138
  instaui_js_link=INSTAUI_JS_HASH_LINK,
135
139
  css_links=[
instaui/js/fn.py CHANGED
@@ -21,12 +21,13 @@ class JsFn(Jsonable, CanInputMixin):
21
21
  ui.label(result)
22
22
  """
23
23
 
24
- def __init__(self, code: str):
24
+ def __init__(self, code: str, *, execute_immediately=False):
25
25
  self.code = code
26
26
  self.__type = "jsFn"
27
27
  app = get_app_slot()
28
28
  app.register_js_fn(self)
29
29
  self.__id = app.generate_js_fn_id()
30
+ self._execute_immediately = execute_immediately
30
31
 
31
32
  def _to_input_config(self):
32
33
  return {
@@ -39,4 +40,7 @@ class JsFn(Jsonable, CanInputMixin):
39
40
  data["type"] = self.__type
40
41
  data["id"] = self.__id
41
42
 
43
+ if self._execute_immediately is True:
44
+ data["immediately"] = 1
45
+
42
46
  return data
instaui/pre_setup.py ADDED
@@ -0,0 +1,45 @@
1
+ from typing import Dict, Sequence, cast
2
+ from instaui.common.jsonable import Jsonable
3
+ from instaui.vars.mixin_types.py_binding import CanOutputMixin, CanInputMixin
4
+
5
+
6
+ def _check_args(config: Dict):
7
+ for key in config.keys():
8
+ if not isinstance(key, CanOutputMixin):
9
+ raise TypeError(f"key {key} is not a CanOutputMixin")
10
+
11
+
12
+ def convert_config(config: Dict):
13
+ return [
14
+ {
15
+ "target": cast(CanInputMixin, key)._to_input_config(),
16
+ **value._to_json_dict(),
17
+ }
18
+ if isinstance(value, PreSetupAction)
19
+ else {
20
+ "type": "const",
21
+ "target": cast(CanInputMixin, key)._to_input_config(),
22
+ "value": value,
23
+ }
24
+ for key, value in config.items()
25
+ ]
26
+
27
+
28
+ class PreSetupAction(Jsonable):
29
+ def __init__(self, *, inputs: Sequence, code: str, reset: bool = True):
30
+ self.type = "action"
31
+ self._inputs = inputs
32
+ self.code = code
33
+ self.reset = reset
34
+
35
+ def _to_json_dict(self):
36
+ data = super()._to_json_dict()
37
+ if self._inputs:
38
+ data["inputs"] = [
39
+ binding._to_input_config()
40
+ if isinstance(binding, CanInputMixin)
41
+ else binding
42
+ for binding in self._inputs
43
+ ]
44
+
45
+ return data
instaui/runtime/_app.py CHANGED
@@ -30,7 +30,7 @@ class App(Jsonable):
30
30
  self._vfor_id_counter = 0
31
31
  self._slot_id_counter = 0
32
32
  self._js_fn_id_counter = 0
33
- self.mode: _T_App_Mode = mode
33
+ self._mode: _T_App_Mode = mode
34
34
  self.items: List[Component] = []
35
35
  self.meta = meta
36
36
  self._slots_stacks: List[Slot] = []
@@ -49,6 +49,10 @@ class App(Jsonable):
49
49
  self._route_collector: Optional[RouteCollector] = None
50
50
  self._js_fns: List[JsFn] = []
51
51
 
52
+ @property
53
+ def mode(self) -> _T_App_Mode:
54
+ return self._mode
55
+
52
56
  @property
53
57
  def page_path(self) -> str:
54
58
  assert self._page_path is not None, "Page path is not set"