How do I use the ::clerk/viewers key in (clerk/table {::clerk/viewers ???} {…}) ? I’m trying to use a custom number formatter within the table. I’ve tried putting a modified viewer/table-viewer , a seq of it, a seq of viewer/table-viewers, only my modified viewer/number-viewer, and I can’t get anything to render. Most errors mention it can’t find a viewer with key ::viewer/table-viewer including if it’s the only item in there. I wasn’t able to find any examples within the Clerk demos.
hi @sean296 both these example work with the latest clerk
(clerk/with-viewers
(clerk/add-viewers [{:pred number? :render-fn '(fn [_ _] [:em "num"])}])
(clerk/table [[1] [2]]))
(clerk/table
{::clerk/viewers (clerk/add-viewers [{:pred number? :render-fn '(fn [_ _] [:em "num"])}])}
[[1] [2]])
thank you!
the point is with-viewers (and the associated clerk/viewers option) actually do specify all the available viewers, so clerk/dd-viewers prepends your custom ones to the list of the defaults
I appreciate it, I couldn’t figure out that connecting piece from the docs
So interestingly, this doesn’t work when I want the number formatter to be used within the table. Attaching a snippet
The number viewer must first be attached to a table viewer
that’s very odd, there should be no need to add the number viewer to the inner table viewers
I guess that has something to do with named viewers
in fact this works again:
(def n-viewer
(dissoc (assoc viewer/number-viewer :render-fn '(fn [_ _] [:em "num"]))
:name))
(clerk/table
{::clerk/viewers (clerk/add-viewers [n-viewer])}
[[1] [2]])
Yep, dissocing the name allowed it to work
that must be something related to how add-viewers work in wrt named viewers, but I don’t recall why we did this: https://github.com/nextjournal/clerk/blob/fc6df7186bfea2571fff2dd790d0ad63f8d6295f/src/nextjournal/clerk/viewer.cljc#L666-L670 cc @mkvlr?
Either way, it’s an amazing tool and I want to thank yall for creating it!
add-viewers! now does in-place positional replacement to make it easier to override them
there’s also a first-class :add-viewers attribute on the table-viewer, also makes customization easier
see the Improve viewer customization section in https://github.com/nextjournal/clerk/blob/main/CHANGELOG.md#015957-2023-09-28
still ned to do a pass to clarify this in the docs
I saw that originally, but wasn’t sure which was the preferred/recommended way to do
> Anonymous viewers (without a :name) or new named viewers will be prepended to the viewer stack
I see, but having to dissoc the name of an existing viewer to have it take precedence sounds a bit odd
(ns scratch
(:require [nextjournal.clerk.viewer :as viewer]
[nextjournal.clerk :as clerk]))
(def my-number-viewer
(assoc viewer/number-viewer :render-fn '(fn [_ _] [:em "num"])))
(clerk/add-viewers! [(update viewer/table-viewer :add-viewers clerk/add-viewers [my-number-viewer])])
(clerk/table [[1] [2]])this is the recommended way, no need to dissoc anything
but use :add-viewers instead of :nextjournal/viewers because the former takes precendence
Thanks!
let me know if that makes sense
Yep, makes sense. The add-viewers! changes the default viewers correct?
or rather, if you used :nextjournal/viewers but left the :add-viewers on the table-viewer as-is, the table viewer would still use its original number viewer, so you woudln’t see the change
yes, can also do it just locally
add-viewers! changes the defaults for that namespace
@andrea712 let’s go over this and document it next week