ex4nicegui 0.3.0__py3-none-any.whl → 0.3.1__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.
ex4nicegui/__init__.py CHANGED
@@ -16,4 +16,4 @@ from signe import batch
16
16
  from ex4nicegui.experimental_ import gridLayout as exp_ui
17
17
 
18
18
 
19
- __version__ = "0.3.0"
19
+ __version__ = "0.3.1"
ex4nicegui/bi/__init__.py CHANGED
@@ -1,3 +1,13 @@
1
1
  from .index import data_source
2
+ from .elements.text import ui_title, ui_header, ui_subheader
3
+ from .elements.containers import ui_cols
4
+ from .elements.layouts import ui_left_drawer
2
5
 
3
- __all__ = ["data_source"]
6
+ __all__ = [
7
+ "data_source",
8
+ "ui_left_drawer",
9
+ "ui_title",
10
+ "ui_header",
11
+ "ui_subheader",
12
+ "ui_cols",
13
+ ]
@@ -1,16 +1,12 @@
1
- from typing import Dict, List, Optional, cast
1
+ from typing import Callable, Dict, List, Optional, Set, cast
2
2
  from ex4nicegui import to_ref, ref_computed, on
3
- from nicegui import globals, Client
3
+ from nicegui import globals as ng_globals, Client, ui
4
4
 
5
- from dataclasses import dataclass
5
+ from dataclasses import dataclass, field
6
6
  from . import types
7
7
  from .protocols import IDataSourceAble
8
8
 
9
-
10
- @dataclass
11
- class DataSourceInfo:
12
- source: "DataSource"
13
- update_callback: types._TSourceBuildFn
9
+ _TComponentUpdateCallback = Callable[[], None]
14
10
 
15
11
 
16
12
  @dataclass
@@ -27,8 +23,12 @@ class ComponentInfoKey:
27
23
  @dataclass
28
24
  class ComponentInfo:
29
25
  key: ComponentInfoKey
30
- update_callback: types._TComponentUpdateCallback
26
+ update_callback: Optional[_TComponentUpdateCallback] = None
31
27
  filter: Optional[Filter] = None
28
+ exclude_keys: Set[ComponentInfoKey] = field(default_factory=set)
29
+
30
+ def __eq__(self, other):
31
+ return isinstance(other, ComponentInfo) and self.key == other.key
32
32
 
33
33
 
34
34
  class ComponentMap:
@@ -56,9 +56,10 @@ class ComponentMap:
56
56
  if client_id in self._client_map:
57
57
  del self._client_map[client_id]
58
58
 
59
- def has_record(self, client_id: types._TNgClientID, element_id: types._TElementID):
59
+ def has_record(self, key: ComponentInfoKey):
60
60
  return (
61
- client_id in self._client_map and element_id in self._client_map[client_id]
61
+ key.client_id in self._client_map
62
+ and key.element_id in self._client_map[key.client_id]
62
63
  )
63
64
 
64
65
  def set_filter(
@@ -74,6 +75,9 @@ class ComponentMap:
74
75
  info for ele_map in self._client_map.values() for info in ele_map.values()
75
76
  )
76
77
 
78
+ def get_info(self, key: ComponentInfoKey) -> ComponentInfo:
79
+ return self._client_map[key.client_id][key.element_id]
80
+
77
81
 
78
82
  class DataSource:
79
83
  _global_id_count: types._TDataSourceId = 0
@@ -118,12 +122,33 @@ class DataSource:
118
122
  def id(self):
119
123
  return self.__id
120
124
 
125
+ def get_component_info_key(self, element_id: types._TElementID):
126
+ client_id = ng_globals.get_client().id
127
+ return ComponentInfoKey(client_id, element_id)
128
+
129
+ def get_filtered_data(self, element: ui.element):
130
+ data = self._idataSource.get_data()
131
+
132
+ # note:must be use client id of the element
133
+ key = ComponentInfoKey(element.client.id, element.id)
134
+ current_info = self._component_map.get_info(key)
135
+
136
+ filters = [
137
+ info.filter.callback
138
+ for info in self._component_map.get_all_info()
139
+ if current_info.key != info.key
140
+ and (info.key not in current_info.exclude_keys)
141
+ and info.filter
142
+ ]
143
+
144
+ return self._idataSource.apply_filters(data, filters)
145
+
121
146
  def _register_component(
122
147
  self,
123
148
  element_id: types._TElementID,
124
- update_callback: types._TComponentUpdateCallback,
149
+ update_callback: Optional[_TComponentUpdateCallback] = None,
125
150
  ):
