nextjournal

genmeblog 2022-02-01T16:00:35.555909Z

Hi. I've seen TOC is possible (after PR56) but how to make it visible?

mkvlr 2022-02-01T16:31:16.268549Z

@andrea712 worked on that and we decided to not show it yet as we want to add more styling. Not sure if it's already possible to show it.

genmeblog 2022-02-01T16:32:03.804069Z

ok! It'll be a great feature. So waiting.

🙏 2
genmeblog 2022-02-01T16:23:25.341119Z

and (possibly asked that before): how to hide result? Having comment block with ^{:nextjournal.clerk/visibility #{:hide}}, the block is hidden but result nil is visible.

mkvlr 2022-02-01T16:29:49.845739Z

Need to also set ^{:nextjournal.clerk/viewer :hide-result}. See https://github.com/nextjournal/clerk/pull/59

genmeblog 2022-02-01T16:31:07.530379Z

ah! thanks! I'm now pretty sure I've seen that before.

mkvlr 2022-02-01T16:32:13.027039Z

you might also have seen https://github.com/nextjournal/clerk/pull/24

2022-02-01T22:50:26.716249Z

I wanted to +1 the approach suggested in https://clojurians.slack.com/archives/C01GH5EN7JA/p1638997812197500 that resulted in https://github.com/nextjournal/clerk/pull/24. The use case I have is wanting to show code in a notebook but 1. Not show the result 2. Not complicate the code I want people to read with having to do (clerk/hide-result (the-code-I-actually-want-to-show)) I like having the ^{::clerk/visibility :hide} metadata because I can control what code I do not want the reader to have to see. I would also like to have a ^{::clerk/visibility :hide-result} option as that would let me show the reader the code, without the result (which may be distracting) and without having to distract from the code with (clerk/hide-result...

2022-02-01T22:53:08.036369Z

That being said, Clerk is awesome and I really appreciate everyone’s work on it! I used to be a huge user of Python and Jupyter notebooks but using Clojure more now, I greatly prefer the Clojure REPL development experience while still being able to do literate programming with Clerk.

❤️ 2
2022-02-04T23:33:33.913629Z

Thanks @mkvlr, I had not seen that updated but just updated the version of clerk I was using and my .clj file and it works great!

2022-02-04T23:34:35.704799Z

Is there a configuration that can hide the ^{…} metadata annotation? For example, I want to talk about this bit of code but would like to hide the metadata used by Clerk. It is in my .clj and gets rendered as

^{::clerk/viewer :hide-result}
(def scratch-db-uri "asami:")

mkvlr 2022-02-05T16:10:09.216069Z

not yet but it’s on the roadmap

mkvlr 2022-02-02T08:43:20.281459Z

@jhemann did you see that ^{::clerk/viewer :hide-result} is supported as of https://github.com/nextjournal/clerk/pull/59? Controlling both result and code cell visibility with a single keyword leads to a combinatorial state explosion that I think is better avoided.

genmeblog 2022-02-01T17:38:17.194679Z

@mkvlr I'll be spamming with some specific issues I have 🙂

mkvlr 2022-02-01T17:38:46.073899Z

please do!

🙏 1
genmeblog 2022-02-01T17:39:14.437819Z

First one: when I make custom viewer, how can I use external functions in :render-fn ?

genmeblog 2022-02-01T17:39:55.034559Z

The example is:

genmeblog 2022-02-01T17:40:00.585009Z

(defn make-color-div
  [c]
  (clerk/html [:div {:style {:background-color "#aaaaaa"}} c]))
(clerk/set-viewers! [{:name :color :render-fn `make-color-div}])

genmeblog 2022-02-01T17:40:42.637899Z

Which doesn't work and produces:

genmeblog 2022-02-01T17:40:45.632659Z

genmeblog 2022-02-01T17:41:47.323769Z

while (clerk/set-viewers! [{:name :color :render-fn '#(v/html [:div {:style {:background-color "#aaaaaa"}} %])}]) works

genmeblog 2022-02-01T17:42:42.286679Z

making render-fn without the access to other namespaces is very limiting

mkvlr 2022-02-01T17:44:20.073989Z

do you already have code you’d like to use there? Is it ClojureScript?

mkvlr 2022-02-01T17:46:06.835739Z

asking because you can extend the environment and hence what namespaces you can use in :render-fn but that means you also have to do your own ClojureScript compilation

mkvlr 2022-02-01T17:47:13.965279Z

this adds some complexity, which may or may not be a problem for you. If you already have a cljs pipeline on this project it’s pretty simple

genmeblog 2022-02-01T17:48:20.609849Z

Hmmm... I want to avoid this path. Maybe I should use transform-fn?

genmeblog 2022-02-01T17:48:35.451679Z

Need to experiment more

mkvlr 2022-02-01T17:49:02.675679Z

depends on what you want to generate, hiccup in the end?

mkvlr 2022-02-01T17:50:19.847649Z

in that case you should be able to define a viewer with only a :transform-fn and return hiccup tagged with clerk/html

mkvlr 2022-02-01T17:50:41.779539Z

and you can use all code on the JVM classpath in that case

genmeblog 2022-02-01T17:50:45.836879Z

Yeah! Should work!

mkvlr 2022-02-01T17:53:56.142359Z

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

(clerk/set-viewers! [{:pred vector? :transform-fn clerk/html}])

[:h1 "Hi Hiccup 👋"]

mkvlr 2022-02-01T17:54:40.014889Z

along those lines

mkvlr 2022-02-01T17:55:10.479999Z

:transform-fn can of course be more complex, but should call clerk/html eventually if you want to render hiccup

genmeblog 2022-02-01T17:56:09.750279Z

Got it. Still building intuition about data flow.

👍 1
mkvlr 2022-02-01T17:58:27.597769Z

me too 🙃

😀 1
genmeblog 2022-02-01T19:35:09.152209Z

Ok. Following notebook hangs everything, eats all CPU, firefox claims that page is slow (and eats tons of memory). Any idea what happens?

genmeblog 2022-02-01T19:35:22.886819Z

^{:nextjournal.clerk/visibility :hide-ns}
(ns notebooks.color
  (:require [nextjournal.clerk :as clerk]
            [nextjournal.clerk.config :as config]
            [clojure2d.color :as c]))

(defn color-spans [c]
  (let [c (:nextjournal/value c)
        str-color (if (< (c/luma c) 128.0) "white" "black")
        n (c/format-hex c)]
    [:span {:style (str "background-color:" n ";color:" str-color ";")} (str "[" n "] " c)]))

(clerk/set-viewers! [{:name :color :render-fn (quote v/html)
                      :transform-fn color-spans}])

(color-spans {:nextjournal/value :red})
;; => [:span {:style "background-color:#ff0000;color:white;"} "[#ff0000] :red"]

#_(swap! config/!resource->url assoc "/css/viewer.css" "../docs/static/static.css")

;; # Color

(clerk/with-viewer :color 11)

;; ## Sub

^{::clerk/viewer :color}
(+ 1 2)

^{:nextjournal.clerk/visibility :hide
  :nextjournal.clerk/viewer :hide-result}
(comment
  (require '[nextjournal.clerk :as clerk])
  (clerk/serve! {:browse? true :watch-paths ["notebooks"]})
  (clerk/show! "notebooks/color.clj")
  (clerk/halt!))

👀 1
genmeblog 2022-02-01T20:04:15.220049Z

@mkvlr let's continue here (don't to spam main channel)

genmeblog 2022-02-01T20:04:30.210309Z

:transform-fn (comp clerk/html color-spans) doesn't change anything

mkvlr 2022-02-01T20:25:13.289849Z

^{:nextjournal.clerk/visibility :hide-ns}
(ns ^:nextjournal.clerk/no-cache notebooks.color
  (:require [nextjournal.clerk :as clerk]
            [nextjournal.clerk.viewer :as v]
            [clojure2d.color :as c]))

(defn color-spans [c]
  (let [c (v/value c)
        str-color (if (< (c/luma c) 128.0) "white" "black")
        hex (c/format-hex c)]
    (v/html [:span {:style {:background-color hex :color str-color}}
             (str "[" hex "] " c)])))

(clerk/set-viewers! [{:name :color :pred #{:red 3 11} :transform-fn color-spans}])

:red

;; # Color
11

;; ## Sub
(+ 1 2)

#_(clerk/serve! {})

mkvlr 2022-02-01T20:25:15.014949Z

try this

mkvlr 2022-02-01T20:25:47.200779Z

the hiccup flavor we’re using (reagent) wants styles as maps

mkvlr 2022-02-01T20:26:20.575709Z

I’d recommend working from a very concrete impl first to implement a viewer

mkvlr 2022-02-01T20:27:53.101679Z

whoops, just fixed an error in the above

mkvlr 2022-02-01T20:28:10.579789Z

now looks like this

mkvlr 2022-02-01T20:28:22.859899Z

gtg now, will check back tomorrow

genmeblog 2022-02-01T20:29:50.608839Z

thanks a lot!

genmeblog 2022-02-01T21:05:18.484119Z

above works indeed, thanks for a hiccup hint. However, selecting viewer with metadata or using with-viewer produces unusable viewer

genmeblog 2022-02-01T21:10:26.913939Z

> I’d recommend working from a very concrete impl first to implement a viewer

genmeblog 2022-02-01T21:10:39.811639Z

^ what do you mean by that?

genmeblog 2022-02-01T23:01:02.859049Z

Ok, eventually I decided not to use custom viewer at all. Just created a function which returns html view. No need to use meta tags etc.

genmeblog 2022-02-01T23:01:46.146719Z

genmeblog 2022-02-01T23:02:26.617039Z

the only issue is that CSS is inlined

mkvlr 2022-02-02T08:40:00.191619Z

re working from a concrete impl, I meant pretty much doing what you did above: start with a let statement and get the hiccup to render for concrete values, only then move things into a viewer. But you’re right that for your use case the viewers aren’t needed as you don’t seem to require the recursive nature of viewers.

👍 1
mkvlr 2022-02-02T08:40:33.727709Z

and great to see you got things working!

genmeblog 2022-02-02T09:32:30.851719Z

So far so good 🙂 I'm trying to prepare better doc for clojure2d.color namespace and above attempt was crucial to make it happen.

❤️ 1
genmeblog 2022-02-01T19:38:56.364819Z

It hangs here (after interrupting in repl):

1. Unhandled java.lang.InterruptedException
   (No message)

               Object.java:   -2  java.lang.Object/wait
               Object.java:  338  java.lang.Object/wait
          ProcessImpl.java:  434  java.lang.ProcessImpl/waitFor
                 shell.clj:  127  clojure.java.shell/sh
                browse.clj:   74  clojure.java.browse/browse-url
                browse.clj:   66  clojure.java.browse/browse-url
                 clerk.clj:  306  nextjournal.clerk/serve!
                 clerk.clj:  284  nextjournal.clerk/serve!
                      REPL:   34  notebooks.color/eval30547

genmeblog 2022-02-02T10:33:58.712869Z

@mkvlr regarding this issue. I think it might be connected to my setup which is WSL2 on Windows 11 with wslg (xserver). I tried to change browsers and google-chrome hangs everything during serve! while firefox hangs during show!

genmeblog 2022-02-02T10:35:28.698829Z

firefox path can be interrupted and show! continues evaluation and displaying

mkvlr 2022-02-02T10:35:42.141329Z

oh didn't realize this is still a problem for you.

genmeblog 2022-02-02T10:35:59.545079Z

temporary solution is to set browse? false and manually open a browser

genmeblog 2022-02-02T10:36:06.835119Z

which is not big deal for me

genmeblog 2022-02-02T10:36:36.996629Z

just describing that for future reference if other people will face this issue

mkvlr 2022-02-02T10:37:37.300789Z

ah so (clojure.java.browse/browse-url "") hangs for you?

genmeblog 2022-02-02T10:38:18.307609Z

well, no! 😕 hmmm...

genmeblog 2022-02-02T10:39:05.946809Z

on firefox the page opens

genmeblog 2022-02-02T10:39:14.991479Z

google-chrome hangs

mkvlr 2022-02-02T10:39:32.051009Z

strange, we’re not doing more https://github.com/nextjournal/clerk/blob/main/src/nextjournal/clerk.clj#L306

mkvlr 2022-02-02T10:39:56.108129Z

ah ok, so you can’t load http://localhost:7777 in chrome?

genmeblog 2022-02-02T10:40:54.508929Z

I can, but I can't (clojure.java.browse/browse-url "") with google-chrome as a default browser

genmeblog 2022-02-02T10:41:59.214449Z

Don't bother with that, it's 99% related to JVM/WSL interop

mkvlr 2022-02-02T10:45:02.766469Z

alright, good to know though, thanks

genmeblog 2022-02-02T22:42:43.923009Z

Looks like this bug may be related to https://clojure.atlassian.net/browse/CLJ-2493

👍 1
genmeblog 2022-02-01T19:39:36.032769Z

browser window is up and waiting

genmeblog 2022-02-01T19:45:09.308989Z

Firefox hangs, eats cpu on getting data from tailwind.css...

genmeblog 2022-02-01T19:56:10.454919Z

It's not deterministic on my setup, sometimes I can continue, sometimes I can't clerk/halt! helps or not. Anyway, currently at the stage where color-spans is called but nothing happens, ie. resulting div is empty:

genmeblog 2022-02-01T19:56:26.793639Z

when I change transform-fn to identity I'm getting the value

2022-02-01T22:09:51.938369Z

I replicated here https://scicloj.github.io/scicloj.ml-tutorials/userguide-models_clerk.html#/src/scicloj/ml/models_clerk.clj one of my userguides for Large part of it is "generated", now using clerk functions. It has as well quite some vega plots and other images. Most was rather easy to do, Clerk is getting really good. 👍 Overall three issues: 1. cannot control overall output width: https://github.com/nextjournal/clerk/issues/68 2. Non deterministic cach behavoiur if vars are defined multiple times: https://github.com/nextjournal/clerk/issues/66 3. A real TOC is missing. I used a workaround. Should come soon. 4. Cannot render verbatim text with newlines: https://github.com/nextjournal/clerk/issues/69 Great job overall !

🙌 2
2022-02-10T21:12:48.113089Z

Hmm . Not sure. I just know that I use "wide" or "full" often to show wide / full polts and tables. And combining this with the code in the normal ,, narrow style", looks ugly.

2022-02-10T21:20:17.472269Z

mkvlr 2022-02-02T08:38:15.958009Z

Great to hear! On your issues: 1. controlling width was possible, see https://github.com/nextjournal/clerk/issues/68#issuecomment-1027683555 & https://github.com/nextjournal/clerk/blob/ec07ed2701fb82ecac94c786a1310d7e324cab18/notebooks/controlling_width.clj. 2. hope to get around to that next week 3. also coming soon:tm: 4. https://github.com/nextjournal/clerk/issues/69#issuecomment-1027687431

2022-02-02T18:40:56.223079Z

for 1): But this cannot be applied to all cells in one go, but needed to be done for each code expression, correct ? So I look for a way to set the default output width.

mkvlr 2022-02-02T18:43:41.262949Z

correct. What you want to set the default to? Currently the table viewer defaults to :wide while most other cells default to :prose . Wide large images default to :full , others to :wide. So there’s not a single default and I’m unsure there should be as e.g. all images will no look good in :full.

➕ 1
mkvlr 2022-02-02T18:45:28.114349Z

or are you looking for other options besides those three?