Skip to content

Commit 9fa6b37

Browse files
Document that viewers can be embedded inside of hiccup code (#439)
Updated the Book of Clerk in both the hiccup section and "composing viewers" section with better examples. Co-authored-by: Philippa Markovics <[email protected]>
1 parent d46560c commit 9fa6b37

File tree

1 file changed

+62
-33
lines changed

1 file changed

+62
-33
lines changed

book.clj

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,17 @@
168168
;; You can style elements, using [Tailwind CSS](https://tailwindcss.com/docs/utility-first).
169169
(clerk/html [:button.bg-sky-500.hover:bg-sky-700.text-white.rounded-xl.px-2.py-1 "✨ Tailwind CSS"])
170170

171-
;; The `html` viewer is also able to display SVG, taking either a hiccup vector or a SVG string.
171+
;; The `html` viewer is able to display SVG, taking either a hiccup vector or a SVG string.
172172
(clerk/html [:svg {:width 500 :height 100}
173173
[:circle {:cx 25 :cy 50 :r 25 :fill "blue"}]
174174
[:circle {:cx 100 :cy 75 :r 25 :fill "red"}]])
175175

176+
;; You can also embed other viewers inside of hiccup.
177+
178+
(clerk/html [:div.flex.justify-center.space-x-6
179+
[:p "a table is next to me"]
180+
(clerk/table [[1 2] [3 4]])])
181+
176182
;; ### 🔢 Tables
177183

178184
;; Clerk provides a built-in data table viewer that supports the three
@@ -339,37 +345,60 @@
339345

340346
;; ### 🍱 Composing Viewers
341347

342-
;; Viewers compose, so you can for example use the plotly viewer inside the grid viewers.
348+
;; Viewers compose, so, for example, you can lay out multiple independent Vega charts using Clerk’s grid viewers:
343349

344350
^{::clerk/visibility {:code :fold}}
345351
(do
346-
(def donut-chart
347-
(clerk/plotly {:data [{:values [27 11 25 8 1 3 25]
348-
:labels ["US" "China" "European Union" "Russian Federation" "Brazil" "India" "Rest of World"]
349-
:text "CO2"
350-
:textposition "inside"
351-
:domain {:column 1}
352-
:hoverinfo "label+percent+name"
353-
:hole 0.4
354-
:type "pie"}]
355-
:layout {:showlegend false
356-
:width 200
357-
:height 200
358-
:margin {:t 0 :b 0 :r 0 :l 0}
359-
:annotations [{:font {:size 20} :showarrow false :x 0.5 :y 0.5 :text "CO2"}]}
360-
:config {:responsive true}}))
361-
362-
(def contour-plot
363-
(clerk/plotly {:data [{:z [[10 10.625 12.5 15.625 20]
364-
[5.625 6.25 8.125 11.25 15.625]
365-
[2.5 3.125 5.0 8.125 12.5]
366-
[0.625 1.25 3.125 6.25 10.625]
367-
[0 0.625 2.5 5.625 10]]
368-
:type "contour"}]
369-
:layout {:margin {:t 0 :b 0 :r 0 :l 0}}})))
370-
371-
(clerk/col (clerk/row donut-chart donut-chart donut-chart)
372-
contour-plot)
352+
(def stock-colors
353+
{"AAPL" "#4c78a8" "AMZN" "#f58518" "GOOG" "#e45756" "IBM" "#72b7b2" "MSFT" "#54a24b"})
354+
(def combined-stocks-chart
355+
(clerk/vl {:width 600
356+
:height 200
357+
:data {:url "https://vega.github.io/vega-lite/examples/data/stocks.csv"}
358+
:mark "area"
359+
:encoding {:x {:timeUnit "yearmonth" :field "date" :axis {:format "%Y"}}
360+
:y {:aggregate "sum" :field "price"}
361+
:color {:field "symbol"
362+
:scale {:domain (keys stock-colors) :range (vals stock-colors)}}}
363+
:embed/opts {:actions false}}))
364+
(defn stock-chart [symbol]
365+
(clerk/vl {:title symbol
366+
:width 100
367+
:height 40
368+
:mark "area"
369+
:data {:url "https://vega.github.io/vega-lite/examples/data/stocks.csv"}
370+
:transform [{:filter (str "datum.symbol == '" symbol "'")}]
371+
:encoding {:x {:field "date" :type "temporal" :title nil :axis {:grid false}}
372+
:y {:field "price" :type "quantitative" :title nil :axis {:grid false} :scale {:domain [0 700]}}
373+
:color {:field "symbol" :type "nominal" :legend nil :scale {:domain [symbol]
374+
:range [(get stock-colors symbol)]}}}
375+
:embed/opts {:actions false}})))
376+
377+
(clerk/col
378+
(clerk/row (stock-chart "AAPL")
379+
(stock-chart "AMZN")
380+
(stock-chart "GOOG")
381+
(stock-chart "IBM")
382+
(stock-chart "MSFT"))
383+
combined-stocks-chart)
384+
385+
;; Viewers can also be embedded in Hiccup. The following example shows
386+
;; how this is used to provide a custom callout for a `clerk/image`.
387+
388+
(clerk/html
389+
[:div.relative
390+
(clerk/image "https://images.unsplash.com/photo-1608993659399-6508f918dfde?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2070&q=80")
391+
[:div.absolute
392+
{:class "left-[25%] top-[21%]"}
393+
[:div.border-4.border-emerald-400.rounded-full.shadow
394+
{:class "w-8 h-8"}]
395+
[:div.border-t-4.border-emerald-400.absolute
396+
{:class "w-[80px] rotate-[30deg] left-4 translate-x-[10px] translate-y-[10px]"}]
397+
[:div.border-4.border-emerald-400.absolute.text-white.font-sans.p-3.rounded-md
398+
{:class "bg-black bg-opacity-60 text-[13px] w-[280px] top-[66px]"}
399+
"Cat's paws are adapted to climbing and jumping, walking and running, and have protractible claws for self-defense and hunting."]]])
400+
401+
#_(clerk/html [:div.flex.justify-around donut-chart donut-chart donut-chart])
373402

374403
;; ### 🤹🏻 Applying Viewers
375404

@@ -475,7 +504,7 @@ v/default-viewers
475504

476505
;; #### ⚙️ Transform
477506

478-
;; When writing your own viewer, the first extention point you should reach for is `:tranform-fn`.
507+
;; When writing your own viewer, the first extention point you should reach for is `:tranform-fn`.
479508

480509
#_ "exercise: wrap this in `v/present` and call it at the REPL"
481510
(v/with-viewer {:transform-fn #(clerk/html [:pre (pr-str %)])}
@@ -502,7 +531,7 @@ v/default-viewers
502531
"James Clerk Maxwell"))
503532

504533

505-
;; **Passing modified viewers down the tree**
534+
;; **Passing modified viewers down the tree**
506535

507536
#_ "TODO: move this into clerk?"
508537
(defn add-child-viewers [viewer viewers]
@@ -705,7 +734,7 @@ v/table-viewer
705734
;; ## 🙈 Controlling Visibility
706735

707736
;; Visibility for code and results can be controlled document-wide and
708-
;; per top-level form. By default, Clerk will always show code and
737+
;; per top-level form. By default, Clerk will always show code and
709738
;; results for a notebook.
710739

711740
;; You can use a map of the following shape to set the visibility of
@@ -736,7 +765,7 @@ v/table-viewer
736765
;;
737766
;; ^{::clerk/visibility {:code :hide}} (shuffle (range 25))
738767
;;
739-
;; This will hide the code but only show the result:
768+
;; This will hide the code but only show the result:
740769

741770
^{::clerk/visibility {:code :hide}} (shuffle (range 25))
742771

0 commit comments

Comments
 (0)