File tree Expand file tree Collapse file tree 5 files changed +42
-25
lines changed Expand file tree Collapse file tree 5 files changed +42
-25
lines changed Original file line number Diff line number Diff line change @@ -103,16 +103,25 @@ def _printout_viewer():
103
103
104
104
@idom .component
105
105
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 ))
116
125
117
126
def capture_print (* args , ** kwargs ):
118
127
buffer = StringIO ()
Original file line number Diff line number Diff line change @@ -110,6 +110,17 @@ other :ref:`later <managing state>`).
110
110
Section 3: State as a Snapshot
111
111
------------------------------
112
112
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
+
113
124
.. card ::
114
125
:link: state-as-a-snapshot
115
126
:link-type: doc
Original file line number Diff line number Diff line change 1
- .. _State as a Snapshot :
1
+ State as a Snapshot
2
+ ===================
2
3
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.
Original file line number Diff line number Diff line change 1
1
.. _Javascript API :
2
2
3
- Javascript API
4
- ==============
3
+ Javascript API 🚧
4
+ =================
5
5
6
6
.. note ::
7
7
Original file line number Diff line number Diff line change 4
4
from threading import Event , Thread
5
5
6
6
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
12
8
from idom .widgets import hotswap
13
9
14
10
@@ -42,7 +38,8 @@ def update_component():
42
38
print (f"Loading example: { ex_name !r} " )
43
39
mount (load_one_example (ex_name ))
44
40
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 )
46
43
47
44
idom .run (component )
48
45
You can’t perform that action at this time.
0 commit comments