dara-core 1.16.19__py3-none-any.whl → 1.16.20__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.
dara/core/defaults.py CHANGED
@@ -39,6 +39,8 @@ from dara.core.visual.components import (
39
39
  DynamicComponent,
40
40
  DynamicComponentDef,
41
41
  Fallback,
42
+ For,
43
+ ForDef,
42
44
  Menu,
43
45
  MenuDef,
44
46
  ProgressTracker,
@@ -77,6 +79,7 @@ CORE_COMPONENTS: Dict[str, ComponentTypeAnnotation] = {
77
79
  TopBarFrame.__name__: TopBarFrameDef,
78
80
  Fallback.Default.py_component: DefaultFallbackDef,
79
81
  Fallback.Row.py_component: RowFallbackDef,
82
+ For.__name__: ForDef,
80
83
  }
81
84
 
82
85
  # These actions are provided by the core JS of this module
@@ -1114,10 +1114,9 @@ class ActionCtx:
1114
1114
 
1115
1115
  ```python
1116
1116
 
1117
- from dara.core import action, ConfigurationBuilder, DataVariable, DownloadContent
1117
+ from dara.core import action, ConfigurationBuilder, DataVariable
1118
1118
  from dara.components.components import Button, Stack
1119
1119
 
1120
-
1121
1120
  # generate data, alternatively you could load it from a file
1122
1121
  df = pandas.DataFrame(data={'x': [1, 2, 3], 'y':[4, 5, 6]})
1123
1122
  my_var = DataVariable(df)
@@ -1126,18 +1125,14 @@ class ActionCtx:
1126
1125
 
1127
1126
  @action
1128
1127
  async def download_csv(ctx: action.Ctx, my_var_value: DataFrame) -> str:
1129
- # The file can be created and saved dynamically here, it should then return a string with a path to it
1130
- # To get the component value, e.g. a select component would return the selected value
1131
- component_value = ctx.input
1132
-
1133
1128
  # Getting the value of data passed as extras to the action
1134
1129
  data = my_var_value
1135
1130
 
1136
1131
  # save the data to csv
1137
1132
  data.to_csv('<PATH_TO_CSV.csv>')
1138
1133
 
1139
- # Instruct the frontend to download the file
1140
- await ctx.download_file(path='<PATH_TO_CSV.csv>', cleanup=False)
1134
+ # Instruct the frontend to download the file and clean up afterwards
1135
+ await ctx.download_file(path='<PATH_TO_CSV.csv>', cleanup=True)
1141
1136
 
1142
1137
 
1143
1138
  def test_page():
@@ -0,0 +1,88 @@
1
+ from typing import List, Optional
2
+
3
+ from pydantic import Field, SerializerFunctionWrapHandler, model_serializer
4
+
5
+ from .non_data_variable import NonDataVariable
6
+
7
+
8
+ class LoopVariable(NonDataVariable):
9
+ """
10
+ A LoopVariable is a type of variable that represents an item in a list.
11
+ It should be constructed using a parent Variable's `.list_item` property.
12
+ It should only be used in conjunction with the `For` component.
13
+
14
+ By default, the entire value is used as the item and the index in the list is used as the unique key.
15
+
16
+ ```python
17
+ from dara.core import Variable
18
+ from dara.core.visual.components import For
19
+
20
+ my_list = Variable([1, 2, 3])
21
+
22
+ # Renders a list of Text component where each item is the corresponding item in the list
23
+ For(
24
+ items=my_list,
25
+ renderer=Text(text=my_list.list_item)
26
+ )
27
+ ```
28
+
29
+ Most of the time, you'll want to store objects in a list. You should then use the `get` property to access specific
30
+ properties of the object and the `key` on the `For` component to specify the unique key.
31
+
32
+ ```python
33
+ from dara.core import Variable
34
+ from dara.core.visual.components import For
35
+
36
+ my_list = Variable([{'id': 1, 'name': 'John', 'age': 30}, {'id': 2, 'name': 'Jane', 'age': 25}])
37
+
38
+ # Renders a list of Text component where each item is the corresponding item in the list
39
+ For(
40
+ items=my_list,
41
+ renderer=Text(text=my_list.list_item.get('name')),
42
+ key_accessor='id'
43
+ )
44
+ ```
45
+
46
+ Alternatively, you can use index access instead of `get` to access specific properties of the object.
47
+ Both `get` and `[]` are equivalent.
48
+ """
49
+
50
+ nested: List[str] = Field(default_factory=list)
51
+
52
+ def __init__(self, uid: Optional[str] = None, nested: Optional[List[str]] = None):
53
+ if nested is None:
54
+ nested = []
55
+ super().__init__(uid=uid, nested=nested)
56
+
57
+ def get(self, key: str):
58
+ """
59
+ Access a nested property of the current item in the list.
60
+
61
+ ```python
62
+ from dara.core import Variable
63
+
64
+ my_list_of_objects = Variable([
65
+ {'id': 1, 'name': 'John', 'data': {'city': 'London', 'country': 'UK'}},
66
+ {'id': 2, 'name': 'Jane', 'data': {'city': 'Paris', 'country': 'France'}},
67
+ ])
68
+
69
+ # Represents the item 'name' property
70
+ my_list_of_objects.list_item.get('name')
71
+
72
+ # Represents the item 'data.country' property
73
+ my_list_of_objects.list_item.get('data').get('country')
74
+ ```
75
+ """
76
+ return self.model_copy(update={'nested': [*self.nested, key]}, deep=True)
77
+
78
+ def __getitem__(self, key: str):
79
+ return self.get(key)
80
+
81
+ @property
82
+ def list_item(self):
83
+ raise RuntimeError('LoopVariable does not support list_item')
84
+
85
+ @model_serializer(mode='wrap')
86
+ def ser_model(self, nxt: SerializerFunctionWrapHandler):
87
+ parent_dict = nxt(self)
88
+ return {**parent_dict, '__typename': 'LoopVariable', 'uid': str(parent_dict['uid'])}
@@ -34,3 +34,38 @@ class NonDataVariable(AnyVariable, abc.ABC):
34
34
 
35
35
  def __init__(self, uid: Optional[str] = None, **kwargs) -> None:
36
36
  super().__init__(uid=uid, **kwargs)
37
+
38
+ @property
39
+ def list_item(self):
40
+ """
41
+ Get a LoopVariable that represents the current item in the list.
42
+ Should only be used in conjunction with the `For` component.
43
+
44
+ Note that it is a type of a Variable so it can be used in places where a regular Variable is expected.
45
+
46
+ By default, the entire list item is used as the item.
47
+
48
+ `LoopVariable` supports nested property access using `get` or index access i.e. `[]`.
49
+ You can mix and match those two methods to access nested properties as they are equivalent.
50
+
51
+ ```python
52
+ my_list = Variable(['foo', 'bar', 'baz'])
53
+
54
+ # Represents the entire item in the list
55
+ my_list.list_item
56
+
57
+ my_list_of_objects = Variable([
58
+ {'id': 1, 'name': 'John', 'data': {'city': 'London', 'country': 'UK'}},
59
+ {'id': 2, 'name': 'Jane', 'data': {'city': 'Paris', 'country': 'France'}},
60
+ ])
61
+
62
+ # Represents the item 'name' property
63
+ my_list_of_objects.list_item['name']
64
+
65
+ # Represents the item 'data.country' property
66
+ my_list_of_objects.list_item.get('data')['country']
67
+ """
68
+
69
+ from .loop_variable import LoopVariable
70
+
71
+ return LoopVariable()
@@ -161,7 +161,7 @@ def normalize(obj: JsonLike, check_root: bool = True) -> Tuple[JsonLike, Mapping
161
161
  if check_root and _is_referrable(obj):
162
162
  identifier = _get_identifier(obj)
163
163
  # Don't check root again otherwise we end up in an infinite loop, we know it's referrable
164
- _normalized, _lookup = normalize(obj, False)
164
+ _normalized, _lookup = normalize(obj, check_root=False)
165
165
  lookup[identifier] = _normalized
166
166
  lookup.update(_lookup)
167
167
  output = Placeholder(__ref=identifier)
@@ -501,7 +501,7 @@ def create_router(config: Configuration):
501
501
  async def _write(store_uid: str, value: Any):
502
502
  WS_CHANNEL.set(ws_channel)
503
503
  store_entry: BackendStoreEntry = await registry_mgr.get(backend_store_registry, store_uid)
504
- result = store_entry.store.write(value)
504
+ result = store_entry.store.write(value, ignore_channel=ws_channel)
505
505
 
506
506
  # Backend implementation could return a coroutine
507
507
  if inspect.iscoroutine(result):
dara/core/persistence.py CHANGED
@@ -29,7 +29,6 @@ from pydantic import (
29
29
 
30
30
  from dara.core.auth.definitions import USER
31
31
  from dara.core.internal.utils import run_user_handler
32
- from dara.core.internal.websocket import WS_CHANNEL
33
32
  from dara.core.logging import dev_logger
34
33
 
35
34
  if TYPE_CHECKING:
@@ -318,7 +317,11 @@ class BackendStore(PersistenceStore):
318
317
  if not payload or len(payload) != 1:
319
318
  raise ValueError("Exactly one of 'value' or 'patches' must be provided")
320
319
 
321
- return {'store_uid': self.uid, 'sequence_number': self.sequence_number.get(scope_key, 0), **payload}
320
+ return {
321
+ 'store_uid': self.uid,
322
+ 'sequence_number': self.sequence_number.get(scope_key, 0),
323
+ **payload,
324
+ }
322
325
 
323
326
  def _get_next_sequence_number(self, key: str) -> int:
324
327
  """
@@ -330,39 +333,40 @@ class BackendStore(PersistenceStore):
330
333
  self.sequence_number[key] = current + 1
331
334
  return self.sequence_number[key]
332
335
 
333
- async def _notify_user(self, user_identifier: str, ignore_current_channel: bool = True, **payload):
336
+ async def _notify_user(self, user_identifier: str, ignore_channel: Optional[str] = None, **payload):
334
337
  """
335
338
  Notify a given user about updates to this store.
336
339
  :param user_identifier: user to notify
337
- :param ignore_current_channel: if True, ignore the current websocket channel
340
+ :param ignore_channel: if specified, ignore the specified channel
338
341
  :param payload: either value=... or patches=...
339
342
  """
340
343
  return await self.ws_mgr.send_message_to_user(
341
344
  user_identifier,
342
345
  self._create_msg(user_identifier, **payload),
343
- ignore_channel=WS_CHANNEL.get() if ignore_current_channel else None,
346
+ ignore_channel=ignore_channel,
344
347
  )
345
348
 
346
- async def _notify_global(self, ignore_current_channel: bool = True, **payload):
349
+ async def _notify_global(self, ignore_channel: Optional[str] = None, **payload):
347
350
  """
348
351
  Notify all users about updates to this store.
349
- :param ignore_current_channel: if True, ignore the current websocket channel
352
+ :param ignore_channel: if specified, ignore the specified channel
350
353
  :param payload: either value=... or patches=...
351
354
  """
352
355
  return await self.ws_mgr.broadcast(
353
356
  self._create_msg('global', **payload),
354
- ignore_channel=WS_CHANNEL.get() if ignore_current_channel else None,
357
+ ignore_channel=ignore_channel,
355
358
  )
356
359
 
357
- async def _notify_value(self, value: Any):
360
+ async def _notify_value(self, value: Any, ignore_channel: Optional[str] = None):
358
361
  """
359
362
  Notify all clients about the new value for this store.
360
363
  Broadcasts to all users if scope is global or sends to the current user if scope is user.
361
364
 
362
365
  :param value: value to notify about
366
+ :param ignore_channel: if passed, ignore the specified channel when broadcasting
363
367
  """
364
368
  if self.scope == 'global':
365
- return await self._notify_global(value=value)
369
+ return await self._notify_global(value=value, ignore_channel=ignore_channel)
366
370
 
367
371
  # For user scope, we need to find channels for the user and notify them
368
372
  user = USER.get()
@@ -371,7 +375,7 @@ class BackendStore(PersistenceStore):
371
375
  return
372
376
 
373
377
  user_identifier = user.identity_id or user.identity_name
374
- return await self._notify_user(user_identifier, value=value)
378
+ return await self._notify_user(user_identifier, value=value, ignore_channel=ignore_channel)
375
379
 
376
380
  async def _notify_patches(self, patches: List[Dict[str, Any]]):
377
381
  """
@@ -406,8 +410,8 @@ class BackendStore(PersistenceStore):
406
410
  async def _on_value(key: str, value: Any):
407
411
  # here we explicitly DON'T ignore the current channel, in case we created this variable inside e.g. a py_component we want to notify its creator as well
408
412
  if user := self._get_user(key):
409
- return await self._notify_user(user, ignore_current_channel=False, value=value)
410
- return await self._notify_global(ignore_current_channel=False, value=value)
413
+ return await self._notify_user(user, value=value)
414
+ return await self._notify_global(value=value)
411
415
 
412
416
  await self.backend.subscribe(_on_value)
413
417
 
@@ -466,7 +470,7 @@ class BackendStore(PersistenceStore):
466
470
 
467
471
  return updated_value
468
472
 
469
- async def write(self, value: Any, notify=True):
473
+ async def write(self, value: Any, notify=True, ignore_channel: Optional[str] = None):
470
474
  """
471
475
  Persist a value to the store.
472
476
 
@@ -475,6 +479,7 @@ class BackendStore(PersistenceStore):
475
479
 
476
480
  :param value: value to write
477
481
  :param notify: whether to broadcast the new value to clients
482
+ :param ignore_channel: if passed, ignore the specified websocket channel when broadcasting
478
483
  """
479
484
  if self.readonly:
480
485
  raise ValueError('Cannot write to a read-only store')
@@ -486,7 +491,7 @@ class BackendStore(PersistenceStore):
486
491
  self._get_next_sequence_number(key)
487
492
 
488
493
  if notify:
489
- await self._notify_value(value)
494
+ await self._notify_value(value, ignore_channel=ignore_channel)
490
495
 
491
496
  return res
492
497