|
1 | 1 | ;; # 📦 Dynamic JS Imports
|
2 | 2 | (ns js-import
|
3 |
| - {:nextjournal.clerk/visibility {:code :hide :result :hide}} |
4 |
| - (:require [nextjournal.clerk :as clerk] |
5 |
| - [nextjournal.clerk.viewer :as clerk.viewer] |
6 |
| - [clojure.data.csv :as csv])) |
| 3 | + (:require [clojure.data.csv :as csv] |
| 4 | + [nextjournal.clerk :as clerk] |
| 5 | + [nextjournal.clerk] |
| 6 | + [nextjournal.clerk.viewer :as viewer])) |
7 | 7 |
|
8 | 8 | ;; This example uses [Observable Plots](https://observablehq.com/plot) with data from https://allisonhorst.github.io/palmerpenguins/
|
9 | 9 |
|
10 |
| -(defn parse-float [^String s] (Float/parseFloat s)) |
11 |
| - |
12 |
| -^{::clerk/visibility {:code :show}} |
13 | 10 | (def observable-plot-viewer
|
14 | 11 | {:transform-fn clerk/mark-presented
|
15 | 12 | :render-fn
|
16 | 13 | '(fn [data _]
|
17 | 14 | [nextjournal.clerk.render/with-dynamic-import
|
18 | 15 | { :module "https://cdn.skypack.dev/@observablehq/[email protected]"}
|
19 | 16 | (fn [Plot]
|
20 |
| - [:div {:ref (fn [el] |
21 |
| - (when el |
22 |
| - (let [dot-plot (.. Plot |
23 |
| - (dot (clj->js data) |
24 |
| - (j/obj :x "flipper_length_mm" |
25 |
| - :y "body_mass_g" |
26 |
| - :fill "species")) |
27 |
| - (plot (j/obj :grid true)))] |
| 17 | + (let [dot-plot (.. Plot |
| 18 | + (dot (clj->js data) |
| 19 | + (j/obj :x "flipper_length_mm" |
| 20 | + :y "body_mass_g" |
| 21 | + :fill "species")) |
| 22 | + (plot (j/obj :grid true))) |
| 23 | + legend (.legend dot-plot "color")] |
| 24 | + [:div {:ref (fn [el] |
| 25 | + (if el |
28 | 26 | (doto el
|
29 |
| - (.append (.legend dot-plot "color")) |
30 |
| - (.append dot-plot)))))}])])}) |
| 27 | + (.append legend) |
| 28 | + (.append dot-plot)) |
| 29 | + (do (.remove legend) |
| 30 | + (.remove dot-plot))))}]))])}) |
31 | 31 |
|
32 |
| -^{::clerk/visibility {:code :show :result :show} |
33 |
| - ::clerk/viewer observable-plot-viewer} |
| 32 | +^{::clerk/viewer observable-plot-viewer} |
34 | 33 | (def palmer-penguins
|
35 | 34 | (-> (slurp "https://nextjournal.com/data/Qmf6FJyJxBQnB6TUZ3J9pdzHSs8UoewoY6WfdZHu1XxkD8?filename=penguins.csv&content-type=text/csv")
|
36 | 35 | (csv/read-csv)
|
37 |
| - clerk.viewer/use-headers |
38 |
| - clerk.viewer/normalize-table-data |
| 36 | + viewer/use-headers |
39 | 37 | (as-> data
|
40 |
| - (let [{:keys [head rows]} data] |
41 |
| - (map (fn [row] (zipmap head (reduce #(update %1 %2 parse-float) row [1 2 3 4]))) |
42 |
| - rows))))) |
| 38 | + (let [{:keys [head rows]} data] |
| 39 | + (map (fn [row] (zipmap head (reduce #(update %1 %2 parse-double) row [1 2 3 4]))) |
| 40 | + rows))))) |
43 | 41 |
|
44 | 42 | ;; or use `js/import` directly:
|
45 |
| -^{::clerk/visibility {:result :show :code :show} ::clerk/no-cache true ::clerk/width :wide} |
46 | 43 | (nextjournal.clerk/with-viewer
|
47 | 44 | '(fn [_]
|
48 |
| - (let [cc (nextjournal.clerk.render.hooks/use-promise |
49 |
| - (js/import "https://cdn.skypack.dev/canvas-confetti")) |
50 |
| - ref (nextjournal.clerk.render.hooks/use-ref)] |
51 |
| - (when cc |
52 |
| - [:div |
53 |
| - [:button.bg-teal-500.hover:bg-teal-700.text-white.font-bold.py-2.px-4.rounded.rounded-full.font-sans |
54 |
| - {:on-click #((.create cc @ref) |
55 |
| - (j/lit {:spread 80 :angle 45 :startVelocity 20 :origin {:x 0.25 :y 0.5}}))} "Peng 🎉!"] |
56 |
| - [:canvas |
57 |
| - {:ref ref |
58 |
| - :style {:width "100%" :height "500px"}}]]))) nil) |
| 45 | + (let [confetti (nextjournal.clerk.render.hooks/use-promise (js/import "https://cdn.skypack.dev/canvas-confetti"))] |
| 46 | + [:button.bg-teal-500.hover:bg-teal-700.text-white.font-bold.py-2.px-4.rounded.rounded-full.font-sans |
| 47 | + (if confetti {:on-click #(.default confetti)} {:class "bg-gray-200"}) "Peng 🎉!"])) {}) |
0 commit comments