Skip to content

Commit 3ccf069

Browse files
zampinomkphilippamarkovics
authored
Bump clerk (#20)
* Simplify dictionary notebook with Clerk synced atoms and viewer-eval viewer * Fix zipper notebook * Bump slideshow viewer * Pass at dropping unnecessary v/html Co-authored-by: Martin Kavalar <[email protected]> Co-authored-by: Philippa Markovics <[email protected]>
1 parent cb212b2 commit 3ccf069

File tree

11 files changed

+74
-339
lines changed

11 files changed

+74
-339
lines changed

deps.edn

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{:paths ["dev" "notebooks" "resources"]
2-
:deps {io.github.nextjournal/clerk {:git/sha "fa0ddcf000bbcd211008c93d8bd0b3047629f6b6"}
3-
io.github.nextjournal/clerk-slideshow {:git/sha "ea9b27e5ec2e5ad57e889060521451ed9138325b"}
2+
:deps {io.github.nextjournal/clerk {:git/sha "5620618c378be87b6b7c695c145424dded1ef080"
3+
#_#_:local/root "../clerk"}
4+
io.github.nextjournal/clerk-slideshow {:git/sha "562f634494a1e1a9149ed78d5d39fd9486cc00ba"}
45

56
;; keep 1.10 until `kixi/stats` and `sicmutils` fix warnings
67
org.clojure/clojure {:mvn/version "1.10.3"}

notebooks/controls.clj

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
;; # Controls! 🎛
22
^{:nextjournal.clerk/visibility {:code :hide}}
33
(ns ^:nextjournal.clerk/no-cache controls
4-
(:require [clojure.string :as str]
5-
[nextjournal.clerk :as clerk]))
4+
(:require [nextjournal.clerk :as clerk]))
65

76
;; As we've seen in other notebooks, anyone using Clerk can extend the
87
;; viewers however they like without changing the source code of Clerk
@@ -12,14 +11,14 @@
1211
;; number as a slider like this:
1312

1413
(def slider
15-
{:pred ::clerk/var-from-def
14+
{:var-from-def? true
1615
:transform-fn (comp clerk/mark-presented
1716
(clerk/update-val (fn [{::clerk/keys [var-from-def]}]
1817
{:var-name (symbol var-from-def) :value @@var-from-def})))
1918
:render-fn '(fn [{:keys [var-name value]}]
20-
(v/html [:input {:type :range
21-
:initial-value value
22-
:on-change #(v/clerk-eval `(reset! ~var-name (Integer/parseInt ~(.. % -target -value))))}]))})
19+
[:input {:type :range
20+
:initial-value value
21+
:on-change #(v/clerk-eval `(reset! ~var-name (Integer/parseInt ~(.. % -target -value))))}])})
2322

2423
;; And then assign is to a particular atom like this:
2524
^{::clerk/viewer slider}
@@ -38,10 +37,9 @@
3837
;; with comments inline to help explain how everything works:
3938

4039
(def text-input
41-
;; We assign a predicate (i.e. `:pred`) function for this viewer,
42-
;; which in this case just checks to see if there's a `var-from-def`
43-
;; (that is, the form Clerk is evaluating defines a var).
44-
{:pred ::clerk/var-from-def
40+
;; We're telling Clerk to opt-out of the default behaviour (acting on the value held by
41+
;; the var) and return a map containing the actual var object instead.
42+
{:var-from-def? true
4543

4644
;; When we specify a `:transform-fn`, it gets run on the JVM side
4745
;; to pre-process our value before sending it to the front-end. In
@@ -70,11 +68,11 @@
7068
;; front- and back- ends of the system is extremely
7169
;; powerful — thanks, Lisp!
7270
:render-fn '(fn [{:keys [var-name value]}]
73-
(v/html [:input {:type :text
74-
:placeholder "⌨️"
75-
:initial-value value
76-
:class "px-3 py-3 placeholder-blueGray-300 text-blueGray-600 relative bg-white bg-white rounded text-sm border border-blueGray-300 outline-none focus:outline-none focus:ring w-full"
77-
:on-input #(v/clerk-eval `(reset! ~var-name ~(.. % -target -value)))}]))})
71+
[:input {:type :text
72+
:placeholder "⌨️"
73+
:initial-value value
74+
:class "px-3 py-3 placeholder-blueGray-300 text-blueGray-600 relative bg-white bg-white rounded text-sm border border-blueGray-300 outline-none focus:outline-none focus:ring w-full"
75+
:on-input #(v/clerk-eval `(reset! ~var-name ~(.. % -target -value)))}])})
7876

7977
^{::clerk/viewer text-input}
8078
(defonce text-state (atom ""))

notebooks/data_science.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
(clerk/table world-happiness)
183183

184184
;; Next, we're computing a linear regression for this dataset using [kixi.stats](https://github.com/MastodonC/kixi.stats).
185-
^{::clerk/viewer kixi-p/parameters}
185+
^{::clerk/viewer {:transform-fn (clerk/update-val kixi-p/parameters)}}
186186
(def linear-regression
187187
(transduce identity (kixi-stats/simple-linear-regression :score :gdp) world-happiness))
188188

notebooks/dictionary.clj

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
;; # 📔️ Regex Dictionary
2-
^{:nextjournal.clerk/visibility {:code :hide :result :hide}}
32
(ns dictionary
3+
{:nextjournal.clerk/visibility {:code :hide :result :hide}}
44
(:require [clojure.string :as str]
55
[clojure.java.io :as io]
66
[wordnet.core :as wordnet]
77
[nextjournal.clerk :as clerk]
8-
[controls :refer [text-input]]))
8+
[nextjournal.clerk.viewer :as v]))
99

1010
(def dict
1111
(try
@@ -21,13 +21,24 @@
2121
str/split-lines
2222
(filter #(seq (dict %)))))
2323

24+
(def text-input
25+
(assoc v/viewer-eval-viewer
26+
:render-fn
27+
'(fn [text-state]
28+
[:input {:type :text :placeholder "⌨️" :value @text-state
29+
:class "px-3 py-3 placeholder-blueGray-300 text-blueGray-600 relative bg-white bg-white rounded text-sm border border-blueGray-300 outline-none focus:outline-none focus:ring w-full"
30+
:on-input #(reset! text-state (.. % -target -value))}])))
31+
32+
(comment
33+
@text-state)
34+
2435
{::clerk/visibility {:result :show}}
2536

26-
(clerk/html [:div [:span {:class "font-bold"} "Type three or more letters of a regex!"]])
27-
^{:nextjournal.clerk/viewer text-input}
37+
^{::clerk/sync true ::clerk/viewer text-input}
2838
(defonce text-state (atom ""))
2939

30-
^::clerk/no-cache
40+
(clerk/html [:div [:span {:class "font-bold"} "Type three or more letters of a regex!"]])
41+
3142
(clerk/html
3243
(let [pat (re-pattern @text-state)
3344
filtered-words (if (> 3 (count @text-state)) []

notebooks/elements.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"bg-black")
2525
:style {:width 16 :height 16}}))
2626
row (fn [r] (into [:div.flex.inline-flex] (map cell) r))]
27-
(v/html (into [:div.flex.flex-col] (map row) board))))
27+
(into [:div.flex.flex-col] (map row) board)))
2828
(->> g1 (iterate evolve) (take 17))))
2929

3030
;; Clerk uses static analysis and a tiny bit of data flow to avoid needless recomputation.

notebooks/git.clj

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919
{:nextjournal.clerk/visibility {:result :show}}
2020

2121
(clerk/with-viewer
22-
{:transform-fn (comp v/mark-presented
23-
(v/update-val (partial map
24-
(fn [c]
25-
(update (select-keys c [:id :changed_files :time])
26-
:time #(subs (str %) 4 10))))))
27-
:render-fn '(fn [commits]
28-
(v/html
22+
{:transform-fn (comp v/mark-presented
23+
(v/update-val (partial map
24+
(fn [c]
25+
(update (select-keys c [:id :changed_files :time])
26+
:time #(subs (str %) 4 10))))))
27+
:render-fn '(fn [commits]
2928
[:div.min-h-screen.py-6.flex.flex-col.justify-center.sm:py-12.font-sans
3029
[:div.py-3.sm:max-w-xl.sm:mx-auto.w-full.px-2.sm:px-0
3130
(into
@@ -54,5 +53,5 @@
5453
:else "black")} filename [:br]])
5554
(:changed_files c)))]]]])
5655
(range)
57-
commits))]]))}
56+
commits))]])}
5857
commits)

notebooks/images.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
;; On the other hand, smaller images are centered and shown using
3434
;; their intrinsic dimensions:
35-
(ImageIO/read (URL. "https://etc.usf.edu/clipart/36600/36667/thermos_36667_sm.gif"))
35+
(ImageIO/read (URL. "https://nextjournal.com/data/QmeyvaR3Q5XSwe14ZS6D5WBQGg1zaBaeG3SeyyuUURE2pq?filename=thermos.gif&content-type=image/gif"))
3636

3737
;; If you find yourself using a library that returns images as a
3838
;; `ByteArray`, you can read the image into a `BufferedImage` by

notebooks/introduction.clj

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,16 @@
183183
;; to any form. Here we make our own little viewer to greet James
184184
;; Clerk Maxwell:
185185

186-
(clerk/with-viewer '#(v/html [:div "Greetings to " [:strong %] "!"])
186+
(clerk/with-viewer '(fn [name] [:div "Greetings to " [:strong name] "!"])
187187
"James Clerk Maxwell")
188188

189189
;; But we can do more interesting things, like using a predicate
190190
;; function to match numbers and turn them into headings, or
191191
;; converting string into paragraphs.
192192
(clerk/with-viewers (clerk/add-viewers [{:pred number?
193-
:render-fn '#(v/html [(keyword (str "h" %)) (str "Heading " %)])}
193+
:render-fn '(fn [n] [(keyword (str "h" n)) (str "Heading " n)])}
194194
{:pred string?
195-
:render-fn '#(v/html [:p %])}])
195+
:render-fn '(fn [s] [:p s])}])
196196
[1 "To begin at the beginning:"
197197
2 "It is Spring, moonless night in the small town, starless and bible-black,"
198198
3 "the cobblestreets silent and the hunched,"
@@ -202,8 +202,8 @@
202202
;; Or you could use black and white squares to render numbers:
203203
^::clerk/no-cache
204204
(clerk/with-viewers (clerk/add-viewers [{:pred number?
205-
:render-fn '#(v/html [:div.inline-block {:style {:width 16 :height 16}
206-
:class (if (pos? %) "bg-black" "bg-white border-solid border-2 border-black")}])}])
205+
:render-fn '(fn [n] [:div.inline-block {:style {:width 16 :height 16}
206+
:class (if (pos? n) "bg-black" "bg-white border-solid border-2 border-black")}])}])
207207
(take 10 (repeatedly #(rand-int 2))))
208208

209209
;; Or build your own colour parser and then use it to generate swatches:
@@ -214,11 +214,12 @@
214214
(str "(?i)"
215215
"(#(?:[0-9a-f]{2}){2,4}|(#[0-9a-f]{3})|"
216216
"(rgb|hsl)a?\\((-?\\d+%?[,\\s]+){2,3}\\s*[\\d\\.]+%?\\))")) %))
217-
:render-fn '#(v/html [:div.inline-block.rounded-sm.shadow
218-
{:style {:width 16
219-
:height 16
220-
:border "1px solid rgba(0,0,0,.2)"
221-
:background-color %}}])}])
217+
:render-fn '(fn [color-str]
218+
[:div.inline-block.rounded-sm.shadow
219+
{:style {:width 16
220+
:height 16
221+
:border "1px solid rgba(0,0,0,.2)"
222+
:background-color color-str}}])}])
222223
["#571845"
223224
"rgb(144,12,62)"
224225
"rgba(199,0,57,1.0)"
@@ -246,7 +247,7 @@
246247
{:nextjournal/presented? true
247248
:nextjournal/content-type "image/png"
248249
:nextjournal/value bytes})
249-
:render-fn '(fn [blob] (v/html [:img {:src (v/url-for blob)}]))}])
250+
:render-fn '(fn [blob] [:img {:src (v/url-for blob)}])}])
250251

251252

252253
(.. (HttpClient/newHttpClient)

0 commit comments

Comments
 (0)