126
- ng_client = globals.get_client()
151
+ ng_client = ng_globals.get_client()
127
152
  client_id = ng_client.id
128
153
 
129
154
  if not self._component_map.has_client_record(client_id):
@@ -133,42 +158,46 @@ class DataSource:
133
158
  if not e.shared:
134
159
  self._component_map.remove_client(e.id)
135
160
 
136
- self._component_map.add_info(
137
- ComponentInfo(ComponentInfoKey(client_id, element_id), update_callback)
138
- )
161
+ info = ComponentInfo(ComponentInfoKey(client_id, element_id), update_callback)
162
+ self._component_map.add_info(info)
139
163
 
140
- return self
164
+ return info
141
165
 
142
166
  def send_filter(self, element_id: types._TElementID, filter: Filter):
143
- client_id = globals.get_client().id
167
+ client_id = ng_globals.get_client().id
168
+ key = ComponentInfoKey(client_id, element_id)
144
169
 
145
- if not self._component_map.has_record(client_id, element_id):
170
+ if not self._component_map.has_record(key):
146
171
  raise ValueError("element not register")
147
172
 
148
173
  self._component_map.set_filter(client_id, element_id, filter)
149
174
 
150
- self.__notify_update([ComponentInfoKey(client_id, element_id)])
151
- return self
175
+ trigger_info = self._component_map.get_info(
176
+ ComponentInfoKey(client_id, element_id)
177
+ )
152
178
 
153
- def __notify_update(self, ignore_keys: Optional[List[ComponentInfoKey]] = None):
154
- ignore_keys = ignore_keys or []
155
- ignore_ids_set = set(ignore_keys)
179
+ self.__notify_update(trigger_info)
180
+ self.__filters.value = [
181
+ info.filter for info in self._component_map.get_all_info() if info.filter
182
+ ]
156
183
 
184
+ return self
185
+
186
+ def __notify_update(self, trigger_info: Optional[ComponentInfo] = None):
157
187
  # nodify every component
158
188
  for current_info in self._component_map.get_all_info():
159
- if current_info.key in ignore_ids_set:
189
+ # not nodify the self triggering
190
+ if trigger_info and current_info.key == trigger_info.key:
160
191
  continue
161
192
 
162
- # apply filters ,except current target
163
- filters = [
164
- info.filter.callback
165
- for info in self._component_map.get_all_info()
166
- if (info.key != current_info.key) and info.filter
167
- ]
168
-
169
- new_data = self._idataSource.apply_filters(self.__data.value, filters)
170
- current_info.update_callback(new_data)
193
+ update_callback = current_info.update_callback
194
+ if update_callback:
195
+ update_callback()
171
196
 
172
- self.__filters.value = [
173
- info.filter for info in self._component_map.get_all_info() if info.filter
174
- ]
197
+ def on_source_update(
198
+ self,
199
+ element_id: types._TElementID,
200
+ callback: _TComponentUpdateCallback,
201
+ ):
202
+ key = self.get_component_info_key(element_id)
203
+ self._component_map.get_info(key).update_callback = callback
@@ -1,10 +1,14 @@
1
1
  from __future__ import annotations
2
2
  from typing import Any, Callable, Dict, TypeVar, Generic, Union, cast
3
3
  from nicegui import ui
4
- from ex4nicegui import ref_computed
5
- from ex4nicegui.reactive import rxui
6
4
  from .dataSource import DataSource, Filter
