/
smart_cell.ex
394 lines (302 loc) · 11.1 KB
/
smart_cell.ex
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
defmodule Kino.SmartCell do
@moduledoc ~S'''
An interface for defining custom smart cells.
A smart cell is a UI wizard designed for producing a piece of code
that accomplishes a specific task. In other words, a smart cell is
like a code template parameterized through UI interactions.
This module builds on top of `Kino.JS.Live`, consequently keeping
all of its component and communication mechanics. The additional
callbacks specify how the UI maps to source code.
## Usage
Defining a custom cell is similar to writing a regular `Kino.JS.Live`
component, with a couple specifics.
First, we only need to define callbacks, so there is no need for
using `Kino.JS.Live.new/2`. The `c:Kino.JS.Live.init/2` callback
always receives `t:attrs/0` as the first argument.
Second, we add a few new bits, namely `use Kino.SmartCell` and the
two corresponding callback definitions.
Here is an outline of a custom module
defmodule KinoDocs.CustomCell do
use Kino.JS
use Kino.JS.Live
use Kino.SmartCell, name: "Our custom wizard"
@impl true
def init(attrs, ctx) do
...
end
# Other Kino.JS.Live callbacks
...
@impl true
def to_attrs(ctx) do
...
end
@impl true
def to_source(attrs) do
...
end
end
Additionally, in order for Livebook to pick up the custom cell, we
need to register our module. This usually happens in `application.ex`
Kino.SmartCell.register(KinoDocs.CustomCell)
## Example
As a minimal example, that's how we can define a cell that allows
editing the underlying code directly through a textarea.
defmodule KinoDocs.SmartCell.Plain do
use Kino.JS
use Kino.JS.Live
use Kino.SmartCell, name: "Plain code editor"
@impl true
def init(attrs, ctx) do
source = attrs["source"] || ""
{:ok, assign(ctx, source: source)}
end
@impl true
def handle_connect(ctx) do
{:ok, %{source: ctx.assigns.source}, ctx}
end
@impl true
def handle_event("update", %{"source" => source}, ctx) do
broadcast_event(ctx, "update", %{"source" => source})
{:noreply, assign(ctx, source: source)}
end
@impl true
def to_attrs(ctx) do
%{"source" => ctx.assigns.source}
end
@impl true
def to_source(attrs) do
attrs["source"]
end
asset "main.js" do
"""
export function init(ctx, payload) {
ctx.importCSS("main.css");
ctx.root.innerHTML = `
<textarea id="source"></textarea>
`;
const textarea = ctx.root.querySelector("#source");
textarea.value = payload.source;
textarea.addEventListener("change", (event) => {
ctx.pushEvent("update", { source: event.target.value });
});
ctx.handleEvent("update", ({ source }) => {
textarea.value = source;
});
ctx.handleSync(() => {
// Synchronously invokes change listeners
document.activeElement &&
document.activeElement.dispatchEvent(new Event("change"));
});
}
"""
end
asset "main.css" do
"""
#source {
box-sizing: border-box;
width: 100%;
min-height: 100px;
}
"""
end
end
And then we would register it as
Kino.SmartCell.register(KinoDocs.SmartCell.Plain)
Note that we register a synchronization handler on the client with
`ctx.handleSync(() => ...)`. This optional handler is invoked before
evaluation and it should flush any deferred UI changes to the server.
In our example we listen to textarea's "change" event, which is only
triggered on blur, so on synchronization we trigger it programmatically.
## Collaborative editor
If a smart cell requires editing some code (like SQL), it may use
a dedicated editor instance managed by Livebook. The editor handles
syntax highlighting and collaborative editing, similarly to the
built-in cells.
To enable the editor, we need to include `:editor` configuration in
options returned from the `c:Kino.JS.Live.init/2` callback.
@impl true
def init(attrs, ctx) do
# ...
{:ok, ctx, editor: [attribute: "code", language: "elixir"]}
end
### Options
* `:attribute` - the key to put the source text under in `attrs`.
Required
* `:language` - the editor language, used for syntax highlighting.
Defaults to `nil`
* `:placement` - editor placement within the smart cell, either
`:top` or `:bottom`. Defaults to `:bottom`
* `:default_source` - the initial editor source. Defaults to `""`
## Other options
Other than the editor configuration, the following options are
supported:
* `:reevaluate_on_change` - if the cell should be reevaluated
whenever the generated source code changes. This option may be
helpful in cases where the cell output is a crucial element of
the UI interactions. Defaults to `false`
'''
require Logger
import Kino.Utils, only: [has_function?: 3]
alias Kino.JS.Live.Context
@typedoc """
Attributes are an intermediate form of smart cell state, used to
persist and restore cells.
Attributes are computed using `c:to_attrs/1` and used to generate
the source code using `c:to_source/1`.
Note that attributes are serialized and deserialized as JSON for
persistence, hence make sure to use JSON-friendly data structures.
Persisted attributes are passed to `c:Kino.JS.Live.init/2` as the
first argument and should be used to restore the relevant state.
"""
@type attrs :: map()
@doc """
Invoked to compute the smart cell state as serializable attributes.
"""
@callback to_attrs(ctx :: Context.t()) :: attrs()
@doc """
Invoked to generate source code based on the given attributes.
A list of sources can be returned, in which case the sources are
joined into a single source, however converting to a Code cell
results in multiple cells.
"""
@callback to_source(attrs()) :: String.t() | list(String.t())
@doc """
Invoked whenever the base evaluation context changes.
This callback receives the binding and environment available to the
smart cell code.
Note that this callback runs asynchronously and it receives the PID
of the smart cell server, so the result needs to be sent explicitly
and handled using `c:Kino.JS.Live.handle_info/2`.
**Important:** remember that data sent between processes is copied,
so avoid sending large data structures. In particular, when looking
at variables, instead of sending their values, extract and send
only the relevant metadata.
**Important:** avoid any heavy work in this callback, as it runs in
the same process that evaluates code, so we don't want to block it.
"""
@callback scan_binding(server :: pid(), Code.binding(), Macro.Env.t()) :: any()
@doc """
Invoked when the smart cell code is evaluated.
This callback receives the result of an evaluation, either the
return value or an exception if raised.
This callback runs asynchronously and has the same characteristics
as `c:scan_binding/3`.
"""
@callback scan_eval_result(server :: pid(), eval_result()) :: any()
@type eval_result ::
{:ok, result :: any()}
| {:error, Exception.kind(), error :: any(), Exception.stacktrace()}
@optional_callbacks scan_binding: 3, scan_eval_result: 2
defmacro __using__(opts) do
quote location: :keep, bind_quoted: [opts: opts] do
@behaviour Kino.SmartCell
@smart_opts opts
@before_compile Kino.SmartCell
end
end
defmacro __before_compile__(env) do
opts = Module.get_attribute(env.module, :smart_opts)
name = Keyword.fetch!(opts, :name)
quote do
def child_spec(%{ref: ref, attrs: attrs, target_pid: target_pid}) do
%{
id: __MODULE__,
start: {Kino.SmartCell.Server, :start_link, [__MODULE__, ref, attrs, target_pid]},
restart: :temporary
}
end
def __smart_definition__() do
%{
kind: Atom.to_string(__MODULE__),
module: __MODULE__,
name: unquote(name)
}
end
end
end
@doc """
Returns a list of available smart cell definitions.
"""
def definitions() do
for module <- get_modules(), do: module.__smart_definition__()
end
@doc """
Registers a new smart cell.
This should usually be called in `application.ex` when starting
the application.
## Examples
Kino.SmartCell.register(KinoDocs.CustomCell)
"""
@spec register(module()) :: :ok
def register(module) do
unless has_function?(module, :__smart_definition__, 0) do
raise ArgumentError, "module #{inspect(module)} does not define a smart cell"
end
modules = get_modules()
updated_modules = if module in modules, do: modules, else: modules ++ [module]
put_modules(updated_modules)
end
@registry_key :smart_cell_modules
defp get_modules() do
Application.get_env(:kino, @registry_key, [])
end
defp put_modules(modules) do
Application.put_env(:kino, @registry_key, modules)
end
@doc """
Generates unique variable names with the given prefix.
When `var_name` is `nil`, allocates and returns the next available
name. Otherwise, marks the given suffix as taken, provided that
`var_name` has the given prefix.
This function can be used to generate default variable names during
smart cell initialization, so that don't overlap.
"""
@spec prefixed_var_name(String.t(), String.t() | nil) :: String.t()
def prefixed_var_name(prefix, var_name)
def prefixed_var_name(prefix, nil) do
case Kino.AttributeStore.counter_next(var_counter_key(prefix)) do
1 -> prefix
n -> "#{prefix}#{n}"
end
end
def prefixed_var_name(prefix, var_name) do
with {:ok, suffix} <- parse_var_prefix(var_name, prefix),
{:ok, n} <- parse_var_suffix(suffix) do
Kino.AttributeStore.counter_put_max(var_counter_key(prefix), n)
end
var_name
end
defp parse_var_prefix(string, prefix) do
if String.starts_with?(string, prefix) do
{:ok, String.replace_prefix(string, prefix, "")}
else
:error
end
end
defp parse_var_suffix(""), do: {:ok, 1}
defp parse_var_suffix(other) do
case Integer.parse(other) do
{n, ""} when n > 1 -> {:ok, n}
_ -> :error
end
end
defp var_counter_key(prefix), do: {:smart_cell_variable, prefix}
@doc """
Checks if the given string is a valid Elixir variable name.
"""
@spec valid_variable_name?(String.t()) :: boolean()
def valid_variable_name?(string) when is_binary(string) do
atom = String.to_atom(string)
Macro.classify_atom(atom) == :identifier
end
@doc """
Converts the given AST to formatted code string.
"""
@spec quoted_to_string(Macro.t()) :: String.t()
def quoted_to_string(quoted) do
quoted
|> Code.quoted_to_algebra()
|> Inspect.Algebra.format(90)
|> IO.iodata_to_binary()
end
end