Skip to content

Commit f8b0b99

Browse files
committed
fix force update in example loader
1 parent a0c0ffd commit f8b0b99

File tree

5 files changed

+42
-25
lines changed

5 files changed

+42
-25
lines changed

docs/examples.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,25 @@ def _printout_viewer():
103103

104104
@idom.component
105105
def ShowPrint():
106-
buffer = idom.hooks.use_state(StringIO)[0]
107-
update = _use_force_update()
108-
109-
@idom.hooks.use_effect
110-
def add_buffer():
111-
print_callbacks.add(buffer.write)
112-
update()
113-
114-
value = buffer.getvalue()
115-
return idom.html.pre({"class": "printout"}, value) if value else idom.html.div()
106+
lines, set_lines = idom.hooks.use_state(())
107+
108+
def set_buffer(text: str):
109+
if len(lines) > 10:
110+
# limit printout size - protects against malicious actors
111+
# plus it gives you some nice scrolling printout
112+
set_lines(lines[1:] + (text,))
113+
else:
114+
set_lines(lines + (text,))
115+
116+
@idom.hooks.use_effect(args=[set_buffer])
117+
def add_set_buffer_callback():
118+
print_callbacks.add(set_buffer)
119+
return lambda: print_callbacks.remove(set_buffer)
120+
121+
if not lines:
122+
return idom.html.div()
123+
else:
124+
return idom.html.pre({"class": "printout"}, "".join(lines))
116125

117126
def capture_print(*args, **kwargs):
118127
buffer = StringIO()

docs/source/adding-interactivity/index.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ other :ref:`later <managing state>`).
110110
Section 3: State as a Snapshot
111111
------------------------------
112112

113+
As we :ref:`learned earlier <Components with State>`, state setters behave a little
114+
differently than you might exepct at first glance. Instead of updating your current
115+
handle on the corresponding state variable it schedules a re-render of the component
116+
which owns the state:
117+
118+
.. code-block::
119+
120+
print(count)
121+
set_count(count + 1)
122+
print(count)
123+
113124
.. card::
114125
:link: state-as-a-snapshot
115126
:link-type: doc
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
.. _State as a Snapshot:
1+
State as a Snapshot
2+
===================
23

3-
State as a Snapshot 🚧
4-
======================
5-
6-
.. note::
7-
8-
Under construction 🚧
4+
While you can read state variables like you might normally in Python, as we
5+
:ref:`learned earlier <Components with State>`, assigning to them is somewhat different.
6+
When you use a state setter to update a component, instead of modifying your handle to
7+
its corresponding state variable, a re-render is triggered. Only after that next render
8+
begins will things change.

docs/source/reference-material/javascript-api.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _Javascript API:
22

3-
Javascript API
4-
==============
3+
Javascript API 🚧
4+
=================
55

66
.. note::
77

scripts/one_example.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
from threading import Event, Thread
55

66
import idom
7-
from docs.examples import (
8-
all_example_names,
9-
get_py_example_file_by_name,
10-
load_one_example,
11-
)
7+
from docs.examples import all_example_names, get_example_files_by_name, load_one_example
128
from idom.widgets import hotswap
139

1410

@@ -42,7 +38,8 @@ def update_component():
4238
print(f"Loading example: {ex_name!r}")
4339
mount(load_one_example(ex_name))
4440

45-
on_file_change(get_py_example_file_by_name(ex_name), update_component)
41+
for file in get_example_files_by_name(ex_name):
42+
on_file_change(file, update_component)
4643

4744
idom.run(component)
4845

0 commit comments

Comments
 (0)