7
- from ex4nicegui.reactive.EChartsComponent.ECharts import echarts
5
+ from . import types as bi_types
6
+ from .elements.ui_select import ui_select
7
+ from .elements.ui_radio import ui_radio
8
+ from .elements.ui_slider import ui_slider
9
+ from .elements.ui_range import ui_range
10
+ from .elements.ui_echarts import ui_echarts
11
+ from .elements.ui_aggrid import ui_aggrid
8
12
 
9
13
 
10
14
  _TData = TypeVar("_TData")
@@ -24,9 +28,7 @@ class DataSourceFacade(Generic[_TData]):
24
28
  """Data after filtering"""
25
29
  return cast(_TData, self._dataSource.filtered_data)
26
30
 
27
- def ui_select(
28
- self, column: str, *, clearable=True, multiple=True, **kwargs
29
- ) -> ui.select:
31
+ def ui_select(self, column: str, *, clearable=True, multiple=True, **kwargs):
30
32
  """
31
33
  Creates a user interface select box.
32
34
 
@@ -37,65 +39,11 @@ class DataSourceFacade(Generic[_TData]):
37
39
  **kwargs: Additional optional parameters that will be passed to the ui.select constructor.
38
40
 
39
41
  Returns:
40
- ui.select: An instance of a user interface select box.
42
+ SelectResult: An instance of a user interface select box.
41
43
  """
42
- options = self._dataSource._idataSource.duplicates_column_values(
43
- self.data, column
44
- )
45
- kwargs.update(
46
- {
47
- "options": options,
48
- "multiple": multiple,
49
- "clearable": clearable,
50
- "label": column,
51
- }
52
- )
53
-
54
- cp = ui.select(**kwargs).props("use-chips outlined")
55
-
56
- def onchange(e):
57
- value = None
58
- if e.args:
59
- if isinstance(e.args, list):
60
- value = [arg["label"] for arg in e.args]
61
- else:
62
- value = e.args["label"]
63
-
64
- cp.value = value
65
-
66
- def data_filter(data):
67
- if cp.value is None or not cp.value:
68
- return data
69
-
70
- cond = None
71
- if isinstance(cp.value, list):
72
- cond = data[column].isin(cp.value)
73
- else:
74
- cond = data[column] == cp.value
75
- return data[cond]
76
-
77
- self._dataSource.send_filter(cp.id, Filter(data_filter))
78
-
79
- cp.on("update:modelValue", onchange)
80
-
81
- def on_source_update(data):
82
- options = self._dataSource._idataSource.duplicates_column_values(
83
- data, column
84
- )
85
- value = cp.value
86
-
87
- # Make the value within the options
88
- if isinstance(value, list):
89
- value = list(set(value) & set(options))
90
- else:
91
- if value not in options:
92
- value = ""
93
-
94
- cp.set_options(options, value=value)
95
-
96
- self._dataSource._register_component(cp.id, on_source_update)
97
-
98
- return cp
44
+ kws = {key: value for key, value in locals().items() if key not in ("kwargs")}
45
+ kws.update(kwargs)
46
+ return ui_select(**kws)
99
47
 
100
48
  def ui_aggrid(self, **kwargs):
101
49
  """
@@ -107,23 +55,7 @@ class DataSourceFacade(Generic[_TData]):
107
55
  Returns:
108
56
  ui.aggrid: aggrid table.
109
57
  """
110
- kwargs.update(
111
- {"options": self._dataSource._idataSource.get_aggrid_options(self.data)}
112
- )
113
-
114
- cp = ui.aggrid(**kwargs)
115
-
116
- def on_source_update(data):
117
- cp._props["options"] = self._dataSource._idataSource.get_aggrid_options(
118
- data
119
- )
120
- cp.update()
121
-
122
- on_source_update(self.filtered_data)
123
-
124
- self._dataSource._register_component(cp.id, on_source_update)
125
-
126
- return cp
58
+ return ui_aggrid(self, **kwargs)
127
59
 
128
60
  def ui_radio(self, column: str, **kwargs):
