Skip to content

Commit 00c30b1

Browse files
mkphilippamarkovics
authored andcommitted
Prepare for per-cell progress reporting
1 parent 0e42cb4 commit 00c30b1

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

notebooks/exec_status.clj

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
;; # 💈 Execution Status
22
(ns exec-status
33
{:nextjournal.clerk/toc true}
4-
(:require [nextjournal.clerk :as clerk]))
4+
(:require [nextjournal.clerk :as clerk]
5+
[nextjournal.clerk.webserver :as webserver]))
56

67
;; To see what's going on while waiting for a long-running
78
;; computation, Clerk will now show an execution status bar on the
@@ -25,20 +26,37 @@
2526

2627
{:progress 0.15 :status "Analyzing…"}
2728

28-
{:progress 0.55 :status "Evaluating…"}
29+
{:progress 0.55 :cell-progress 0.34 :status "Evaluating…"}
2930

3031
{:progress 0.95 :status "Presenting…"}
3132

33+
(defn set-cell-progress! [progress]
34+
(swap! webserver/!doc (fn [doc] (if-let [status (-> doc meta :status)]
35+
(let [status+progress (assoc status :cell-progress progress)]
36+
(when-let [send-future (-> doc meta ::webserver/!send-status-future)]
37+
(future-cancel send-future))
38+
(webserver/broadcast-status! status+progress)
39+
(-> doc
40+
(vary-meta dissoc ::!send-status-future)
41+
(vary-meta assoc :status status+progress)))
42+
doc))))
43+
3244
(defonce !rand
3345
(atom 0))
3446

3547
^::clerk/no-cache (reset! !rand (rand-int 100))
3648
(Thread/sleep (+ 2000 @!rand))
3749

3850
(def sleepy-cell
39-
(Thread/sleep (+ 2001 @!rand)))
51+
(let [total (+ 2001 @!rand)]
52+
(doseq [i (range total)]
53+
(do
54+
(Thread/sleep 10)
55+
(set-cell-progress! (/ i (float total)))))))
4056

4157
(Thread/sleep (+ 2002 @!rand))
4258

4359
(def sleepy-cell-2
4460
(Thread/sleep (+ 2003 @!rand)))
61+
62+

src/nextjournal/clerk/render.cljs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,16 @@
9898

9999
(defonce !eval-counter (r/atom 0))
100100

101-
(defn exec-status [{:keys [progress status]}]
102-
[:div.w-full.bg-purple-200.dark:bg-purple-900.rounded.z-20 {:class "h-0.5"}
103-
[:div.bg-purple-600.dark:bg-purple-400 {:class "h-0.5" :style {:width (str (* progress 100) "%")}}]
104-
[:div.absolute.text-purple-600.dark:text-white.text-xs.font-sans.ml-1.bg-white.dark:bg-purple-900.rounded-full.shadow.z-20.font-bold.px-2.border.border-slate-300.dark:border-purple-400
105-
{:style {:font-size "0.5rem"} :class "left-[35px] md:left-0 mt-[7px] md:mt-1"}
106-
status]])
101+
(defn exec-status [{:keys [progress cell-progress status]}]
102+
[:<>
103+
[:div.w-full.bg-purple-200.dark:bg-purple-900.rounded.z-20 {:class "h-[2px]"}
104+
[:div.bg-purple-600.dark:bg-purple-400 {:class "h-[2px]" :style {:width (str (* progress 100) "%")}}]
105+
[:div.absolute.text-purple-600.dark:text-white.text-xs.font-sans.ml-1.bg-white.dark:bg-purple-900.rounded-full.shadow.z-20.font-bold.px-2.border.border-slate-300.dark:border-purple-400
106+
{:style {:font-size "0.5rem"} :class "left-[35px] md:left-0 mt-[7px] md:mt-1"}
107+
status]]
108+
(when cell-progress
109+
[:div.w-full.bg-sky-100.dark:bg-purple-900.rounded.z-20 {:class "h-[2px] mt-[0.5px]"}
110+
[:div.bg-sky-500.dark:bg-purple-400 {:class "h-[2px]" :style {:width (str (* cell-progress 100) "%")}}]])])5
107111

108112
(defn connection-status [status]
109113
[:div.absolute.text-red-600.dark:text-white.text-xs.font-sans.ml-1.bg-white.dark:bg-red-800.rounded-full.shadow.z-20.font-bold.px-2.border.border-red-400

src/nextjournal/clerk/webserver.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@
276276

277277
(defn set-status! [status]
278278
(swap! !doc (fn [doc] (-> (or doc (help-doc))
279-
(vary-meta assoc :status status)
280-
(vary-meta update ::!send-status-future broadcast-status-debounced! status)))))
279+
(vary-meta assoc :status status)
280+
(vary-meta update ::!send-status-future broadcast-status-debounced! status)))))
281281

282282
#_(clojure.java.browse/browse-url "http://localhost:7777")
283283

0 commit comments

Comments
 (0)