Fork me on GitHub
#clerk
<
2023-01-29
>
match3720:01:19

I had a hard time trying to get the beholder/watch working with a file content (original thread: https://clojurians.slack.com/archives/C035GRLJEP8/p1674095093522629 Thanks @jackrusher’s suggestions in original thread) . So I'm just trying to have the mermaid viewer to show a diagram from a file. The file can be updated externally. I tried both call beholder/watch from inside the notebook or from repl, neither worked form me. (require '[nextjournal.beholder :as beholder]) (beholder/watch #(state/reload!) (state/data-path "data.txt"))

(ns my-notebook
    {:nextjournal.clerk/visibility {:code :fold}}
    (:require [nextjournal.clerk :as clerk]
      [state]
      [clojure.string :as str]
      [nextjournal.clerk.viewer :as v])
    )


(def mermaid-viewer
  {:transform-fn clerk/mark-presented
   :render-fn '(fn [value]
                   (v/html
                     (when value
                           [v/with-d3-require {:package ["[email protected]/dist/mermaid.js"]}
                            (fn [mermaid]
                                [:div {:ref (fn [el] (when el
                                                           (.render mermaid (str (gensym)) value #(set! (.-innerHTML el) %))))}])])))})

(state/reload!)
(when-not (str/blank? @state/chart)
          (clerk/with-viewer mermaid-viewer @state/chart)
      )
(ns state
  (:require
      [nextjournal.clerk :as clerk]
)
(def data-dir "/home/demo/myData")
(defn data-path [fName]
      (str data-dir "/" fName))
(defonce chart (atom ""))
(defn reload! []
      (println "in reload!")
      (reset! chart (slurp (data-path "data.txt")))
      ;(clerk/show! 'my-notebook)
      (clerk/recompute!)
      )

Andrea14:02:52

Hi @U3N4R4TLK this is working for me (with beholder watching a path from within the same notebook), not sure this fits your scenario

;; # Mermaid with watcher
(ns scratch.mermaid
  (:require [nextjournal.clerk :as clerk]
            [nextjournal.beholder :as beholder]))

(def mermaid-viewer
  {:transform-fn clerk/mark-presented
   :render-fn '(fn [value]
                 (when value
                   [v/with-d3-require {:package ["[email protected]/dist/mermaid.js"]}
                    (fn [mermaid]
                      [:div {:ref (fn [el]
                                    (when el
                                      (.render mermaid (str (gensym)) value #(set! (.-innerHTML el) %))))}])]))})

^::clerk/no-cache
(clerk/with-viewer mermaid-viewer
  (slurp "notebooks/scratch/data/mermaid.mm"))

(comment
  (def watcher
    (beholder/watch (fn [e] (clerk/recompute!))
                    "notebooks/scratch/data"))
  (beholder/stop watcher))
let me know if that works for you as well