clerk

Sean Swezey 2023-10-06T08:46:03.585469Z

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.

Andrea 2023-10-06T12:42:37.331169Z

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]])

Sean Swezey 2023-10-06T12:44:42.597509Z

thank you!

Andrea 2023-10-06T12:44:43.230459Z

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

Sean Swezey 2023-10-06T12:46:02.174329Z

I appreciate it, I couldn’t figure out that connecting piece from the docs

👍 1
Sean Swezey 2023-10-06T13:15:54.806769Z

So interestingly, this doesn’t work when I want the number formatter to be used within the table. Attaching a snippet

Sean Swezey 2023-10-06T13:20:52.756139Z

Sean Swezey 2023-10-06T13:23:09.294689Z

The number viewer must first be attached to a table viewer

Andrea 2023-10-06T13:26:46.290019Z

that’s very odd, there should be no need to add the number viewer to the inner table viewers

Andrea 2023-10-06T13:27:24.865069Z

I guess that has something to do with named viewers

Andrea 2023-10-06T13:28:29.806439Z

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]])

Sean Swezey 2023-10-06T13:33:24.292959Z

Yep, dissocing the name allowed it to work

Andrea 2023-10-06T13:33:47.739599Z

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?

Sean Swezey 2023-10-06T13:37:41.664839Z

Either way, it’s an amazing tool and I want to thank yall for creating it!

mkvlr 2023-10-06T13:38:24.416129Z

add-viewers! now does in-place positional replacement to make it easier to override them

mkvlr 2023-10-06T13:39:05.065999Z

there’s also a first-class :add-viewers attribute on the table-viewer, also makes customization easier

mkvlr 2023-10-06T13:39:24.283709Z

see the Improve viewer customization section in https://github.com/nextjournal/clerk/blob/main/CHANGELOG.md#015957-2023-09-28

mkvlr 2023-10-06T13:39:38.322489Z

still ned to do a pass to clarify this in the docs

Sean Swezey 2023-10-06T13:43:29.894029Z

I saw that originally, but wasn’t sure which was the preferred/recommended way to do

Andrea 2023-10-06T13:44:27.771069Z

> 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

mkvlr 2023-10-06T13:45:39.764259Z

(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]])

mkvlr 2023-10-06T13:45:57.906429Z

this is the recommended way, no need to dissoc anything

mkvlr 2023-10-06T13:47:20.926119Z

but use :add-viewers instead of :nextjournal/viewers because the former takes precendence

Sean Swezey 2023-10-06T13:47:37.279649Z

Thanks!

mkvlr 2023-10-06T13:47:46.035379Z

let me know if that makes sense

Sean Swezey 2023-10-06T13:48:31.965899Z

Yep, makes sense. The add-viewers! changes the default viewers correct?

mkvlr 2023-10-06T13:49:03.525399Z

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

mkvlr 2023-10-06T13:49:13.527089Z

yes, can also do it just locally

mkvlr 2023-10-06T13:49:30.249989Z

add-viewers! changes the defaults for that namespace

1
mkvlr 2023-10-06T13:51:03.715389Z

@andrea712 let’s go over this and document it next week