Tap support playground
We have recently added Clojure tap support to Nextjournal. The most simple case is as follows:
(tap> :hello_tap)
(println "hello_print")
Values send via tap are displayed above the result and below the stdout of a cell. One can now use our viewer API to send asynchronous updates during an execution.
(tap> {:nextjournal/viewer :html
:nextjournal/tap-id :foo
:nextjournal.viewer/value
(str "<pre>" "Progress bar:" (apply str (repeat 3 "π")) "</pre>")})
Progress bar:πππππ
The optional :nextjournal/tap-id
identifies a tap output which one can update also in other cells. Specifying a tap-id
is like a database upsert operation. If the id exists the tap output gets updated, if it doesn't exist the new output gets appended to the tap section of the current cell. With no tap-id
specified the result gets pushed to the tap section of the executing cell.
(tap> {:nextjournal/viewer :html
:nextjournal/tap-id :foo
:nextjournal.viewer/value
(str "<pre>" "Progress bar:" (apply str (repeat 5 "π")) "</pre>")})
One use case is to implement a progress bar.
(for [i (range 10)]
(do (Thread/sleep 1000)
(tap> {:nextjournal/viewer :html
:nextjournal/tap-id :bar
:nextjournal.viewer/value
(str "<pre>" "Progress bar: "
(apply str (repeat (inc i) "π")) "</pre>")})))
Progress bar: ππππππππππππππππππππ
The tap-id
can also be specified via metadata.
(tap> {:nextjournal/tap-id :bar}
{:nextjournal/viewer :html
:nextjournal.viewer/value
(str "<pre>" "Progress bar: "
(apply str (repeat 20 "π")) "</pre>")})
Also check out playing Tic Tac Toe on Nextjournal!