Skip to content

Commit 1b838f5

Browse files
committed
Add two more exercises
1 parent d345ef5 commit 1b838f5

File tree

7 files changed

+78
-4
lines changed

7 files changed

+78
-4
lines changed

deps.edn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
dk.ative/docjure {:mvn/version "1.16.0"}
1515
clojusc/wordnet {:mvn/version "1.2.0"}
1616
clj-jgit/clj-jgit {:mvn/version "1.0.2" :exclusions [org.slf4j/slf4j-api]}
17+
scicloj/tablecloth {:mvn/version "7.000-beta-27"}
1718

1819
org.babashka/cli {:mvn/version "0.5.40"}
1920

exercises/ex_4_viewer_selection.clj

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
;; # Custom Viewer
2+
(ns exercises.ex-4-custom-viewer
3+
(:require [nextjournal.clerk :as clerk]
4+
[nextjournal.clerk.viewer :as viewer]
5+
[tablecloth.api :as tc]))
6+
7+
;; Here's a tablecloth `dataset` and a skeleton for a
8+
;; `dataset-viewer`. Modify the `:transform-fn` of the viewer so Clerk
9+
;; shows it as a `clerk/table`.
10+
11+
;; You might want to use a table format that preserves the column
12+
;; ordering.
13+
14+
(def dataset
15+
(-> "https://raw.githubusercontent.com/techascent/tech.ml.dataset/master/test/data/stocks.csv"
16+
(tc/dataset {:key-fn keyword})
17+
(tc/group-by (fn [row]
18+
{:symbol (:symbol row)
19+
:year (tech.v3.datatype.datetime/long-temporal-field :years (:date row))}))
20+
(tc/aggregate #(tech.v3.datatype.functional/mean (% :price)))))
21+
22+
(def dataset-viewer
23+
{:transform-fn (clerk/update-val (fn [dataset]
24+
,,,
25+
dataset))})
26+
27+
(clerk/with-viewer dataset-viewer
28+
dataset)
29+
30+
;; Extend the viewer with a `:pred-fn` that automatically selects it
31+
;; based on the `tc/dataset?` predicate. Validate that this works
32+
;; correctly using `viewer/viewer-for`.
33+
34+
;; Finally add the viewer to the namespace using `clerk/add-viewers!`
35+
;; so the dataset is rendered as a table, without requiring an
36+
;; explicit `clerk/with-viewer` call.
37+
38+
(clerk/add-viewers! [,,,])
39+
40+
dataset

exercises/ex_4_viewer_presentation.clj renamed to exercises/ex_5_viewer_presentation.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;; # Viewer Presentation
22

3-
(ns exercises.ex-3-viewer-presentation
3+
(ns exercises.ex-5-viewer-presentation
44
(:require [nextjournal.clerk :as clerk]
55
[nextjournal.clerk.viewer :as viewer]))
66

exercises/ex_5_caching.clj renamed to exercises/ex_6_caching.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
;; # Caching
2-
(ns ex-5-caching
2+
(ns ex-6-caching
33
(:require [nextjournal.clerk :as clerk]))
44

55
;; Note how the following notebook will show the same value

exercises/ex_6_visibility.clj renamed to exercises/ex_7_visibility.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(ns exercises.ex-6-visibility
1+
(ns exercises.ex-7-visibility
22
(:require [clojure.string :as str]
33
[meta-csv.core :as csv]
44
[nextjournal.clerk :as clerk]))

exercises/ex_7_viewer_require.clj renamed to exercises/ex_8_viewer_require.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
;; # Viewer Require
2-
(ns ex-7-viewer-require
2+
(ns ex-8-viewer-require
33
(:require [nextjournal.clerk :as clerk]))
44

55
;; In this exercise, we'll use the Clerk's viewer api to dynamically

exercises/ex_9_sync.clj

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
;; # Sync
2+
(ns ex-9-sync
3+
(:require [nextjournal.clerk :as clerk]
4+
[nextjournal.clerk.experimental :as cx]))
5+
6+
;; A `^{::clerk/sync true}` metadata annotation enables two-way
7+
;; bindings between the JVM and Clerk's viewer in the browser.
8+
9+
(defonce !text
10+
(atom "Hello"))
11+
12+
(defonce !repeat
13+
(atom 3))
14+
15+
(repeat @!repeat @!text)
16+
17+
;; Start by evaluating forms in the following `comment` form and show
18+
;; the notebook again to see it update. Then add a `^{::clerk/sync
19+
;; true}` metadata annotation and try again.
20+
21+
(comment
22+
(reset! !text "Hello, again")
23+
(reset! !text "Hello World")
24+
(reset! !text "Hello")
25+
26+
(swap! !repeat inc)
27+
(swap! !repeat dec)
28+
)
29+
30+
;; Finally, add `::clerk/viewer` with `cx/text-input` and `cx/slider`
31+
;; values to the metadata annotation. Play with the value propagation
32+
;; from both the browser and the REPL.
33+

0 commit comments

Comments
 (0)