Fork me on GitHub
#nextjournal
<
2021-12-08
>
mkvlr13:12:53

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

mkvlr13:12:57

@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 Slutsky13:12:00

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

🙏 1
Carsten Behring15:12:00

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.

Carsten Behring15:12:20

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

mkvlr15:12:57

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

mkvlr15:12:03

the bytes? is just an example

mkvlr15:12:09

and probably not a super good one

mkvlr15:12:42

think we might want to add the file viewers as defaults

Carsten Behring15:12:39

Is there documentation which function a pred? can be ?

Carsten Behring15:12:02

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

Carsten Behring15:12:16

The "evaluation result" ?

Carsten Behring15:12:41

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.

Carsten Behring15:12:48

I will try it a bit.

Carsten Behring16:12:29

it is supposed to work with with-viewers ?

Carsten Behring16:12:39

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

Carsten Behring16:12:45

with-viewers works

Carsten Behring20:12:03

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

mkvlr20:12:11

@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

mkvlr20:12:35

and you can use clerk/set-viewers!

mkvlr20:12:12

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

mkvlr20:12:20

;; # 🏞 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")

mkvlr20:12:19

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

Carsten Behring15:12:27

Maybe something for the docu: Avoid a namespace like:

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


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

1
mkvlr16:12:28

yep, been there before as well 😼

Carsten Behring15:12:52

endless loop ... while evaluating last line

Carsten Behring21:12:17

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

Carsten Behring21:12:12

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.

mkvlr21:12:14

this use case is actually something we want to support better

mkvlr21:12:41

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

Carsten Behring22:12:39

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

Carsten Behring21:12:56

So the same as for "hide and "fold"

Carsten Behring21:12:05

To get this result:

Carsten Behring21:12:29

I need to use "hide-result" a lot

Carsten Behring21:12:24

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

Carsten Behring22:12:04

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

Carsten Behring21:12:28

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

mkvlr21:12:47

copy the result you mean?

Carsten Behring22:12:28

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