129
61
  """
@@ -134,41 +66,11 @@ class DataSourceFacade(Generic[_TData]):
134
66
  **kwargs: Additional optional parameters that will be passed to the ui.radio constructor.
135
67
 
136
68
  Returns:
137
- ui.radio: An radio Selection.
69
+ RadioResult: An radio Selection.
138
70
  """
139
- options = self._dataSource._idataSource.duplicates_column_values(
140
- self.data, column
141
- )
142
- kwargs.update({"options": options})
143
-
144
- cp = ui.radio(**kwargs)
145
-
146
- def onchange(e):
147
- cp.value = cp.options[e.args]
148
-
149
- def data_filter(data):
150
- if cp.value not in cp.options:
151
- return data
152
- cond = data[column] == cp.value
153
- return data[cond]
154
-
155
- self._dataSource.send_filter(cp.id, Filter(data_filter))
156
-
157
- cp.on("update:modelValue", onchange)
158
-
159
- def on_source_update(data):
160
- options = self._dataSource._idataSource.duplicates_column_values(
161
- data, column
162
- )
163
- value = cp.value
164
- if value not in options:
165
- value = ""
166
-
167
- cp.set_options(options, value=value)
168
-
169
- self._dataSource._register_component(cp.id, on_source_update)
170
-
171
- return cp
71
+ kws = {key: value for key, value in locals().items() if key not in ("kwargs")}
72
+ kws.update(kwargs)
73
+ return ui_radio(**kws)
172
74
 
173
75
  def ui_slider(self, column: str, **kwargs):
174
76
  """
@@ -181,39 +83,28 @@ class DataSourceFacade(Generic[_TData]):
181
83
  Returns:
182
84
  ui.radio: An Slider.
183
85
  """
184
- self._dataSource._idataSource.slider_check(self.data, column)
185
-
186
- min, max = self._dataSource._idataSource.slider_min_max(self.data, column)
187
- kwargs.update({"min": min, "max": max})
188
-
189
- cp = ui.slider(**kwargs).props("label label-always switch-label-side")
190
-
191
- def onchange():
192
- def data_filter(data):
193
- if cp.value is None or cp.value < min:
194
- return data
195
- cond = data[column] == cp.value
196
- return data[cond]
86
+ kws = {key: value for key, value in locals().items() if key not in ("kwargs")}
87
+ kws.update(kwargs)
88
+ return ui_slider(**kws)
197
89
 
198
- self._dataSource.send_filter(cp.id, Filter(data_filter))
199
-
200
- cp.on("change", onchange)
201
-
202
- def on_source_update(data):
203
- min, max = self._dataSource._idataSource.slider_min_max(data, column)
204
- if min is None or max is None:
205
- cp.value = None
206
- else:
207
- cp._props["min"] = min
208
- cp._props["max"] = max
90
+ def ui_range(self, column: str, **kwargs):
91
+ """
92
+ Creates Range.
209
93
 
210
- self._dataSource._register_component(cp.id, on_source_update)
94
+ Parameters:
95
+ column (str): The column name of the data source.
96
+ **kwargs: Additional optional parameters that will be passed to the ui.slider constructor.
211
97
 
212
- return cp
98
+ Returns:
99
+ QRange: An Range.
100
+ """
101
+ kws = {key: value for key, value in locals().items() if key not in ("kwargs")}
102
+ kws.update(kwargs)
103
+ return ui_range(**kws)
213
104
 
214
105
  def ui_echarts(
215
106
  self, fn: Callable[[Any], Union[Dict, "pyecharts.Base"]] # pyright: ignore
216
- ) -> echarts:
107
+ ):
217
108
  """Create charts
218
109
 
219
110
  Args:
@@ -245,19 +136,13 @@ class DataSourceFacade(Generic[_TData]):
245
136
  ```
246
137
 
247
138
  """
