ipystream 0.1.0__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Charles Dabadie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.1
2
+ Name: ipystream
3
+ Version: 0.1.0
4
+ Summary: Easy interactive Jupyter dashboards, flowing top to bottom like a stream
5
+ Home-page: https://github.com/jleblanc64/ipystream
6
+ License: MIT
7
+ Keywords: jupyter,dashboard,interactive,stream
8
+ Author: Charles Dabadie
9
+ Author-email: dabadich@gmail.com
10
+ Requires-Python: >=3.10
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Requires-Dist: ipydatagrid (==1.1.16)
19
+ Requires-Dist: pydantic (==2.11.7)
20
+ Requires-Dist: solara (==1.45.0)
21
+ Project-URL: Repository, https://github.com/jleblanc64/ipystream
22
+ Description-Content-Type: text/markdown
23
+
24
+ # ipystream
25
+
26
+ Easy interactive Jupyter dashboards, flowing top to bottom like a stream
@@ -0,0 +1,3 @@
1
+ # ipystream
2
+
3
+ Easy interactive Jupyter dashboards, flowing top to bottom like a stream
File without changes
@@ -0,0 +1,324 @@
1
+ import threading
2
+ from typing import Any, Callable
3
+
4
+ import pandas as pd
5
+ import solara
6
+ from ipydatagrid import DataGrid
7
+ from IPython.core.display import Javascript
8
+ from IPython.core.display_functions import clear_output, display, update_display
9
+ from ipywidgets import HTML, HBox, IntText
10
+ from pydantic import BaseModel
11
+
12
+ display_sep = "---------------------------------------------------------"
13
+ internal_counter_desc = "#{[34_9azerfcd"
14
+ quiet_display_key = "quiet_display"
15
+ logs_key = "logs"
16
+
17
+
18
+ def is_internal_counter(widget):
19
+ if not isinstance(widget, IntText):
20
+ return False
21
+
22
+ return widget.description == internal_counter_desc
23
+
24
+
25
+ def remove_internal_counter(l):
26
+ return [x for x in l if not is_internal_counter(x)]
27
+
28
+
29
+ def proxy_display(widg, display_id, cache):
30
+ if quiet_display_key in cache:
31
+ log(widg, display_id, cache)
32
+ else:
33
+ display(widg, display_id=display_id)
34
+
35
+
36
+ def proxy_update_display(widg, display_id, cache):
37
+ if quiet_display_key in cache:
38
+ log(widg, display_id, cache)
39
+ else:
40
+ update_display(widg, display_id=display_id)
41
+
42
+
43
+ def log(widg, display_id, cache):
44
+ if logs_key not in cache:
45
+ cache[logs_key] = {}
46
+
47
+ cache[logs_key][display_id] = widg
48
+
49
+
50
+ class Handle(BaseModel):
51
+ display_id: str
52
+ cache: dict
53
+
54
+ def update(self, widget):
55
+ proxy_update_display(widget, self.display_id, self.cache)
56
+
57
+
58
+ class WidgetCurrentsChildren(BaseModel):
59
+ parents: list[Any]
60
+ currents: list[Any]
61
+ cache: dict
62
+ currents_level: int
63
+ current_idx: int = 0
64
+ vertical: bool
65
+
66
+ def remove_counter(self):
67
+ clean_parents = self.parents.copy()
68
+ clean_currents = self.currents.copy()
69
+
70
+ if is_internal_counter(clean_parents[-1]):
71
+ clean_parents.pop(-1)
72
+
73
+ if is_internal_counter(clean_currents[-1]):
74
+ clean_currents.pop(-1)
75
+
76
+ return WidgetCurrentsChildren(
77
+ parents=clean_parents,
78
+ currents=clean_currents,
79
+ cache=self.cache,
80
+ currents_level=self.currents_level,
81
+ current_idx=self.current_idx,
82
+ vertical=self.vertical,
83
+ )
84
+
85
+ def display_id(self, index):
86
+ return f"{str(self.currents_level)}_{str(index)}"
87
+
88
+ def display_or_update(self, widget) -> Handle:
89
+ id = self.display_id(self.current_idx)
90
+ h = Handle(idx=self.current_idx, w=self, display_id=id, cache=self.cache)
91
+
92
+ is_update = self.current_idx < len(self.currents)
93
+ if is_update:
94
+ existing = self.currents[self.current_idx]
95
+ # in this case re use existing, as it is certainly observed (eg. SelectMultiple, RadioButtons)
96
+ if hasattr(existing, "options") and hasattr(existing, "value"):
97
+ opts = widget.options
98
+ value = widget.value
99
+
100
+ with existing.hold_trait_notifications():
101
+ existing.options = opts
102
+ existing.value = value
103
+
104
+ self.current_idx = self.current_idx + 1
105
+ return h
106
+ elif self.vertical:
107
+ h.update(widget)
108
+
109
+ else:
110
+ self.currents.append(None)
111
+
112
+ self.currents[self.current_idx] = widget
113
+ self.current_idx = self.current_idx + 1
114
+ return h
115
+
116
+ def sub_title(self, x):
117
+ x = f"<font color='red'>-- {x} --</font>"
118
+ self.display_or_update(HTML(x))
119
+
120
+
121
+ class WidgetUpdater(BaseModel):
122
+ widgets: list[Any]
123
+ updater: Callable[[WidgetCurrentsChildren], None] | None
124
+ vertical: bool
125
+ title: str | None
126
+ split_hbox_after: int | None
127
+
128
+ def stream_down(self, parents, currents, currents_level, level_obj, first_display: bool, last_level: bool):
129
+ # disable all observed
130
+ self.disable_loading(level_obj, first_display, last_level)
131
+
132
+ with level_obj.lock:
133
+ wca = WidgetCurrentsChildren(
134
+ parents=parents, currents=currents, cache=level_obj.cache, currents_level=currents_level, vertical=self.vertical
135
+ )
136
+ wca_cleaned = wca.remove_counter()
137
+ self.updater(wca_cleaned)
138
+
139
+ if first_display:
140
+ missing = len(wca_cleaned.currents) - len(currents) + 1
141
+ for _ in range(missing):
142
+ currents.insert(len(currents) - 1, None)
143
+
144
+ for i, widg in enumerate(wca_cleaned.currents):
145
+ currents[i] = widg
146
+
147
+ if self.title:
148
+ proxy_display(title_html(self.title), None, wca.cache)
149
+
150
+ cache = wca_cleaned.cache
151
+ level_obj.cache = cache
152
+
153
+ # update counter
154
+ if is_internal_counter(currents[-1]):
155
+ c = currents[-1]
156
+ c.value = c.value + 1
157
+
158
+ if self.vertical:
159
+ for i, w in enumerate(wca_cleaned.currents):
160
+ id = wca_cleaned.display_id(i)
161
+ if first_display:
162
+ proxy_display(w, id, cache)
163
+ # else:
164
+ # proxy_update_display(w, id, cache)
165
+ else:
166
+ self.display_horizontal(currents_level, wca_cleaned, first_display)
167
+
168
+ if last_level:
169
+ level_obj.stream_update_done_count = level_obj.stream_update_done_count + 1
170
+
171
+ def display_horizontal(self, currents_level, wca_cleaned, first_display):
172
+ cache = wca_cleaned.cache
173
+ id = str(currents_level)
174
+ if self.split_hbox_after and len(wca_cleaned.currents) > self.split_hbox_after:
175
+ box1 = HBox(wca_cleaned.currents[0 : self.split_hbox_after]) # noqa: E203
176
+ box2 = HBox(wca_cleaned.currents[self.split_hbox_after :]) # noqa: E203
177
+ id1 = f"{id}_1"
178
+ id2 = f"{id}_2"
179
+
180
+ if first_display:
181
+ proxy_display(box1, id1, cache)
182
+ proxy_display(box2, id2, cache)
183
+ else:
184
+ proxy_update_display(box1, id1, cache)
185
+ proxy_update_display(box2, id2, cache)
186
+
187
+ else:
188
+ box = HBox(wca_cleaned.currents)
189
+ if first_display:
190
+ proxy_display(box, id, cache)
191
+ else:
192
+ proxy_update_display(box, id, cache)
193
+
194
+ def stream_down_obs(self, parents, currents, debouncer, currents_level, level_obj, last_level):
195
+ for widget in parents:
196
+ widget.observe(
197
+ lambda x: debouncer(self.stream_down, parents, currents, currents_level, level_obj, False, last_level), names="value"
198
+ )
199
+
200
+ def disable_loading(self, level_obj, first_display: bool, last_level: bool):
201
+ if not first_display:
202
+ level_to_widget = level_obj.level_to_widget
203
+ levels = list(level_to_widget.keys())
204
+ levels.sort()
205
+ levels.pop()
206
+
207
+ for lvl in levels:
208
+ widgets = level_to_widget[lvl].widgets
209
+
210
+ for w in widgets:
211
+ if hasattr(w, "disabled"):
212
+ if not last_level:
213
+ w.disabled = True
214
+ else:
215
+ w.disabled = False
216
+
217
+
218
+ def title_html(x):
219
+ x = f"<font size='4' style='font-weight:bold;line-height: 50px'>{x}</font>"
220
+ return HTML(x)
221
+
222
+
223
+ class Stream(BaseModel):
224
+ debounce_sec: float = 1.0
225
+ level_to_widget: dict[int, WidgetUpdater] = {}
226
+ cache: dict = {}
227
+ lock: Any = None
228
+ stream_update_done_count: int = -1
229
+
230
+ def register(self, level, widgets=None, updater=None, vertical=False, title=None, split_hbox_after=None):
231
+ if not self.level_to_widget:
232
+ self.lock = threading.RLock()
233
+ # check_javascript()
234
+
235
+ if not widgets:
236
+ widgets = []
237
+
238
+ self.level_to_widget[level] = WidgetUpdater(
239
+ widgets=[f(self) for f in widgets], updater=updater, vertical=vertical, title=title, split_hbox_after=split_hbox_after
240
+ )
241
+
242
+ def display_registered(self):
243
+ css = "<style>.widget-radio-box {margin-right: 40px;}</style>"
244
+ display(HTML(css))
245
+
246
+ levels = list(self.level_to_widget.keys())
247
+ levels.sort()
248
+ for level_i, level in enumerate(levels):
249
+
250
+ wu = self.level_to_widget[level]
251
+ currents = wu.widgets
252
+
253
+ # otherwise display happens in wu.stream_down()
254
+ if level_i == 0:
255
+ if wu.title:
256
+ proxy_display(title_html(wu.title), None, self.cache)
257
+ proxy_display(HBox(remove_internal_counter(currents)), None, self.cache)
258
+ print(display_sep)
259
+
260
+ level_below = level + 1
261
+ if level_below not in self.level_to_widget:
262
+ continue
263
+
264
+ self.level_to_widget[level].updater = self.level_to_widget[level_below].updater
265
+ self.level_to_widget[level].vertical = self.level_to_widget[level_below].vertical
266
+ self.level_to_widget[level].title = self.level_to_widget[level_below].title
267
+ self.level_to_widget[level].split_hbox_after = self.level_to_widget[level_below].split_hbox_after
268
+
269
+ children = self.level_to_widget[level_below].widgets
270
+ int_txt = IntText(value=0, disabled=True, description=internal_counter_desc)
271
+ children.append(int_txt)
272
+
273
+ @debounce(self.debounce_sec)
274
+ def debouncer(f, *args):
275
+ f(*args)
276
+
277
+ # init
278
+ last_level = level_i == len(levels) - 2
279
+ wu.stream_down(currents, children, level_below, self, True, last_level)
280
+
281
+ # update on change
282
+ wu.stream_down_obs(currents, children, debouncer, level_below, self, last_level)
283
+
284
+
285
+ def debounce(wait: float):
286
+ def decorator(fn):
287
+ timer = None
288
+
289
+ def debounced(*args, **kwargs):
290
+ nonlocal timer
291
+
292
+ def call_it():
293
+ fn(*args, **kwargs)
294
+
295
+ if timer is not None:
296
+ timer.cancel()
297
+
298
+ timer = threading.Timer(wait, call_it)
299
+ timer.start()
300
+
301
+ return debounced
302
+
303
+ return decorator
304
+
305
+
306
+ def check_javascript():
307
+ grid = DataGrid(pd.DataFrame({"0": [0]}), layout={"height": "10px"})
308
+ display(grid)
309
+
310
+ dl = solara.FileDownload("a", filename="a", label="a")
311
+ display(dl)
312
+
313
+ js = """
314
+ var err = 'Click to show javascript ' + 'error';
315
+ var isJsError = document.body.innerHTML.includes(err);
316
+
317
+ if (isJsError){
318
+ alert('Browser will be refreshed to fully load Jupyter widgets');
319
+ window.location.reload();
320
+ }
321
+ """
322
+
323
+ display(Javascript(js))
324
+ clear_output()
@@ -0,0 +1,30 @@
1
+ [tool.poetry]
2
+ name = "ipystream"
3
+ version = "0.1.0"
4
+ description = "Easy interactive Jupyter dashboards, flowing top to bottom like a stream"
5
+ authors = ["Charles Dabadie <dabadich@gmail.com>"]
6
+ license = "MIT"
7
+ readme = "README.md"
8
+ homepage = "https://github.com/jleblanc64/ipystream"
9
+ repository = "https://github.com/jleblanc64/ipystream"
10
+ keywords = ["jupyter", "dashboard", "interactive", "stream"]
11
+ classifiers = [
12
+ "Programming Language :: Python :: 3",
13
+ "License :: OSI Approved :: MIT License",
14
+ "Operating System :: OS Independent",
15
+ ]
16
+
17
+ [tool.poetry.dependencies]
18
+ python = ">=3.10"
19
+ ipydatagrid = "1.1.16"
20
+ solara = "1.45.0"
21
+ pydantic = "2.11.7"
22
+
23
+ [tool.poetry.dev-dependencies]
24
+ pytest = "7.0"
25
+ black = "23.1.0"
26
+ flake8 = "6.0.0"
27
+
28
+ [build-system]
29
+ requires = ["poetry-core>=1.0.0"]
30
+ build-backend = "poetry.core.masonry.api"