nbb

2023-04-26T21:26:57.234809Z

I posted https://stackoverflow.com/questions/76106830/how-can-i-use-clojurescripts-hiccups-library-in-nbb on stack overflow and was directed here. My basic question is: how do I consume cljs libs within nbb (and manage them as deps)? I'm returning to clojure after some years away and i don't remember anything about the tooling--and nbb seems different than I remember anyway. Can someone point me to an example script that shows how to do this? I'm in a Linux environment.

borkdude 2023-04-26T21:28:06.855239Z

Hi @sirrobert! Have you tried what I suggested in the SO answer? Try to run bb --config nbb.edn -e nil and see it what it does?

borkdude 2023-04-26T21:28:51.985919Z

Nbb is a bit special in that it can't load all CLJS libs since the code has to be compatible with the ClojureScript interpreter that is used in nbb, but it seemed you didn't even reach that stage yet and had some other problem

2023-04-26T21:30:46.552399Z

No, I didn't understand that part of the reply yet. I just focused do far on generating the html from hiccups (which worked). Now I'm trying to understand the deps management side

2023-04-26T21:31:54.996389Z

I will try that and reply back here in a while (I'm away from my computer right now). Thanks so much for the reply.

borkdude 2023-04-26T21:32:29.407619Z

Alright, I think we're in different timezones and it's getting late here but I'll surely check back tomorrow

👍 1
2023-04-26T21:33:11.091109Z

I appreciate it!

2023-04-27T02:43:50.445319Z

So, here's my nbb.edn file:

{:deps
 {hiccups/hiccups {:mvn/version "0.3.0"}}}
and here's the error when I run the command bb --config nbb.edn -e nil:
Error: Could not find or load main class clojure.main
Caused by: java.lang.ClassNotFoundException: clojure.main
Exception in thread "main" java.io.FileNotFoundException: /home/sirrobert/.clojure/.cpcache/A4CEF68F951217FE6FE54759160B5276.cp (No such file or directory)
	at java.base@19.0.2/java.io.FileInputStream.open0(FileInputStream.java)
	at java.base@19.0.2/java.io.FileInputStream.open(FileInputStream.java:219)
	at java.base@19.0.2/java.io.FileInputStream.<init>(FileInputStream.java:158)
	at $fn__11617.invokeStatic(io.clj:229)
	at $fn__11617.invoke(io.clj:229)
	at $fn__11569$G__11523__11576.invoke(io.clj:69)
	at $fn__11591.invokeStatic(io.clj:165)
	at $fn__11591.invoke(io.clj:165)
	at $fn__11530$G__11519__11537.invoke(io.clj:69)
	at $reader.invokeStatic(io.clj:102)
	at $reader.doInvoke(io.clj:86)
	at clojure.lang.RestFn.invoke(RestFn.java:410)
	at clojure.lang.AFn.applyToHelper(AFn.java:154)
	at clojure.lang.RestFn.applyTo(RestFn.java:132)
	at clojure.core$apply.invokeStatic(core.clj:669)
	at clojure.core$slurp.invokeStatic(core.clj:7009)
	at clojure.core$slurp.doInvoke(core.clj:7009)
	at clojure.lang.RestFn.invoke(RestFn.java:410)
	at borkdude.deps$_main.invokeStatic(deps.clj:816)
	at borkdude.deps$_main.doInvoke(deps.clj:589)
	at clojure.lang.RestFn.applyTo(RestFn.java:137)
	at clojure.core$apply.invokeStatic(core.clj:667)
	at babashka.impl.deps$add_deps$fn__26946$fn__26947.invoke(deps.clj:101)
	at clojure.lang.AFn.applyToHelper(AFn.java:152)
	at clojure.lang.AFn.applyTo(AFn.java:144)
	at clojure.core$apply.invokeStatic(core.clj:667)
	at clojure.core$with_bindings_STAR_.invokeStatic(core.clj:1990)
	at clojure.core$with_bindings_STAR_.doInvoke(core.clj:1990)
	at clojure.lang.RestFn.invoke(RestFn.java:425)
	at babashka.impl.deps$add_deps$fn__26946.invoke(deps.clj:101)
	at babashka.impl.deps$add_deps.invokeStatic(deps.clj:101)
	at babashka.main$exec$fn__31700.invoke(main.clj:872)
	at clojure.lang.AFn.applyToHelper(AFn.java:152)
	at clojure.lang.AFn.applyTo(AFn.java:144)
	at clojure.core$apply.invokeStatic(core.clj:667)
	at clojure.core$with_bindings_STAR_.invokeStatic(core.clj:1990)
	at clojure.core$with_bindings_STAR_.doInvoke(core.clj:1990)
	at clojure.lang.RestFn.invoke(RestFn.java:425)
	at babashka.main$exec.invokeStatic(main.clj:824)
	at babashka.main$main.invokeStatic(main.clj:1165)
	at babashka.main$main.doInvoke(main.clj:1124)
	at clojure.lang.RestFn.applyTo(RestFn.java:137)
	at clojure.core$apply.invokeStatic(core.clj:667)
	at babashka.main$_main$f__31813__auto____31823.invoke(main.clj:1204)
	at clojure.lang.AFn.run(AFn.java:22)
	at java.base@19.0.2/java.lang.Thread.run(Thread.java:1589)
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:775)
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:203)
Exception in thread "main" java.lang.NullPointerException
	at clojure.lang.RT.intCast(RT.java:1221)
	at babashka.main$_main.invokeStatic(main.clj:1204)
	at babashka.main$_main.doInvoke(main.clj:1190)
	at clojure.lang.RestFn.applyTo(RestFn.java:137)
	at babashka.main.main(Unknown Source)