248
-
249
- @ref_computed
250
- def chart_options():
251
- options = fn(self.filtered_data)
252
- if isinstance(options, Dict):
253
- return options
254
-
255
- import simplejson as json
256
- from pyecharts.charts.chart import Base
257
-
258
- if isinstance(options, Base):
259
- return cast(Dict, json.loads(options.dump_options()))
260
-
261
- cp = rxui.echarts(chart_options) # type: ignore
262
-
263
- return cp.element # type: ignore
139
+ return ui_echarts(self, fn)
140
+
141
+ def send_filter(
142
+ self, element: ui.element, filter: bi_types._TFilterCallback[_TData]
143
+ ):
144
+ ele_id = element.id
145
+ key = self._dataSource.get_component_info_key(ele_id)
146
+ if not self._dataSource._component_map.has_record(key):
147
+ self._dataSource._register_component(ele_id)
148
+ self._dataSource.send_filter(ele_id, Filter(filter))
File without changes
@@ -0,0 +1,13 @@
1
+ from nicegui import ui
2
+ from ex4nicegui.layout import grid_box
3
+
4
+
5
+ def ui_cols(num: int, min_width="0"):
6
+ with grid_box(template_columns=f"repeat({num},1fr)").classes(
7
+ "justify-between"
8
+ ) as gb:
9
+ divs = [ui.column() for _ in range(num)]
10
+
11
+ gb.grid_box(template_columns="1fr", break_point="<sm[0-599.99px]")
12
+
13
+ return divs
@@ -0,0 +1,28 @@
1
+ from nicegui import ui
2
+ from ex4nicegui.reactive import rxui
3
+ from ex4nicegui import to_ref
4
+
5
+
6
+ def ui_left_drawer():
7
+ drawer_show = to_ref(True)
8
+
9
+ with rxui.drawer("left", value=drawer_show) as drawer:
10
+ with ui.page_sticky("top-right", x_offset=10, y_offset=10):
11
+ icon_close = (
12
+ rxui.icon(name="close")
13
+ .classes("cursor-pointer")
14
+ .props("round ")
15
+ .bind_visible(drawer_show)
16
+ )
17
+ icon_close.on("click", drawer.toggle)
18
+
19
+ with ui.page_sticky("top-left", x_offset=10, y_offset=10):
20
+ icon_close = (
21
+ rxui.icon(name="keyboard_arrow_right", size="1.2rem")
22
+ .classes("cursor-pointer")
23
+ .props("round ")
24
+ .bind_not_visible(drawer_show)
25
+ )
26
+ icon_close.on("click", drawer.toggle)
27
+
28
+ return drawer.element
@@ -0,0 +1,55 @@
1
+ from __future__ import annotations
2
+ from typing import Optional, TypeVar, Generic, TYPE_CHECKING, Union
3
+ from nicegui import ui
4
+
5
+ if TYPE_CHECKING:
6
+ from ex4nicegui.bi.dataSource import DataSource
7
+
8
+
9
+ _T_ELEMENT = TypeVar("_T_ELEMENT", bound=ui.element)
10
+
11
+
12
+ class UiResult(Generic[_T_ELEMENT]):
13
+ def __init__(self, element: _T_ELEMENT, dataSource: "DataSource") -> None:
14
+ self.__element = element
15
+ self._dataSource = dataSource
16
+
17
+ @property
18
+ def element(self):
19
+ return self.__element
20
+
21
+ @property
22
+ def id(self):
23
+ return self.element.id
24
+
25
+ def classes(
26
+ self,
27
+ add: Optional[str] = None,
28
+ *,
29
+ remove: Optional[str] = None,
30
+ replace: Optional[str] = None,
31
+ ):
32
+ self.element.classes(add, remove=remove, replace=replace)
33
+ return self
34
+
35
+ def props(
36
+ self,
37
+ add: Optional[str] = None,
38
+ *,
39
+ remove: Optional[str] = None,
40
+ ):
41
+ self.element.props(add, remove=remove)
42
+ return self
43
+
44
+ def cancel_linkage(self, *source: Union[ui.element, "UiResult"]):
45
+ get_info_key = self._dataSource.get_component_info_key
46
+
47
+ key = get_info_key(self.element.id)
48
+
49
+ info = self._dataSource._component_map.get_info(key)
50
+
51
+ for s in source:
52
+ res_key = get_info_key(s.id)
53
+ info.exclude_keys.add(res_key)
54
+
55
+ return self
@@ -0,0 +1,33 @@
1
+ from typing import Callable, Any, Union
2
+ from nicegui import ui
3
+ from ex4nicegui import effect
4
+
5
+
6
+ _T_Maybe_Callable = Any
7
+
8
+
9
+ def _ui_label(text_or_build: _T_Maybe_Callable):
10
+ label = ui.label()
11
+
12
+ if isinstance(text_or_build, Callable):
13
+
14
+ @effect
15
+ def _():
16
+ label.text = text_or_build()
17
+
18
+ else:
19
+ label.text = str(text_or_build)
20
+
21
+ return label
22
+
23
+
24
+ def ui_title(text_or_build: _T_Maybe_Callable):
25
+ return _ui_label(text_or_build).style("font-size: calc(1.4rem + 1.8vw)")
26
+
27
+
28
+ def ui_header(text_or_build: _T_Maybe_Callable):
29
+ return _ui_label(text_or_build).style("font-size: calc(1.35rem + 1.2vw)")
30
+
31
+
32
+ def ui_subheader(text_or_build: _T_Maybe_Callable):
33
+ return _ui_label(text_or_build).style("font-size: calc(1.3rem + .6vw)")
@@ -0,0 +1,46 @@
1
+ from __future__ import annotations
2
+ from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union, cast
3
+ from nicegui import ui
4
+ from nicegui.events import UiEventArguments
5
+ from ex4nicegui.reactive import rxui
6
+ from ex4nicegui.reactive.EChartsComponent.ECharts import (
7
+ EChartsClickEventArguments,
8
+ echarts,
9
+ )
10
+ from ex4nicegui.bi.dataSource import DataSource
11
+ from .models import UiResult
12
+
13
+ if TYPE_CHECKING:
14
+ from ex4nicegui.bi.dataSourceFacade import DataSourceFacade
15
+
16
+
17
+ class AggridResult(UiResult[ui.aggrid]):
18
+ def __init__(
19
+ self, element: ui.aggrid, dataSource: DataSource, table_update: Callable
20
+ ) -> None:
21
+ super().__init__(element, dataSource)
22
+ self.table_update = table_update
23
+
24
+ def cancel_linkage(self, *source: Union[ui.element, "UiResult"]):
25
+ super().cancel_linkage(*source)
26
+ self.table_update()
27
+
28
+
29
+ def ui_aggrid(
30
+ self: DataSourceFacade,
31
+ **kwargs,
32
+ ):
33
+ kwargs.update({"options": {}})
34
+
35
+ cp = ui.aggrid(**kwargs)
36
+
37
+ def on_source_update():
38
+ data = self._dataSource.get_filtered_data(cp)
39
+ cp._props["options"] = self._dataSource._idataSource.get_aggrid_options(data)
40
+ cp.update()
41
+
42
+ info = self._dataSource._register_component(cp.id, on_source_update)
43
+
44
+ on_source_update()
45
+
46
+ return AggridResult(cp, self._dataSource, on_source_update)
@@ -0,0 +1,35 @@
1
+ export default {
2
+ template: `
3
+ <div :id="'cus-'+id" class="q-pa-md" style="max-width: 300px">
4
+ <q-input filled v-model="value" mask="date" :rules="['date']">
5
+ <template v-slot:append>
6
+ <q-icon name="event" class="cursor-pointer">
7
+ <q-popup-proxy cover transition-show="scale" transition-hide="scale">
8
+ <q-date v-model="value" >
9
+ </q-date>
10
+ </q-popup-proxy>
11
+ </q-icon>
12
+ </template>
13
+ </q-input>
14
+ </div>
15
+ `,
16
+ props: {
17
+ id: String,
18
+ date: String,
19
+ },
20
+ data() {
21
+ return {
22
+ value: this.date
23
+ }
24
+ },
25
+ watch: {
26
+ value(newValue) {
27
+ this.$emit("update:value", newValue);
28
+ },
29
+ },
30
+ computed: {
31
+ },
32
+ methods: {
33
+
34
+ },
35
+ };