-
Notifications
You must be signed in to change notification settings - Fork 6
/
bind_wizardry.jl
318 lines (249 loc) · 7.49 KB
/
bind_wizardry.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
### A Pluto.jl notebook ###
# v0.12.6
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
el
end
end
# ╔═╡ 05b01f6e-106a-11eb-2a88-5f523fafe433
begin
using Pkg
Pkg.activate(mktempdir())
Pkg.add([
Pkg.PackageSpec(name="PlutoUI", version="0.6.7-0.6"),
Pkg.PackageSpec(name="JSON"),
])
using PlutoUI
import JSON
end
# ╔═╡ a4ac414a-1a17-11eb-278b-d7bba3013f42
md"""
# `this` persistence
"""
# ╔═╡ b75a16aa-1a17-11eb-3f6a-05f136313585
md"""
Edit `x`!
"""
# ╔═╡ aac7928c-1a17-11eb-1c5d-bffde1a43f11
x = [80, 200, 500]
# ╔═╡ bc00430a-1a17-11eb-0593-b11c999979c7
"""
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/d3.min.js"></script>
<script id="hello">
const x = $(JSON.json(x))
const svg = this == null ? DOM.svg(600,200) : this
const s = this == null ? d3.select(svg) : this.s
s.selectAll("circle")
.data(x)
.join("circle")
.transition()
.duration(300)
.attr("cx", d => d)
.attr("cy", 100)
.attr("r", 10)
.attr("fill", "gray")
const output = svg
output.s = s
return output
</script>
""" |> HTML
# ╔═╡ bf8a6938-1a17-11eb-32ef-6f04f60ecff0
md"""
(More info will follow, sorry!)
"""
# ╔═╡ 350bb500-1a17-11eb-3ccc-b567385b89eb
md"""
# Self updating cells
"""
# ╔═╡ ea4019c6-1a16-11eb-1eb2-89dda5e2e461
begin
# we need to reference potato in this cell --before assigning to potato-- to
# register as self-updating cell
if !@isdefined(potato)
# potato not defined means that this is the first run
# when the bond sets a value, the new value will be assigned to potato
# __before__ this cell is run.
# so potato defined means that this cell is running as
# response to the bond update
# set a default value
potato = 10
end
s = @bind potato Slider(1:100, default=potato)
# We now assigned to potato to register as self-update
# (`@bind x a` counts as assignment to `x`)
# bound values will only be set if they are assigned somewhere.
md"""
Old value: $potato
New value: $s
"""
end
# ╔═╡ 3c699970-1a17-11eb-28c1-c36551071fb0
md"""
Comments:
You see that you can't _slide_ the slider. This is because a new slider gets rendered every time the cell updates, which resets its internal (cursor) state.
If this is a problem, it can be fixed using the `this` persistence together with self-updating, see the next section.
"""
# ╔═╡ 1cbc1278-1a18-11eb-34db-3704e69a8a9c
md"""
# `this` persistence + self updating
The demo below uses both techniques, but you can see that the animations are not working when you click.
"""
# ╔═╡ e8ea71fc-108e-11eb-2f27-e984fde247d2
A = [0 -1
1 -.7]
# ╔═╡ 10853972-108f-11eb-36b5-57f656bc992e
T = LinRange(0.0, 60.0, 500)
# ╔═╡ 2187253c-108f-11eb-04a2-512ce3c17abf
ΔT = step(T)
# ╔═╡ 0af07152-108f-11eb-2c0b-d96b54bfd3a5
f(t,x) = A*x
# ╔═╡ bcac8f60-1086-11eb-1756-bb2b19f7a25f
function compute_path(first)
accumulate(T; init=first) do x_prev, t
x_prev + ΔT * f(t,x_prev)
end
end
# ╔═╡ cf5f7bda-19f1-11eb-2c79-89c98f1d34da
md"""
The next one is kinda broken, come back later!
"""
# ╔═╡ e926013a-1080-11eb-24b8-034df3032883
with_d3_libs(content) = HTML("""
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/d3.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/d3-scale.min.js"></script>
$(repr(MIME"text/html"(), content))
""")
# ╔═╡ 53dc7e0a-1081-11eb-39a3-c981848c2b1d
begin
struct BondDefault
element
default
end
Base.show(io::IO, m::MIME"text/html", bd::BondDefault) = Base.show(io, m, bd.element)
Base.get(bd::BondDefault) = bd.default
end
# ╔═╡ 04b79b02-1086-11eb-322b-cd995fa4196e
function wow(previous)
path = compute_path(previous)
c = """
<script id="aa">
const path = $(JSON.json(path))
const svg = this == null ? DOM.svg(600,400) : this
const s = this == null ? d3.select(svg) : this.s
svg.value = $(JSON.json(previous))
const xscale = d3.scaleLinear()
.domain([-3,3])
.range([20, 580])
const yscale = d3.scaleLinear()
.domain([-2,2])
.range([380, 20])
if(this == null) {
s.append("g")
.attr("transform", `translate(\${xscale(0)},0)`)
.call(d3.axisLeft(yscale))
s.append("g")
.attr("transform", `translate(0,\${yscale(0)})`)
.call(d3.axisBottom(xscale))
s.append("g").classed("thepath", true)
}
const down_handler =(e) => {
svg.value = [xscale.invert(e.clientX - svg.getBoundingClientRect().left), yscale.invert(e.clientY - svg.getBoundingClientRect().top)]
svg.dispatchEvent(new CustomEvent("input", {}))
}
svg.addEventListener("pointerdown", down_handler)
invalidation.then(() => svg.removeEventListener("pointerdown", down_handler))
s.select("g.thepath").selectAll("path")
.data([path])
.join("path")
.transition()
.duration(300)
.attr("d", d => d3.line()
.x(p => xscale(p[0]))
.y(p => yscale(p[1]))(d))
.attr("stroke", "gray")
.attr('stroke-width', 5)
.attr("fill", "none")
const output = svg
output.s = s
return output
</script>
"""
BondDefault(HTML(c), previous)
end
# ╔═╡ 99a6a906-1086-11eb-3528-fb705460853e
begin
if !@isdefined(x0)
x0 = [0.5, 0.5]
end
@bind x0 wow(x0)
end |> with_d3_libs
# ╔═╡ 337ece1a-1225-11eb-3e24-9b4a816a1ee1
x0
# ╔═╡ ad5b94c6-1071-11eb-25f6-bf800b21b265
let
previous = if @isdefined(pos)
pos
else
100
end
x = previous * (1:10)
c = """
<script id="helloaza">
const x = $(JSON.json(x))
const svg = this == null ? DOM.svg(600,200) : this
const s = this == null ? d3.select(svg) : this.s
svg.value = $(previous)
const down_handler =(e) => {
svg.value = e.clientX - svg.getBoundingClientRect().left
svg.dispatchEvent(new CustomEvent("input", {}))
console.log(svg.value)
}
svg.addEventListener("pointerdown", down_handler)
invalidation.then(() => svg.removeEventListener("pointerdown", down_handler))
s.selectAll("circle")
.data(x)
.join("circle")
.transition()
.duration(300)
.attr("cx", d => d)
.attr("cy", 100)
.attr("r", 10)
.attr("fill", "gray")
const output = svg
output.s = s
return output
</script>
"""
with_d3_libs(@bind pos BondDefault(HTML(c), previous))
end
# ╔═╡ 847b089a-1083-11eb-04d6-e12a4385a8fa
pos
# ╔═╡ Cell order:
# ╠═05b01f6e-106a-11eb-2a88-5f523fafe433
# ╟─a4ac414a-1a17-11eb-278b-d7bba3013f42
# ╠═b75a16aa-1a17-11eb-3f6a-05f136313585
# ╠═aac7928c-1a17-11eb-1c5d-bffde1a43f11
# ╟─bc00430a-1a17-11eb-0593-b11c999979c7
# ╟─bf8a6938-1a17-11eb-32ef-6f04f60ecff0
# ╟─350bb500-1a17-11eb-3ccc-b567385b89eb
# ╠═ea4019c6-1a16-11eb-1eb2-89dda5e2e461
# ╟─3c699970-1a17-11eb-28c1-c36551071fb0
# ╟─1cbc1278-1a18-11eb-34db-3704e69a8a9c
# ╠═e8ea71fc-108e-11eb-2f27-e984fde247d2
# ╠═99a6a906-1086-11eb-3528-fb705460853e
# ╠═337ece1a-1225-11eb-3e24-9b4a816a1ee1
# ╠═04b79b02-1086-11eb-322b-cd995fa4196e
# ╠═10853972-108f-11eb-36b5-57f656bc992e
# ╠═2187253c-108f-11eb-04a2-512ce3c17abf
# ╠═bcac8f60-1086-11eb-1756-bb2b19f7a25f
# ╠═0af07152-108f-11eb-2c0b-d96b54bfd3a5
# ╟─cf5f7bda-19f1-11eb-2c79-89c98f1d34da
# ╠═ad5b94c6-1071-11eb-25f6-bf800b21b265
# ╠═847b089a-1083-11eb-04d6-e12a4385a8fa
# ╠═e926013a-1080-11eb-24b8-034df3032883
# ╠═53dc7e0a-1081-11eb-39a3-c981848c2b1d