Skip to content

Commit

Permalink
Use fully-qualified symbols in render fns & drop global aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
mk committed Feb 8, 2023
1 parent dc1bbfd commit 1b5bf5e
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 50 deletions.
9 changes: 4 additions & 5 deletions notebooks/js_import.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
(ns js-import
(:require [clojure.data.csv :as csv]
[nextjournal.clerk :as clerk]
[nextjournal.clerk]
[nextjournal.clerk.viewer :as viewer]))

;; This example uses [Observable Plots](https://observablehq.com/plot) with data from https://allisonhorst.github.io/palmerpenguins/
Expand All @@ -17,10 +16,10 @@
(fn [Plot]
(let [dot-plot (.. Plot
(dot (clj->js data)
(j/obj :x "flipper_length_mm"
:y "body_mass_g"
:fill "species"))
(plot (j/obj :grid true)))
(applied-science.js-interop/obj :x "flipper_length_mm"
:y "body_mass_g"
:fill "species"))
(plot (applied-science.js-interop/obj :grid true)))
legend (.legend dot-plot "color")]
[:div {:ref (fn [el]
(if el
Expand Down
39 changes: 19 additions & 20 deletions notebooks/jump_to_definition.clj
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
;; # 🤾🏼 Jump to Definition
(ns jump-to-definition
(:require
[clojure.string :as str]
[nextjournal.clerk :as clerk]
[nextjournal.clerk.render :as-alias render]
[nextjournal.clerk.render.hooks :as-alias hooks]
[nextjournal.clerk.viewer :as v]))
(:require [nextjournal.clerk :as clerk]
[nextjournal.clerk.render :as-alias render]
[nextjournal.clerk.render.hooks :as-alias hooks]
[nextjournal.clerk.viewer :as v]))

;; Trying various ways to support jump to definition
(def my-code-viewer
{:render-fn 'nextjournal.clerk.render/render-code :transform-fn clerk/mark-presented})


(v/->viewer-fn '(fn [spec] (let [plotly (hooks/use-d3-require "plotly.js-dist@2.15.1")
ref-fn (hooks/use-callback #(when % (.newPlot plotly % (clj->js spec ))) [spec plotly])]
(js/console.log "dude")
(when spec
(if plotly
[:div.overflow-x-auto [:div.plotly {:ref ref-fn}]]
render/default-loading-view)))))
(clerk/resolve-aliases '(fn [spec] (let [plotly (hooks/use-d3-require "plotly.js-dist@2.15.1")
ref-fn (hooks/use-callback #(when % (.newPlot plotly % (clj->js spec ))) [spec plotly])]
(js/console.log "dude")
(when spec
(if plotly
[:div.overflow-x-auto [:div.plotly {:ref ref-fn}]]
render/default-loading-view)))))

(v/->viewer-eval '(fn [spec] (let [plotly (hooks/use-d3-require "plotly.js-dist@2.15.1")
ref-fn (hooks/use-callback #(when % (.newPlot plotly % (clj->js spec))) [spec plotly])]
(js/console.log "dude")
(when spec
(if plotly
[:div.overflow-x-auto [:div.plotly {:ref ref-fn}]]
render/default-loading-view)))))
(v/->viewer-eval
(clerk/resolve-aliases '(fn [spec] (let [plotly (hooks/use-d3-require "plotly.js-dist@2.15.1")
ref-fn (hooks/use-callback #(when % (.newPlot plotly % (clj->js spec))) [spec plotly])]
(js/console.log "dude")
(when spec
(if plotly
[:div.overflow-x-auto [:div.plotly {:ref ref-fn}]]
render/default-loading-view))))))
2 changes: 1 addition & 1 deletion notebooks/multiviewer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
:transform-fn (comp clerk/mark-preserve-keys
(clerk/update-val transform-literal))
:render-fn '(fn [label->val]
(reagent/with-let [!selected-label (reagent/atom (ffirst label->val))]
(reagent.core/with-let [!selected-label (reagent.core/atom (ffirst label->val))]
[:<> (into
[:div.flex.items-center.font-sans.text-xs.mb-3
[:span.text-slate-500.mr-2 "View-as:"]]
Expand Down
16 changes: 8 additions & 8 deletions notebooks/render_aliases.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@

(def plotly-viewer
{:transform-fn clerk/mark-presented
:render-fn (clerk/resolve-aliases
'(fn [spec]
(let [plotly (hooks/use-d3-require "plotly.js-dist@2.15.1")
ref-fn (hooks/use-callback #(when % (.newPlot plotly % (clj->js spec))) [spec plotly])]
(when spec
(if plotly
[:div.overflow-x-auto [:div.plotly {:ref ref-fn}]]
render/default-loading-view)))))})
:render-fn #_clerk/resolve-aliases
'(fn [spec]
(let [plotly (hooks/use-d3-require "plotly.js-dist@2.15.1")
ref-fn (hooks/use-callback #(when % (.newPlot plotly % (clj->js spec))) [spec plotly])]
(when spec
(if plotly
[:div.overflow-x-auto [:div.plotly {:ref ref-fn}]]
render/default-loading-view))))})

(clerk/with-viewer plotly-viewer
{:layout {:title "A surface plot"}
Expand Down
8 changes: 4 additions & 4 deletions notebooks/rule_30.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

(def viewers
[{:pred number?
:render-fn '#(v/html [:div.inline-block {:style {:width 16 :height 16}
:class (if (pos? %) "bg-black" "bg-white border-solid border-2 border-black")}])}
:render-fn '#(vector :div.inline-block {:style {:width 16 :height 16}
:class (if (pos? %) "bg-black" "bg-white border-solid border-2 border-black")})}
{:pred (every-pred list? (partial every? (some-fn number? vector?)))
:render-fn '#(v/html (into [:div.flex.flex-col] (v/inspect-children %2) %1))}
:render-fn '#(into [:div.flex.flex-col] (nextjournal.clerk.render/inspect-children %2) %1)}
{:pred (every-pred vector? (complement map-entry?) (partial every? number?))
:render-fn '#(v/html (into [:div.flex.inline-flex] (v/inspect-children %2) %1))}])
:render-fn '#(into [:div.flex.inline-flex] (nextjournal.clerk.render/inspect-children %2) %1)}])

(clerk/add-viewers! viewers)

Expand Down
2 changes: 1 addition & 1 deletion notebooks/viewer_api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@

;; The clerk viewer api also includes `reagent` and `applied-science/js-interop`.
(clerk/with-viewer '(fn [_]
(reagent/with-let [counter (reagent/atom 0)]
(reagent.core/with-let [counter (reagent.core/atom 0)]
[:h3.cursor-pointer {:on-click #(swap! counter inc)} "I was clicked " @counter " times."]))
nil)
3 changes: 1 addition & 2 deletions notebooks/viewers/control_lab.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
;; # 🎛 Control Lab 🧑🏼‍🔬
(ns viewers.control-lab
{:nextjournal.clerk/visibility {:code :hide :result :hide}}
(:require [clojure.string :as str]
[nextjournal.clerk :as clerk]
(:require [nextjournal.clerk :as clerk]
[nextjournal.clerk.viewer :as viewer]))

;; Experimenting with ways of making controls. We start with two
Expand Down
6 changes: 3 additions & 3 deletions notebooks/viewers/errors.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
(clerk/with-viewer {:render-fn '(fn [x] (throw (ex-info "I blow up when called" {:some "data"})))}
:boom)

(clerk/with-viewer {:render-fn '(fn [_] (v/inspect-presented :crash))}
(clerk/with-viewer {:render-fn '(fn [_] (nextjournal.clerk.render/inspect-presented :crash))}
42)

(clerk/with-viewer {:render-fn '(fn [_] (v/html (v/inspect-presented :crash)))}
(clerk/with-viewer {:render-fn '(fn [_] (nextjournal.clerk.viewer/html (nextjournal.clerk.render/inspect-presented :crash)))}
42)

(clerk/with-viewer {:render-fn '(fn [_] (v/html [1 2 3]))}
(clerk/with-viewer {:render-fn '(fn [_] (nextjournal.clerk.viewer/html [1 2 3]))}
42)


Expand Down
4 changes: 2 additions & 2 deletions notebooks/viewers/table.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
{:rows (map (juxt identity inc) (range 100))
:head (map format-head head-data)}))

(clerk/with-viewers (clerk/add-viewers [(assoc v/image-viewer :render-fn '(fn [blob] (v/html [:img {:width "30px" :height "30px" :src (v/url-for blob)}])))])
(clerk/with-viewers (clerk/add-viewers [(assoc v/image-viewer :render-fn '(fn [blob] [:img {:width "30px" :height "30px" :src (nextjournal.clerk.viewer/url-for blob)}]))])
(clerk/table
{:rows (map (juxt identity dec) (range 1 100))
:head [(javax.imageio.ImageIO/read (java.net.URL. "https://upload.wikimedia.org/wikipedia/commons/1/17/Plus_img_364976.png"))
Expand All @@ -73,7 +73,7 @@
(def custom-table-viewer
(add-child-viewers v/table-viewer
[(assoc v/table-head-viewer :transform-fn (v/update-val (partial map (comp (partial str "Column: ") str/capitalize name))))
(assoc v/table-missing-viewer :render-fn '(fn [x] (v/html [:span.red "N/A"])))]))
(assoc v/table-missing-viewer :render-fn '(fn [x] [:span.red "N/A"]))]))

(clerk/with-viewer custom-table-viewer
{:col/a [1 2 3 4] :col/b [1 2 3] :col/c [1 2 3]})
Expand Down
4 changes: 0 additions & 4 deletions src/nextjournal/clerk/sci_env.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@
"@nextjournal/lang-clojure" lang-clojure
"framer-motion" framer-motion
"react" react}
:aliases {'j 'applied-science.js-interop
'reagent 'reagent.core
'v 'nextjournal.clerk.viewer
'p 'nextjournal.clerk.parser}
:namespaces (merge {'nextjournal.clerk.viewer viewer-namespace
'clojure.core {'read-string read-string
'implements? (sci/copy-var implements?* core-ns)}}
Expand Down

0 comments on commit 1b5bf5e

Please sign in to comment.