-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.html
More file actions
1325 lines (1044 loc) · 48.9 KB
/
index.html
File metadata and controls
1325 lines (1044 loc) · 48.9 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@celine/celine</title>
<script type="module" src="./global.mjs"></script>
<script type="module">
import { renderChangelogTable } from "./changelog.mjs";
const celineChangelogUrl = new URL("./celine/changelog.xml", import.meta.url).href;
window.addEventListener("DOMContentLoaded", () => {
renderChangelogTable({
feedUrl: celineChangelogUrl,
tableBodyId: "changelog-table-body",
limit: 10
});
});
</script>
<link rel="stylesheet" href="celine/cell.css" />
<link rel="stylesheet" href="libertine/libertine.css" />
<meta name="theme-color" content="#ffffff">
<link rel="apple-touch-icon" sizes="180x180" href="static/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="static/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="static/favicon-16x16.png">
<link rel="manifest" href="static/site.webmanifest">
<meta name="description" content="A library for building reactive HTML notebooks with inline contenteditable <script>s">
<meta name="author" content="Max Bo">
<meta name="keywords" content="celine, celine, reactive, html, notebook, observable, runtime, api, library, esm, module, script, style, element, contenteditable, reevaluate, blur, cell, viewof, mutable, counter, fizzbuzz, name, greeting, ref, increment, sword">
<meta name="date" content="2024-09-18">
<meta name="license" content="MIT">
<meta property="og:title" content="@celine/celine">
<meta property="og:description" content="A library for building reactive HTML notebooks with inline contenteditable <script>s">
<meta property="og:url" content="https://maxbo.me/celine/">
<meta property="og:type" content="website">
<meta property="og:site_name" content="Max Bo">
<meta property="og:image" content="https://maxbo.me/celine/static/og.png">
<meta name="twitter:card" content="summary">
<meta name="twitter:creator" content="@_max_bo_">
<meta name="twitter:title" content="@celine/celine">
<meta name="twitter:description" content="A library for building reactive HTML notebooks with inline contenteditable <script>s">
<meta name="twitter:image" content="https://maxbo.me/celine/static/og.png">
<meta name="twitter:card" content="summary_large_image">
<script defer>
document.addEventListener("DOMContentLoaded", () => {
const toc = document.getElementById("toccontainer");
const headers = document.querySelectorAll("h2, h3, h4, h5, h6");
// Create an object to store the current ol element at each level
const levels = {};
let prevLevel = 1; // Start with h2 (level 1)
for (const header of headers) {
const level = parseInt(header.tagName.substring(1)) - 2; // Convert h2-h6 to 0-4
// If going deeper, create new ol
if (level > prevLevel) {
const parentLi = levels[prevLevel].lastElementChild;
const newOl = document.createElement("ol");
parentLi.appendChild(newOl);
levels[level] = newOl;
}
// If going back up, remove references to deeper levels
if (level < prevLevel) {
for (let i = prevLevel; i > level; i--) {
delete levels[i];
}
}
// Create list item with link
const li = document.createElement("li");
const a = document.createElement("a");
const headerLink = header.querySelector('a');
if (!headerLink) {
continue;
}
a.href = `#${header.querySelector('a').id}`;
a.innerHTML = header.innerHTML;
// Remove the anchor links
a.querySelectorAll('a[href^="#"]').forEach(link => link.remove());
li.appendChild(a);
// Add to the appropriate ol
if (!levels[level]) {
const newOl = document.createElement("ol");
toc.appendChild(newOl);
levels[level] = newOl;
}
levels[level].appendChild(li);
prevLevel = level;
}
});
</script>
</head>
<body>
<main>
<div style="text-align: right; font-family: 'Libertinus Sans', sans-serif;">
<a href="libertine/index.html">/libertine</a>,
<a href="bibhtml/index.html">/bibhtml</a>
</div>
<h1 class="title">@celine/celine</h1>
<style>
.postit {
float: right;
width: 100px;
z-index: 10;
filter: contrast(150%) brightness(120%);
}
</style>
<img src="static/snail.webp" alt="chiocciolina (Italian: small snail)" class="postit">
<div class="authors">
<div class="author">
<span class="author-name"><a href="https://maxbo.me">Max Bo</a></span>
</div>
</div>
<section class="abstract">
<p>
<cite>@celine/celine</cite> is a library for building reactive HTML notebooks with <code>display: block</code> <code>contenteditable</code> <code><script></code> elements.
It wraps a subset of the <a href="https://observablehq.com/documentation/notebooks/"><cite>Observable Notebook</cite></a> <a href="https://github.com/observablehq/runtime"><cite>Observable Runtime</cite></a> to power inter-cell reactivity, just like <a href="https://quarto.org/docs/interactive/ojs/"><cite>Quarto</cite></a>. It aims to make it easier to publish research as HTML files rather than as PDF files.
</p>
<p>
I initially considered calling this library <i>incel</i>, short for <q>inline cell</q>, but was advised against it.
</p>
<style>
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-2px);
}
60% {
transform: translateY(-1px);
}
}
.bounce {
animation: bounce 2s infinite;
}
</style>
<p class="bounce"><a href="#demo"><b>Jump straight to the demo?</a></b></p>
</section>
<table class="header">
<tbody>
<tr>
<th>GitHub</th>
<td class="width-auto"><a href="https://github.com/MaxwellBo/celine">MaxwellBo/<wbr>celine</a><span id="stars"></span> + <a href="https://github.com/MaxwellBo/celine/issues">/issues</a> </td>
<th class="width-min">License</th>
<td><a href="https://github.com/MaxwellBo/celine/blob/master/LICENSE">MIT</a></td>
</tr>
<script type="module">
// ping the GitHub API for the number of stars
fetch("https://api.github.com/repos/MaxwellBo/celine")
.then(d => d.json())
.then(resp => {
const stars = resp.stargazers_count;
document.getElementById("stars").textContent = ` (⭐ ${stars})`;
})
</script>
<tr>
<th>JSR</th>
<td class="width-auto"><a href="https://jsr.io/@celine/celine">jsr.io/<wbr>@celine/<wbr>celine</a> + <a href="https://jsr.io/@celine/celine/doc">/docs</a></td>
<th>Version</th>
<!-- these should be updated to the best of your ability -->
<td class="width-min"><a href="https://jsr.io/@celine/celine/versions" id="version">9.1.0</a>, <time id="updated">Apr 29 2026</time></td>
</tr>
<script type="module">
fetch("https://npm.jsr.io/@jsr/celine__celine")
.then(d => d.json())
.then(resp => {
const latestVersion = resp["dist-tags"].latest;
document.getElementById("version").textContent = latestVersion;
const date = new Date(resp.time[latestVersion]);
const formattedDate = `${date.toLocaleString('default', { month: 'short' })} ${date.getDate()} ${date.getFullYear()}`;
document.getElementById("updated").textContent = formattedDate;
})
</script>
</tbody>
</table>
<br />
<h2 id="toc"><a href="#toc" id="toc"></a>Table of Contents</h2>
<style>
.table-of-contents > * {
animation: fadeIn 0.3s ease-in;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
</style>
<div class="table-of-contents" id="toccontainer">
</div>
<h2><a href="#usage" id="usage"></a>Usage</h2>
<h3><a href="#via-cdn" id="via-cdn"></a>via CDN, with explicit imports</h3>
<p>
Either use the <a href="cdn-starter-explicit-imports.html">cdn-starter-explicit-imports.html</a> template file
</p>
<span style="font-variant: all-small-caps;">OR</span>
<p>Add the following <code><script></code> element to your HTML file's <code><head></code> block:</p>
<pre class="echo" style="margin-bottom: 1ch;"><script type="module">
import { CelineModule, registerScriptReevaluationOnBlur } from 'https://esm.sh/jsr/@celine/celine@9.2.0';
import * as Inputs from 'https://esm.run/@observablehq/inputs@0.12.0';
import * as htl from 'https://esm.run/htl@0.3.1';
window.celine = CelineModule.usingNewObservableRuntimeAndModule(document);
window.library = celine.library; /* @observablehq/stdlib */
window.Inputs = Inputs;
window.htl = htl;
registerScriptReevaluationOnBlur(document, /*class=*/'echo');
</script>
</pre>
<p>and link <a href="#.echo">cell.css</a> in your <code><head></code> block:</p>
<pre class="echo" style="margin-bottom: 1ch;">
<link
rel="stylesheet"
href="https://esm.sh/jsr/@celine/celine@9.2.0/cell.css" />
</pre>
<p>
You may want to include <cite>@celine/celine</cite>'s drop-in stylesheet, <a href="libertine/index.html">libertine.css</a>:
</p>
<pre class="echo" style="margin-bottom: 1ch;">
<link
rel="stylesheet"
href="https://esm.sh/jsr/@celine/libertine@26.0.0/libertine.css" />
</pre>
<h3><a href="#via-cdn-implicit-imports" id="via-cdn-implicit-imports"></a>via CDN, with implicit imports</h3>
<p>
Either use the <a href="cdn-starter-implicit-imports.html">cdn-starter-implicit-imports.html</a> template file
</p>
<span style="font-variant: all-small-caps;">OR</span>
<p>
Add the following <code><script></code> element to your HTML file's <code><head></code> block:
</p>
<pre class="echo" style="margin-bottom: 1ch;">
<script type="module" src="https://esm.sh/jsr/@celine/celine@9.2.0/global"></script>
</pre>
<p>
This implicit entrypoint initializes and exposes <a href="https://jsr.io/@celine/celine/doc"><cite>@celine/celine</cite></a> (as <code>window.celine</code>), <a href="https://observablehq.com/documentation/stdlib/library"><cite>@observablehq/stdlib</cite></a> (as <code>window.library</code>), <a href="https://github.com/observablehq/inputs"><cite>@observablehq/inputs</cite></a> (as <code>window.Inputs</code>), and <a href="https://github.com/observablehq/htl"><cite>htl</cite></a> (as <code>window.htl</code>).
</p>
<p>Link <a href="#.echo">cell.css</a> in your <code><head></code> block:</p>
<pre class="echo" style="margin-bottom: 1ch;">
<link
rel="stylesheet"
href="https://esm.sh/jsr/@celine/celine@9.2.0/cell.css" />
</pre>
<p>
You may want to include <cite>@celine/celine</cite>'s drop-in stylesheet, <a href="libertine/index.html">libertine.css</a>:
</p>
<pre class="echo" style="margin-bottom: 1ch;">
<link
rel="stylesheet"
href="https://esm.sh/jsr/@celine/libertine@26.0.0/libertine.css" />
</pre>
<h3><a href="#via-local-file" id="via-local-file"></a>via local file</h3>
<p>
Either use the <a href="local-file-starter.zip" download="local-file-starter.zip">local-file-starter.zip</a> template bundle
</p>
<span style="font-variant: all-small-caps;">OR</span>
<p>
Download <a href="celine/celine.bundle.mjs" download="celine.bundle.mjs">celine.bundle.mjs</a> and place it next to your HTML file, then add:
</p>
<pre class="echo" style="margin-bottom: 1ch;">
<script type="module" src="./celine.bundle.mjs"></script>
</pre>
<p>
The local bundle includes and exposes the same libraries: <a href="https://jsr.io/@celine/celine/doc"><cite>@celine/celine</cite></a> (as <code>window.celine</code>), <a href="https://observablehq.com/documentation/stdlib/library"><cite>@observablehq/stdlib</cite></a> (as <code>window.library</code>), <a href="https://github.com/observablehq/inputs"><cite>@observablehq/inputs</cite></a> (as <code>window.Inputs</code>), and <a href="https://github.com/observablehq/htl"><cite>htl</cite></a> (as <code>window.htl</code>).
</p>
<p>
Download <a href="celine/cell.css" download="cell.css">cell.css</a> and link it locally:
</p>
<pre class="echo" style="margin-bottom: 1ch;">
<link rel="stylesheet" href="./cell.css" />
</pre>
<p>
You may want to include <cite>@celine/celine</cite>'s drop-in stylesheet, <a href="libertine/libertine.css" download="libertine.css">libertine.css</a>, locally:
</p>
<pre class="echo" style="margin-bottom: 1ch;">
<link rel="stylesheet" href="./libertine.css" />
</pre>
<span style="font-variant: all-small-caps;">THEN I RECOMMEND</span>
<p>
using <a href="https://bun.com/docs/bundler/standalone-html"><cite>Bun</cite>'s Standalone HTML mode</a>, to package your local notebook and assets into one file,
with <code>bun build --compile --target=browser ./index.html --outdir=dist</code>
</p>
<!-- <h3><a href="#with-bun" id="with-bun"></a>with Bun and Standalone HTML mode</h3>
<p>
<a href="https://bun.com"><cite>Bun</cite></a> supports a <a href="https://bun.com/docs/bundler/standalone-html">
Standalone HTML mode</a> that bundles a HTML file any assets it depends on into a single HTML file,
either by inlining scripts and CSS or by using <code>data:type/extension;base64</code> encodings.
</p>
<p>
Bun can't inline scripts loaded from a CDN in so we need to use an additional
entry file.
</p>
<p>
Either use the <a href="bun-starter.html">bun-starter.html</a> and
<a href="global.mjs">global.mjs</a> template files
</p>
<span style="font-variant: all-small-caps;">OR</span>
<p>
Create a <code>global.mjs</code> that contains:
<pre><code>import { CelineModule, registerScriptReevaluationOnBlur } from './celine/mod.ts';
import * as Inputs from '@observablehq/inputs';
import * as htl from 'htl';
window.celine = CelineModule.usingNewObservableRuntimeAndModule(document);
window.library = celine.library; /* @observablehq/stdlib */
window.Inputs = Inputs;
window.htl = htl;
registerScriptReevaluationOnBlur(document, /*class=*/'echo');</code></pre>
<p>
and then add:
</p>
<pre><code><script type="module" src="./global.mjs"></script></code></pre>
<p>
to your HTML file's <code><head></code> block.
</p>
</p> -->
<h2><a href="#demo" id="demo"></a>Demo: Observable Plot + SQLite</h2>
<p><i>Try removing a <code>0</code> from the <code>WHERE</code> condition, then click away from the <code><script></code> to <abbr title="lose focus">blur</abbr> and reevaluate.</i></p>
<script type="module" class="echo reflect" data-display="chinook" contenteditable="true">
import * as Plot from 'https://esm.run/@observablehq/plot@0.6.16';
celine.cell("chinook", async () => {
const url = "https://maxbo.me/celine/static/chinook.db";
const db = await library.SQLiteDatabaseClient().open(url);
const tracks = await db.sql`SELECT * FROM tracks WHERE Milliseconds < 1000000`;
return Plot.plot({
title: htl.html`<h3>Track length distribution</h3>`,
marks: [
Plot.rectY(
tracks,
Plot.binX({ y: "count" }, { x: "Milliseconds", tip: true })
),
Plot.ruleY([0])
]
});
});
</script>
<small>
<a href="https://observablehq.com/framework/lib/sqlite"><cite><code>SQLiteDatabaseClient</code> docs</cite></a>
</small>
<h2><a href="#api" id="api"></a>API</h2>
<p>The following <code><styles></code>s are marked <code>contenteditable</code> and reevaluate on edit.</p>
<h3><a href="#.echo" id=".echo"></a><code>.echo</code></h3>
<p>The <code>.echo</code> class can display <code><script></code> and <code><style></code> elements inline, using a <a href="https://blog.glyphdrawing.club/font-with-built-in-syntax-highlighting/"><cite>font with built-in syntax highlighting</cite></a>.</p>
<p><i>Try changing the <code>border</code> thickness!</i></p>
<style class="echo" id="echo-style" contenteditable="true">#echo-style {
border: 3px solid fuchsia;
}
</style>
<p><code>.echo</code> has a dark mode. Set the <code>class</code> attribute to <code>echo dark</code> to enable:</p>
<style class="echo dark" id="echo-dark-style" contenteditable="true">#echo-dark-style {
transform: rotate(0.5deg);
}
</style>
<h3><a href="#.reflect" id=".reflect"></a><code>.reflect</code></h3>
<p>The <code>.reflect</code> class forces <code><script></code> and <code><style></code> elements to display their opening and closing tags, <code>type</code>, <code>class</code>, <code>id</code> / <code>data-display</code>, and <code>contenteditable</code> attributes (a little trick from <a href="https://secretgeek.github.io/html_wysiwyg/html.html"><cite>This page is a truly naked, brutalist html quine</cite></a>).</p>
<style class="echo reflect" id="reflect-style" contenteditable="true">
#reflect-style {
border: 3px solid lime;
}
</style>
<p>All of the following <code><script></code>s are marked <code>contenteditable</code> and reevaluate on <abbr title="loss of focus">blur</abbr>.</p>
<h3><a href="#cell" id="cell"></a><code>celine.cell(name,<wbr> [inputs, ]definition)</code></h3>
<p>The <code>cell</code> constructor declares a reactive cell called <code>"${name}"</code>.</p>
<p>The <code>definition</code> can be <code>T</code> or <code>(...inputs) => T</code>, where <code>T</code> can be <code>object</code>, <code>Promise<?></code>, <code>Iterator<?></code>, or <code>AsyncIterator<?></code>.</p>
<p>Cells render their current value above an element that has a <code>data-display</code> attribute the same as the cell's <code>name</code>. Thus, to render the counter value above the <code><script></code>, we set <code>data-display="counter"</code> on the <code><script></code>:</p>
<script type="module" class="echo reflect" data-display="counter" contenteditable="true">
celine.cell("counter", async function* () {
let i = 0;
while (true) {
await library.Promises.delay(1000);
yield i++;
}
});
</script>
<p>The <code>cell</code> constructor accepts <code>inputs</code>, a list of other cell names to depend on.</p></p>
<p>Here we use <a href="https://observablehq.com/@observablehq/htl"><cite>Hypertext Literal</cite></a>'s <code>html</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">template literal</a>, to transform the value of another cell:</p>
<script type="module" class="echo reflect" data-display="fizzbuzz" contenteditable="true">
celine.cell("fizzbuzz", ["counter"], (counter) => {
if (counter % 3 === 0 && counter % 5 === 0) {
return htl.html`<b style="color: purple">FizzBuzz</b>`;
} else if (counter % 3 === 0) {
return htl.html`<b style="color: red">Fizz</b>`;
} else if (counter % 5 === 0) {
return htl.html`<b style="color: blue">Buzz</b>`;
} else {
return htl.html`<b>${counter}</b>`;
}
});
</script>
<p>A <code><script></code> declaring a cell can be hidden inside a <code><details></code>s element.</p>
<details data-display="hue" id="hue">
<summary>Show code</summary>
<script type="module" class="echo reflect" contenteditable="true">
celine.cell("hue", async function* () {
let hue = 0;
while (true) {
await library.Promises.delay(1000);
yield htl.html`<span style="color: hsl(${hue}, 100%, 40%);">
Hello from inside the <details> element
</span>`;
hue = (hue + 55) % 360;
}
});
</script>
</details>
<br />
<p>To display the cell's current value above the <code><details></code> element, rather than above the <code><script></code>, we add <code>data-display="hue"</code> to the <code><details></code> element, as the cell's <code>name</code> is <code>"hue"</code>:</p>
<script type="module" data-display="innerhue">
celine.cell("innerhue", () => {
return htl.html`<span class='echo'>${document.getElementById("hue").outerHTML}</span>`;
});
</script>
<h3><a href="#viewof" id="viewof"></a><code>celine.viewof(name,<wbr> [inputs, ]definition)</code></h3>
<p>The <code>viewof</code> constructor is a special constructor designed to work with <a href="https://github.com/observablehq/inputs"><cite>Observable Inputs</cite></a>.</p>
<p><p>It declares 2 reactive cells: a cell called <code>"${name}"</code>, and a cell called <code>"viewof ${name}"</code> - one for the value, and one for the DOM element itself.</p>
<p>To display the DOM element above another element <code><script></code>, set <code>data-display="viewof ${name}"</code> on the element to which the input should be prepended.</p>
<p>Here, we want to display an input above the <code><script></code> element, so we set <code>data-display="viewof password"</code> on the <code><script></code>:</p>
<script type="module" class="echo reflect" data-display="viewof password" contenteditable="true">
celine.viewof("password", () => {
return Inputs.text({ label: "Password", placeholder: "hunter2" });
});
</script>
<p>We still have to depend on the cell called <code>"password"</code> to use the input's value:</p>
<script type="module" class="echo reflect" data-display="strength" contenteditable="true">
celine.cell("strength", ["password"], (password) => {
// please don't use this for real password strength checking
if (password.length >= 12) {
return htl.html`<b style="color: green">Strong</b>`;
} else if (password.length >= 10) {
return htl.html`<b style="color: lime">Fairly Strong</b>`;
} else if (password.length >= 8) {
return htl.html`<b style="color: orange">Medium</b>`;
} else if (password.length >= 6) {
return htl.html`<b style="color: gold">Weak</b>`;
} else if (password.length >= 4) {
return htl.html`<b style="color: red">Very Weak</b>`;
} else {
return htl.html`<b style="color: darkred">Extremely Weak</b>`;
}
});
</script>
<p>For further information on how to create custom inputs, see the <a href="https://observablehq.com/@observablehq/synchronized-inputs"><cite>Synchronized Inputs</cite></a> guide.</p>
<h3><a href="#silent" id="silent"></a><code>celine.silentCell(name,<wbr> [inputs, ]definition)</code></h3>
<p>The <code>silentCell</code> constructor declares a cell that doesn't need an element annotated with <code>
data-display="${name}"</code> declared in the HTML. It is useful for cells that are used as inputs to other cells, but whose values do not need to be displayed.</p>
</code>
<script type="module" class="echo reflect" contenteditable="true">
celine.silentCell("silent", () => {
return "This string does NOT render anywhere, but can be used by other cells.";
});
</script>
<h3><a href="#mutable" id="mutable"></a><code>celine.mutable(name,<wbr> value)</code> / <code>celine.silentMutable(name,<wbr> value)</code></h3>
<p>The <code>mutable</code> (and <code>silentMutable</code>) constructor declares a cell <em>and</em> returns a reference that can be mutated. Mutations propagate to cells that depend upon it.</p>
<script type="module" class="echo reflect" data-display="ref" contenteditable="true">
window.ref = celine.mutable("ref", 3)
</script>
<script type="module" class="echo reflect" data-display="viewof increment" contenteditable="true">
celine.viewof("increment", () => {
const increment = () => ++ref.value;
const reset = () => ref.value = 0;
return Inputs.button([["Increment", increment], ["Reset", reset]]);
});
</script>
<script type="module" class="echo reflect" data-display="sword" contenteditable="true">
celine.cell("sword", ["ref"], (ref) => {
return htl.html`↜(╰ •ω•)╯ |${'═'.repeat(ref)}═ﺤ`
});
</script>
<h3><a href="#import" id="import"></a><code>celine.import(source,<wbr> cells)</code></h3>
<p>The <code>import</code> method imports cells from <a href="https://observablehq.com/documentation/notebooks/"><cite>Observable Notebooks</cite></a>, emulating Observable's <a href="https://observablehq.com/documentation/notebooks/imports"><cite>import syntax</cite></a>. The <code>source</code> can be an Observable notebook URL (e.g. <a href="https://observablehq.com/@mjbo/celine-celine-import-target">https://observablehq.com/@mjbo/celine-celine-import-target</a>) or an API URL (e.g. <a href="https://api.observablehq.com/@mjbo/celine-celine-import-target.js?v=4">https://api.observablehq.com/@mjbo/celine-celine-import-target.js?v=4</a>).</p>
<p>The target notebook <a href="https://observablehq.com/@mjbo/celine-celine-import-target">@mjbo/celine-celine-import-target</a> defines:</p>
<pre>
importMeUnchanged = "importMeUnchanged"
renameMe = "renameMe"
overrideMeWithANumber = 0
out = overrideMeWithANumber * 10
</pre>
<p>which we can import, with aliases and overrides:</p>
<script type="module" class="echo reflect" data-display="import" contenteditable="true">
celine.import(
"https://observablehq.com/@mjbo/celine-celine-import-target",
[ "importMeUnchanged",
{name: "renameMe", alias: "renamed"},
{name: "overrideMeWithANumber", with: "counter"},
"out" ]
);
celine.cell("import",
["importMeUnchanged", "renamed", "overrideMeWithANumber", "out"],
(importMeUnchanged, renamed, overrideMeWithANumber, out) => {
return JSON.stringify({ importMeUnchanged, renamed, overrideMeWithANumber, out }, null, 2);
}
);
</script>
Observe that the <code>overrideMeWithANumber</code> cell is overridden with the value of the <code>counter</code> cell, and the <code>out</code> cell is the product of <code>overrideMeWithANumber</code> and 10.
<p>
Let's try and import something with a complex dependency tree:
</p>
<script data-display="d3import" type="module" class="echo reflect" contenteditable="true">
celine.import(
"https://observablehq.com/@d3/interrupted-sinu-mollweide",
[{name: "map", alias: "landcoverMap"}]
);
celine.cell("d3import", ["landcoverMap"], (landcoverMap) => {
return landcoverMap;
});
</script>
<h3><a href="#library" id="library"></a><code>celine.library</code> / Observable standard library</h3>
<p>There are many useful utilities in the <a href="https://github.com/observablehq/stdlib"><cite>Observable standard library</cite></a>. Inspect the contents of the <code>celine.library</code> object:</p>
<script type="module" class="echo reflect" data-display="lib" contenteditable="true">
celine.cell("lib", () => {
return celine.library;
});
</script>
<h4><a href="#tex" id="tex"></a>TeX</h4>
<script type="module" class="echo reflect" data-display="eq1" contenteditable="true">
celine.cell("eq1", async () => {
const tex = await celine.library.tex();
return tex`c = \pm\sqrt{a^2 + b^2}`;
});
</script>
<h5><a href="#celine_tex" id="#celine_tex"></a><code>celine.tex</code></h5>
<p>Because rendering TeX is so useful, <cite>@celine/celine</cite> provides a shorthand <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">template literal</a>, <code>celine.tex</code>:</p>
<script type="module" class="echo reflect" data-display="eq2" contenteditable="true">
celine.cell("eq2", celine.tex`c = \pm\sqrt{a^2 + b^2}`);
</script>
<p>Because cells render their contents <code>display: inline</code> (<i>celine ⇒ <q>cell inline</q></i>), we can embed the script <script type="module" class="echo reflect" data-display="eq3" contenteditable="true">celine.cell("eq3", celine.tex`c = \pm\sqrt{a^2 + b^2}`)</script> in the middle of the <code><p></code> element.</p>
<p>In non-demonstration use, we'd also leave off the <code>.echo</code> and <code>.reflect</code> classes, to render <script type="module" data-display="eq4">celine.cell("eq4", celine.tex`c = \pm\sqrt{a^2 + b^2}`)</script> inline.</p>
<p>To render TeX centered, wrap the <code><script></code> with a <code><div style="text-align: center"></code>:</p>
<div style="text-align: center; margin-top: 2ch; margin-bottom: 1ch;">
<script type="module" data-display="eq5" contenteditable="true">
celine.cell("eq5", celine.tex`c = \pm\sqrt{a^2 + b^2}`);
</script>
</div>
<p>Both <code>tex</code> template literals are unconfigurable. You will need to import the <a href="https://katex.org"><cite>KaTeX</cite></a> library proper if you'd like to modify any of its <a href="https://katex.org/docs/options">options</a>.</p>
<h4><a href="#markdown" id="markdown"></a>Markdown</h4>
<script type="module" class="echo reflect" data-display="md" contenteditable="true">
celine.cell("md", async () => {
const elements = [
{ "symbol": "Co", "name": "Cobalt", "number": 27 },
{ "symbol": "Cu", "name": "Copper", "number": 29 },
{ "symbol": "Sn", "name": "Tin", "number": 50 },
{ "symbol": "Pb", "name": "Lead", "number": 82 }
];
const md = await celine.library.md();
return md`
| Name | Symbol | Atomic number |
|-----------|-------------|---------------|${elements.map(e => `
| ${e.name} | ${e.symbol} | ${e.number} |`)}
`;
});
</script>
<h5><a href="#celine_md" id="celine_md"></a><code>celine.md</code></h5>
<p>Markdown also has a shorthand <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">template literal</a>, <code>celine.md</code>:</p>
<script type="module" class="echo reflect" data-display="md2" contenteditable="true">
celine.cell("md2", celine.md`
1. I
2. love
3. simple
4. lists
`);
</script>
<h4><a href="#graphviz" id="graphviz"></a>Graphviz</h4>
<p>
<a href="https://observablehq.com/framework/lib/dot">Docs</a>
</p>
<script type="module" class="echo reflect" data-display="dot" contenteditable="true">
celine.cell("dot", async () => {
const dot = await celine.library.dot();
return dot`
digraph G {
rankdir = LR
a -> b -> c
}`;
});
</script>
<h4><a href="#mermaid" id="mermaid"></a>Mermaid</h4>
<p>
<a href="https://observablehq.com/framework/lib/mermaid">Docs</a>
</p>
<script type="module" class="echo reflect" data-display="cmermaid" contenteditable="true">
celine.cell("cmermaid", async () => {
const mermaid = await celine.library.mermaid();
return mermaid`
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
`;
});
</script>
<h4><a href="#leaflet" id="leaflet"></a>Leaflet</h4>
<p>
<a href="https://observablehq.com/framework/lib/leaflet">Docs</a>
</p>
<script type="module" class="echo reflect" data-display="L" contenteditable="true">
celine.cell("L", async function* () {
const L = await celine.library.L();
const container = htl.html`<div id="map" style="height: 300px;"></div>`;
yield container;
const map = L.map(container).setView([-33.8688, 151.2093], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright"><cite>OpenStreetMap</cite></a> contributors'
}).addTo(map);
});
</script>
<h4><a href="#duckdb" id="duckdb"></a>DuckDB</h4>
<p>
<a href="https://observablehq.com/framework/lib/duckdb">Docs</a>
</p>
<script type="module" class="echo reflect" data-display="duck" contenteditable="true">
import * as Plot from 'https://esm.run/@observablehq/plot@0.6.16';
celine.cell("duck", async () => {
const db = await celine.library.DuckDBClient().of();
await db.sql`
CREATE TABLE gaia
AS SELECT *
FROM read_parquet('https://maxbo.me/celine/static/gaia-sample.parquet')`;
const bins = await db.sql`
SELECT
floor(ra / 2) * 2 + 1 AS ra,
floor(dec / 2) * 2 + 1 AS dec,
count() AS count
FROM gaia
GROUP BY 1, 2`
return Plot.plot({
aspectRatio: 1,
caption: htl.html`<p>Source: <a href="https://observablehq.com/@cmudig/peeking-into-the-gaia-star-catalog"><cite>Peeking into the Gaia Star Catalog</cite></a></p>`,
x: {domain: [0, 360]},
y: {domain: [-90, 90]},
marks: [
Plot.frame({fill: 0}),
Plot.raster(bins, {
x: "ra",
y: "dec",
fill: "count",
width: 360 / 2,
height: 180 / 2,
imageRendering: "pixelated"
})
]
})
});
</script>
<h3><a href="#special-cells" id="special-cells"></a>Special cells</h3>
<p>
Like Observable notebooks, <cite>@celine/celine</cite> implicitly defines special cells: <code>invalidation</code>, <code>now</code>, <code>width</code>, and <code>visibility</code>.
</p>
<h4><a href="#invalidation" id="invalidation"></a><code>invalidation</code></h4>
<p>
<code>invalidation</code> is a promise that resolves when the current cell is re-evaluated. It is useful for cleanup, such as aborting fetches or disposing of resources before the cell runs again.
</p>
<p>
Focus and then blur the following cell to see the count increment:
</p>
<script type="module" class="echo reflect" data-display="invalidation-example" contenteditable="true">
celine.cell("invalidation-example", ["invalidation"], (invalidation) => {
invalidation.then(() => window.invalidationCount.value++);
});
</script>
<script type="module" class="echo reflect" data-display="invalidation-count" contenteditable="true">
window.invalidationCount = celine.mutable("invalidation-count", 0);
</script>
<h4><a href="#now" id="now"></a><code>now</code></h4>
<p>
<code>now</code> is the current value of <code>Date.now()</code>. Use it when you want a cell to react to the current time without wiring up your own timer.
</p>
<script type="module" class="echo reflect" data-display="now-example" contenteditable="true">
celine.cell("now-example", ["now"], (now) => {
return new Date(now).toLocaleTimeString();
});
</script>
<h4><a href="#width" id="width"></a><code>width</code></h4>
<p>
<code>width</code> is the current width of the cell's container in pixels. Use it when you need responsively sized plots or other elements that can't be responsively managed by CSS.
</p>
<p>
This is especially useful with <a href="https://observablehq.com/plot/what-is-plot"><cite>Observable Plot</cite></a>, where setting <code>width</code> from the <code>width</code> cell keeps the chart sized to the page instead of hard-coding a fixed pixel width.
</p>
<script type="module" class="echo reflect" data-display="responsive-plot" contenteditable="true">
import * as Plot from 'https://esm.run/@observablehq/plot@0.6.16';
celine.cell("responsive-plot", ["width"], async (width) => {
const alphabet = await celine.library.alphabet();
return Plot.plot({
width,
marks: [
Plot.barY(alphabet, {x: "letter", y: "frequency"})
]
});
});
</script>
<h4><a href="#visibility" id="visibility"></a><code>visibility</code></h4>
<p>
<code>visibility</code> is a function that returns a promise that resolves when the current cell becomes visible in the viewport. It is handy for deferring expensive work until the reader has actually scrolled to that part of the page.
</p>
<script type="module" class="echo reflect" data-display="visibility-example" contenteditable="true">
celine.cell("visibility-example", ["visibility"], async function* (visibility) {
yield "Waiting to become visible...";
await visibility();
let i = 0;
while (true) {
yield `I became visible ${i} seconds ago!`;
await library.Promises.delay(1000);
i++;
}
})
</script>
<h2><a href="#cell-css" id="cell-css"></a>cell.css</h2>
<p>
A cell's output is rendered by <a href="https://github.com/observablehq/inspector"><cite>@observablehq/inspector</cite></a>.
<a href="celine/cell.css">cell.css</a> adds cell-state specific styles:
</p>
<h3><a href="#pending-state" id="pending-state"></a>Pending state styling</h3>
<p>
While a cell is waiting on a <code>Promise</code>, the inspector applies a <code>observablehq--running</code> class on the cell's output element.
</p>
<script type="module" class="echo reflect" data-display="status-pending-demo" contenteditable="true">
celine.cell("status-pending-demo", () => {
return new Promise(() => {});
});
</script>
<h3><a href="#rejected-state" id="rejected-state"></a>Rejected state styling</h3>
<p>
If a cell's definition throws, or a returned <code>Promise</code> rejects, the inspector applies a <code>observablehq--error</code> class on the cell's output element.
</p>
<script type="module" class="echo reflect" data-display="status-rejected-demo" contenteditable="true">
celine.cell("status-rejected-demo", () => {
throw new Error("This cell has thrown an error");
});
</script>
<h2><a href="#recipes" id="recipes"></a>Recipes</h2>
<h3><a href="#1st-party-pairings" id="1st-party-pairings"></a>1st-party library pairings</h3>
<!-- <h3><a href="#dfn" id="dfn"><dfn>: The Definition element</h3>
<p>
HTML has a little known element, <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dfn"><dfn></a>, that can be used to define terms.
</p> -->
<h4><a href="#celine-libertine" id="celine-libertine"></a>@celine/libertine</h4>
<p><cite>@celine/libertine</cite> provides a stylesheet based around the <a href="https://en.wikipedia.org/wiki/Linux_Libertine"><cite>Linux Libertine</cite></a> typeface, one common in academic typesetting.</p>
<p>
Information about it lives on a subpage, <a href="./libertine/index.html">/libertine</a>.
</p>
<h4><a href="#celine-bibhtml" id="celine-bibhtml"></a>@celine/bibhtml</h4>
<p>
<cite>@celine/bibhtml</cite> is a <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_components">Web
Components</a>-based referencing system for HTML documents.
</p>
<p>
Information about it lives on a subpage, <a href="./bibhtml/index.html">/bibhtml</a>.
</p>
<h3><a href="#3rd-party-library-pairings" id="#3rd-party-library-pairings"></a>3rd-party library pairings</h3>
<p>Some libraries that pair well with <cite>@celine/celine</cite> are:</p>
<h4><a id="pyodide" href="#pyodide"></a>Pyodide</h4>
<p><a href="https://pyodide.org/"><cite>Pyodide</cite></a> is a port of CPython to WebAssembly.</p>
<script type="module" class="echo reflect" data-display="mean" contenteditable="true">
import { loadPyodide } from 'https://cdn.jsdelivr.net/pyodide/v0.26.3/full/pyodide.mjs';
celine.cell("mean", async () => {
const pyodide = await loadPyodide();
await pyodide.loadPackage(['numpy']);
return pyodide.runPython(`
import numpy as np
x = np.random.randn(1000)
x.mean()
`);
});
</script>
<h4><a id="webr" href="#webr"></a>WebR</h4>
<p><a href="https://docs.r-wasm.org/webr/latest/"><cite>WebR</cite></a> is a version of the statistical language R compiled for the browser using WebAssembly, via Emscripten.</p>
<script type="module" class="echo reflect" data-display="summary" contenteditable="true">
import { WebR } from 'https://webr.r-wasm.org/latest/webr.mjs';
celine.cell("summary", async () => {
const webR = new WebR();
await webR.init();
const shelter = await new webR.Shelter();
const capture = await shelter.captureR(`
fit <- lm(mpg ~ am, data=mtcars)
print(summary(fit))`);
return capture.output.map(d => d.data).join('\n');
});