Hi. I've seen TOC is possible (after PR56) but how to make it visible?
@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.
ok! It'll be a great feature. So waiting.
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.
Need to also set ^{:nextjournal.clerk/viewer :hide-result}. See https://github.com/nextjournal/clerk/pull/59
ah! thanks! I'm now pretty sure I've seen that before.
you might also have seen https://github.com/nextjournal/clerk/pull/24
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...
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.
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!
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:") not yet but it’s on the roadmap
@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.
@mkvlr I'll be spamming with some specific issues I have 🙂
please do!
First one: when I make custom viewer, how can I use external functions in :render-fn ?
The example is:
(defn make-color-div
[c]
(clerk/html [:div {:style {:background-color "#aaaaaa"}} c]))
(clerk/set-viewers! [{:name :color :render-fn `make-color-div}])Which doesn't work and produces:
while (clerk/set-viewers! [{:name :color :render-fn '#(v/html [:div {:style {:background-color "#aaaaaa"}} %])}]) works
making render-fn without the access to other namespaces is very limiting
do you already have code you’d like to use there? Is it ClojureScript?
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
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
Hmmm... I want to avoid this path. Maybe I should use transform-fn?
Need to experiment more
depends on what you want to generate, hiccup in the end?
in that case you should be able to define a viewer with only a :transform-fn and return hiccup tagged with clerk/html
and you can use all code on the JVM classpath in that case
Yeah! Should work!
(ns transform
(:require [nextjournal.clerk :as clerk]))
(clerk/set-viewers! [{:pred vector? :transform-fn clerk/html}])
[:h1 "Hi Hiccup 👋"]along those lines
:transform-fn can of course be more complex, but should call clerk/html eventually if you want to render hiccup
Got it. Still building intuition about data flow.
me too 🙃
Ok. Following notebook hangs everything, eats all CPU, firefox claims that page is slow (and eats tons of memory). Any idea what happens?
^{: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!))@mkvlr let's continue here (don't to spam main channel)
:transform-fn (comp clerk/html color-spans) doesn't change anything
^{: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! {})try this
the hiccup flavor we’re using (reagent) wants styles as maps
I’d recommend working from a very concrete impl first to implement a viewer
whoops, just fixed an error in the above
now looks like this
gtg now, will check back tomorrow
thanks a lot!
above works indeed, thanks for a hiccup hint. However, selecting viewer with metadata or using with-viewer produces unusable viewer
> I’d recommend working from a very concrete impl first to implement a viewer
^ what do you mean by that?
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.
the only issue is that CSS is inlined
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.
and great to see you got things working!
So far so good 🙂 I'm trying to prepare better doc for clojure2d.color namespace and above attempt was crucial to make it happen.
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@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!
firefox path can be interrupted and show! continues evaluation and displaying
oh didn't realize this is still a problem for you.
temporary solution is to set browse? false and manually open a browser
which is not big deal for me
just describing that for future reference if other people will face this issue
ah so (clojure.java.browse/browse-url " hangs for you?
well, no! 😕 hmmm...
on firefox the page opens
google-chrome hangs
strange, we’re not doing more https://github.com/nextjournal/clerk/blob/main/src/nextjournal/clerk.clj#L306
ah ok, so you can’t load http://localhost:7777 in chrome?
I can, but I can't (clojure.java.browse/browse-url " with google-chrome as a default browser
Don't bother with that, it's 99% related to JVM/WSL interop
alright, good to know though, thanks
Looks like this bug may be related to https://clojure.atlassian.net/browse/CLJ-2493
browser window is up and waiting
Firefox hangs, eats cpu on getting data from tailwind.css...
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:
when I change transform-fn to identity I'm getting the value
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 !
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.
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
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.
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.
or are you looking for other options besides those three?