nextjournal

mkvlr 2021-12-08T13:43:53.181900Z

weโ€™d like to cut a Clerk release, would appreciate it if folks could take io.github.nextjournal/clerk {:git/sha "aa6ed09880316d15b0d3c76ae50505d1978e4008"} for a spin and let me know if thereโ€™s anything we missed. Preliminary changelog is at https://github.com/nextjournal/clerk/blob/main/CHANGELOG.md#unreleased

mkvlr 2021-12-08T13:46:57.184400Z

@carsten.behring @daslu I believe both of you asked for being able to let Clerk serve arbitrary things. In the latest main a viewer can provide a fetch-fn to enable this by returning a map with :nextjournal/content-type and :nextjournal/value keys, see https://github.com/nextjournal/clerk/blob/main/notebooks/viewers/image.clj for an example. This will also inline the blob for the static builds. Curious to hear if that serves your needs.

Daniel Slutsky 2021-12-08T13:54:00.184800Z

Wonderful, many thanks, @mkvlr. I hope to look into that soon.

๐Ÿ™ 1
2021-12-08T15:34:00.185100Z

Thanks. Looking at the example seems to say, that it satisfies the "general need", of being able to load / generate byte arrays in image format and show them on the web. This would allow Clerk , for example, to render plots where the plotting library can only generate "desktop" graphics, typically PNGs.

2021-12-08T15:35:20.185300Z

I was a bit wondering, if there is a limitation that I need to set a global viewer for any type of "bytes" (= same viewer for all byte arrays).

mkvlr 2021-12-08T15:35:57.185500Z

no, you could also set a viewer e.g. on java.io.File and match on an extension

mkvlr 2021-12-08T15:36:03.185700Z

the bytes? is just an example

mkvlr 2021-12-08T15:36:09.185900Z

and probably not a super good one

mkvlr 2021-12-08T15:36:42.186100Z

think we might want to add the file viewers as defaults

2021-12-08T15:38:39.186300Z

Is there documentation which function a pred? can be ?

2021-12-08T15:39:02.186500Z

so what exactly gets passed to the function specified in :pred?

2021-12-08T15:39:16.186700Z

The "evaluation result" ?

2021-12-08T15:46:41.186900Z

I just looked at teh code, and relaized how

clerk/set-viewers!
clerk/with-viewer
clerk/with-viewers
So I have ultimate control on the "active viewers" That's fine. So pred?: true would works as well, if i am inside a with-viewer block. That was my doubt.

2021-12-08T15:46:48.187100Z

I will try it a bit.

2021-12-08T16:45:29.188700Z

it is supposed to work with with-viewers ?

2021-12-08T16:45:39.188900Z

This fails:

(clerk/with-viewers [{:pred bytes?
                      :fetch-fn (fn [_ bytes] {:nextjournal/content-type "image/png"}
                                              :nextjournal/value bytes)
                      :render-fn (fn [blob] (v/html [:img {:src (v/url-for blob)}]))}]

  (.. (HttpClient/newHttpClient)
      (send (.build (HttpRequest/newBuilder (URI. "")))
            (HttpResponse$BodyHandlers/ofByteArray)) body))

2021-12-08T16:46:07.189100Z

2021-12-08T16:55:34.189800Z

without pred?

2021-12-08T16:55:45.190Z

with-viewers works

2021-12-08T16:56:07.190200Z

2021-12-08T20:36:03.190800Z

The issue went away. The image supports is working, thanks @mkvlr

mkvlr 2021-12-08T20:38:11.191200Z

@carsten.behring good to hear. Btw you should also be able to register the predicate on the file and use a :transform-fn to convert it to bytes

mkvlr 2021-12-08T20:39:35.191400Z

and you can use clerk/set-viewers!

mkvlr 2021-12-08T20:40:12.191600Z

then creating a file object is enough to display it as an image

mkvlr 2021-12-08T20:53:08.191800Z

mkvlr 2021-12-08T20:53:20.192200Z

;; # ๐Ÿž Customizing Fetch
;; Showing how to use a custom `fetch-fn` with a `content-type` to let Clerk serve arbitrary things, in this case a PNG image.
(ns ^:nextjournal.clerk/no-cache image
  (:require [ :as io]
            [nextjournal.clerk :as clerk])
  (:import (java.nio.file Files)))

;; We set a custom viewer for `java.io.File` that includes a `:fetch-fn`, returning a wrapped value with a `:nextjournal/content-type` key set.
(clerk/set-viewers! [{:pred #(instance? File %)
                      :fetch-fn (fn [_ file] {:nextjournal/content-type "image/png"
                                              :nextjournal/value (Files/readAllBytes (.toPath file))})
                      :render-fn (fn [blob] (v/html [:img {:src (v/url-for blob)}]))}])


(io/file "/Users/mk/Downloads/James_Clerk_Maxwell.png")

mkvlr 2021-12-08T20:54:19.192400Z

this is just for pngs but could check the extension or sniff the content type to extend this

2021-12-08T15:56:27.188100Z

Maybe something for the docu: Avoid a namespace like:

(ns wordclouds
  (:require [nextjournal.clerk :as clerk]))


(clerk/show! "notebooks/wordclouds.clj")

โœ… 1
mkvlr 2021-12-08T16:49:28.189600Z

yep, been there before as well ๐Ÿ˜ผ

2021-12-08T15:56:52.188500Z

endless loop ... while evaluating last line

2021-12-08T21:06:17.193400Z

I was looking at the visibility control in the new version of Clerk. Nice !

2021-12-08T21:10:12.197500Z

This allows now precise control what to hide and what to show. Nevertheless I found myself quite a lot of times using clerk/hide-result It is maybe possible to use the hide-result as metadata and have an option to hide results for the whole ns and switch it on for a few forms only ? A good use case for this is "documentation of Clojure libraries", where maybe a lot of code / results is hidden.

mkvlr 2021-12-08T21:27:14.201100Z

this use case is actually something we want to support better

mkvlr 2021-12-08T21:29:41.201300Z

but want to explore the minimum thing needed in Clerk to support this first

2021-12-08T22:44:39.202700Z

I see 4 use cases: 1. I know I do a data science notebook for me, so I want to see all code and all results 2. I know I want to do docu, so I want to show some (most code, most results) 3. I do a data science notebook , and a static rendering I want to hide all code (for my manager) (= override the visibility settings) 4. I want to do a specially nice output. So I need to be sure to be able to disable all ugly output/code The fact that Clerk renders as well "one lines" on def / nil makes this control more important

2021-12-08T21:11:56.197900Z

So the same as for "hide and "fold"

2021-12-08T21:13:05.198100Z

To get this result:

2021-12-08T21:13:29.198700Z

I need to use "hide-result" a lot

2021-12-08T21:14:24.199400Z

This is maybe not for the typical data science notebook use case, I agree.

2021-12-08T22:38:04.202100Z

I changed a bit the code and it seemed to work very nice. I made a PR to replace the hide-result fn with meta data :hide-result :show-result https://github.com/nextjournal/clerk/pull/24

2021-12-08T21:19:28.200900Z

An other idea for a feature: Allow "copy" from a code cell with one keypress. And eventually as well for whole rendered namespace.

mkvlr 2021-12-08T21:30:47.201500Z

copy the result you mean?

2021-12-08T22:33:48.201700Z

No the code,

2021-12-08T22:34:28.201900Z

just a simple button, which copies the code from a code cell, without need to "mark" by mouse