Skip to content

Fix clerk stats viewer npe #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions notebooks/missing_id_repro.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
;; # 🐞Missing ID Repro
(ns missing-id-repro
{:nextjournal.clerk/no-cache true}
(:require [nextjournal.clerk :as clerk]))

(def pred-viewer
{:pred :a/b
:transform-fn (fn [{:as wv :keys [id path]}]
(prn :matching id :path path)
#_ (when-not id (throw (ex-info "No ID" {:wrapped-value wv})))
wv)
:render-fn '(fn [_ _] [:em "test"])})

(clerk/add-viewers! [pred-viewer])

;; cell-id is only present for values matching top-level forms, all matching values
;; at deeper paths won't have an id.

{:a/b 1
:children [{:a/b 2}]}

(comment
(-> (ex-data *e) :wrapped-value :path))
25 changes: 25 additions & 0 deletions notebooks/nested_tabular_data.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(ns nested-tabular-data
{:nextjournal.clerk/visibility {:code :hide :result :hide}
:nextjournal.clerk/no-cache true}
(:require [nextjournal.clerk :as clerk]
[nextjournal.clerk-table-stats :as clerk-table-stats]))

#_(clerk/add-viewers! [])
(clerk/add-viewers! [clerk-table-stats/view-as-table-viewer
clerk-table-stats/viewer])

{:nextjournal.clerk/visibility {:code :hide :result :show}}

[{:category :bang
:value 11}
{:category :barx
:value 20}
{:category :bug
:value 22
:children [{:category :bang :value 11}
{:category :barx :value 20}]}]

[(sorted-map "one" 1 "two" 2)
(sorted-map "one" 1 "two" 2)
(sorted-map "one" 1 "two" 2)
(sorted-map "one" 1 "two" 2)]
28 changes: 28 additions & 0 deletions src/nextjournal/clerk_table_stats.clj
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,34 @@
(nextjournal.clerk.render/inspect-presented opts cell)]))
row)))})

(defn tabular? [xs]
(and (seqable? xs)
(sequential? xs)
(let [sample (take 100 xs)]
;; TODO: do we also need some normalization to be successful?
;; normalize-seq-of-map currently always returns truthy on a seq of maps
(every? map? sample))))

{::clerk/render-opts {:group-headers true}
::clerk/page-size 5
::clerk/width :full}

(def view-as-table-viewer
;; TODO: decide if we want to opt out of matching only top-level forms
{:pred {:wrapped (every-pred (comp #{1} count (viewer/get-safe :path))
(comp tabular? viewer/->value))}
:transform-fn (partial clerk/with-viewer 'nextjournal.clerk.viewer/table-viewer)})

;; usage with default global options
(comment
(update view-as-table-viewer
:transform-fn
(fn [f]
(fn [wv]
(f {::clerk/render-opts {:group-headers true}
::clerk/page-size 5
::clerk/width :full} wv)))))

(def viewer
(assoc viewer/table-viewer
:transform-fn
Expand Down