Fork me on GitHub
#sci
<
2023-09-18
>
timothypratley21:09:47

Hi 👋 Is it possible to initialize SCI as though it was Babashka?

(sci/init {???})
So that we can evaluate forms from a Babashka compatible version of SCI running inside the JVM:
(sci/eval-form ctx form)
Or even better, is there a way we can Depend on Babashka and evaluate from it? something like:
(bab/eval ...)
I want to evaluate Babashka notebooks using JVM notebook tools (Clay), switching the evaluator. The SCI part works great, but I'm not sure how to initialize it to behave like Babashka with aliases etc.

borkdude21:09:05

Hey, the SCI configuration of babashka is done in babashka/main.clj

borkdude21:09:26

Is it important that your code runs through SCI or would you just like to have the babashka dependencies available?

timothypratley21:09:24

Thank you for the pointers! It's not necessary to run in SCI, my main goal is that the notebook is runnable with bb as well as evaluated correctly to produce results for the notebook. Ideally I'd like it to give the same results in both (error for stuff that shouldn't work, and values when it should work). I took a look into main.clj and the challenge seems to be that the construction of the options is inside the exec... so I'm not sure how to feed it one form at a time. Maybe I should actually set up a process with exec and feed it lines from the file over a REPL, or something like that instead :thinking_face: That seems like it would be closest to a real BB env. /shrug

borkdude21:09:27

yes. that's what I was I was thinking too

timothypratley21:09:59

Ah O.K. I'll experiment with that for a bit 🙂

borkdude22:09:02

you can get all the built-in deps using the bb print-deps command btw

borkdude22:09:47

perhaps spinning up a prepl would be convenient for your use case, this is also how bb support for http://nextjournal.com was done

timothypratley03:09:55

Nice! Thank you I didn't know about prepl, that sounds perfect. I'll try that. One thing that's confusing me still; Inside a Clojure REPL when I try

(require '[babashka.main :as bb])
I get an error:
Execution error (FileNotFoundException) at babashka.impl.features/eval11693 (features.clj:33).
Could not locate babashka/impl/xml__init.class, babashka/impl/xml.clj or babashka/impl/xml.cljc on classpath.
I have
babashka/babashka                 {:mvn/version "1.3.184"}
in my deps, and I can navigate to babashka.impl.features, and even
(require '[babashka.impl.features])
works fine, but still I can't load babashka.main to run (-main). Is intended to load IN Babashka not Clojure?

timothypratley05:09:54

Maybe instead I should launch an nrepl-server and connect with an nrepl-client instead of trying to have it all run in the same process. :thinking_face:

borkdude05:09:40

No, babashka.main does not run IN babashka

borkdude05:09:14

You can launch a prepl via the command line arguments via the main function

timothypratley00:09:21

Hi Michiel, I've been experimenting and ran into a problem with metadata:

bb -e '(clojure.core.server/io-prepl)'
(set! *print-meta* true)
{:tag :ret, :val "true", :ns "user", :ms 2, :form "(set! *print-meta* true)"}
^:foo {}
{:tag :ret, :val "{}", :ns "user", :ms 1, :form "^:foo {}"}
Notice :val "{}"
clojure -e '(clojure.core.server/io-prepl)'
(set! *print-meta* true)
{:tag :ret, :val "true", :ns "user", :ms 3, :form "(set! *print-meta* true)"}
^:foo {}
{:tag :ret, :val "^{:foo true} {}", :ns "user", :ms 1, :form "^:foo {}"}
Notice :val "^{:foo true} {}" The metadata is essential to the behavior of the notebook, but in babashka it was lost.

timothypratley01:09:14

Actually I tried this and it seems to work better: 1. Launch a bb nrepl-server 2. Connect and request the bb session load a custom evaluator that will read the notebook and preserve the metadata as desired 3. The results of custom evaluation are received over the nrepl socket back to the Clojure world 4. profit I wasn't expecting the custom evaluator to just work in babashka, so that's great that it does 🙂

borkdude05:09:58

ok, feel free to file a bug about the io-prepl and metadata, provided that it does work in JVM Clojure

timothypratley21:09:15

Thanks 🙂 It's working now 🙂

🎉 1