2023-04-27T02:48:53.378959Z

As an aside (not directly about the deps failure), but the built-in reagent hiccups has some bumps: it has some react requirements like needing a key property for lists. I'm not building a react app, but static html. I'd much rather just bring it in clean and render html directly without reagent. Thanks again for your help!

borkdude 2023-04-27T07:17:10.540759Z

@sirrobert re deps failure: can you try:

rm -rf ~/.deps.clj
bb --config nbb.edn -e nil

borkdude 2023-04-27T07:26:41.490739Z

As for react keys: use render-to-string instead. I understand you want only hiccup, but this is currently the recommended way in nbb. One of the problems with hiccups is that it doesn't work in a CLJS-only environment due to this: https://github.com/teropa/hiccups/blob/master/src/clj/hiccups/core.clj It contains references to clojure.lang.PersistentVector

2023-04-27T11:57:27.861029Z

1. Great! That seems to have cleared up the deps issue. I guess all my fiddling around had created a dirty environment. 2. This is the way I am requiring it within my cljs script. Is this the recommended method? I don't quite understand the implications of the two forms I've seen.

(ns example)

(require '[reagent.dom.server :refer [render-to-string]])
(require '[cljs.reader :refer [read-string]])

(defn render [content]
  (render-to-string
    (read-string content)))

#js {:render render}

2023-04-27T12:04:50.549949Z

Finally, that makes sense about hiccups-- not a problem. I'm not even attached to it as a lib, I just remembered it from before and it seems like a clean approach. Using render-to-string (as in the snippet above), with the following html (taken from the hiccups page):

[:div
 [:ul (for [x (range 1 4)] [:li x])]
 [:p "Hello, World!"]]
I still get the following warning:
Warning: Each child in a list should have a unique "key" prop.

Check the top-level render call using <ul>. See  for more information.
    at x
<div><ul>for<x>range<!-- -->1<!-- -->4</x><li>x</li></ul><p>Hello, World!</p></div>
If that's normal, I can live with it; if not, I'd love to get rid of it =) Thanks again!

borkdude 2023-04-27T12:21:04.955279Z

@sirrobert Aha.. didn't know about those keys when just generating a string, that's a bit annoying yes. I once wrote a little hiccup function in user space when babashka didn't yet have one: https://github.com/babashka/babashka/blob/8c1077af00c818ade9e646dfe1297bbe24b17f4d/examples/notes.clj#L21

borkdude 2023-04-27T12:21:12.382709Z

Perhaps that suffices your needs

borkdude 2023-04-27T12:22:08.115239Z

#babashka does have hiccup2.core (but this is not CLJS, more like JVM Clojure)