Fork me on GitHub
#babashka
<
2022-11-29
>
jumar06:11:28

Is there a good "REPL story" for babashka? I mean something like rebel-readline to get auto-completion, etc. https://github.com/bhauman/rebel-readline/issues/214 Right now the best I could get is rlwrap bb

jumar07:11:56

My use case, today: I want to quickly explore some JSON data on the command line.

borkdude07:11:53

The nREPL story is good, the console REPL is similarly basic as Clojure. Maybe one day we should have something better

jumar07:11:19

So I should just run bb --nrepl-server and then cider-connect? 🙂

borkdude07:11:07

You can also cider jack in with bb

borkdude07:11:31

Perhaps it checks the existence of a bb.edn file

jumar07:11:33

Do you use cider inspector? The connected REPL looks fine except I'm getting "Inspector error" for the results I'm trying to inspect.

borkdude07:11:42

Hmm that’s one thing that is Cider specific and not yet in bb. You can use portal with bb though, that’s a one liner on the command line and then you can browse your data too

borkdude07:11:13

You could make a bash alias if it’s too long to remember. I can look up the invocation when I’m at the kbd

borkdude08:11:38

@U06BE1L6T Something like this:

echo '[1 2 3]' | bb -Sdeps '{:deps {djblue/portal {:mvn/version "0.34.2"}}}' -e "(require '[portal.api :as p]) (def p (p/open)) (add-tap #'p/submit) (tap> *input*) @(promise)"

jumar08:11:51

I see, thanks!

borkdude08:11:42

https://twitter.com/borkdude/status/1597505695800516609:

cat deps.edn | 
  bb -Sdeps '{:deps {djblue/portal {:mvn/version "0.34.2"}}}' \
  -e "(require '[portal.api :as p]) (def p (p/open)) 
        (add-tap #'p/submit) (tap> *input*) @(promise)"

jurjanpaul09:11:07

Seems super handy! But (on 1.0.166) I just get:

----- Error --------------------------------------------------------------------
Type:     java.lang.Exception
Message:  Could not find namespace: portal.api.
Location: <expr>:1:1

----- Context ------------------------------------------------------------------
1: (require '[portal.api :as p]) (def p (p/open)) 
   ^--- Could not find namespace: portal.api.
2:         (add-tap #'p/submit) (tap> *input*) @(promise)

----- Stack trace --------------------------------------------------------------
user - <expr>:1:1
Apparently I am missing something obvious... (it seems to have been necessary before to build the classpath explicitly and following that approach I got it to work.)

borkdude09:11:00

let me try to erase that dep so I start from scratch...

borkdude09:11:50

borkdude@m1 ~/dev/joyride (master) $ rm -rf ~/.m2/repository/djblue
borkdude@m1 ~/dev/joyride (master) $ cat deps.edn |
  bb -Sdeps '{:deps {djblue/portal {:mvn/version "0.34.2"}}}' \
  -e "(require '[portal.api :as p]) (def p (p/open))
        (add-tap #'p/submit) (tap> *input*) @(promise)"
Downloading: djblue/portal/0.34.2/portal-0.34.2.pom from clojars
Downloading: djblue/portal/0.34.2/portal-0.34.2.jar from clojars
worked...

jurjanpaul09:11:40

Thanks! Still some environmental difference?! :thinking_face:

$ rm -rf ~/.m2/repository/djblue
$ cat deps.edn |
  bb -Sdeps '{:deps {djblue/portal {:mvn/version "0.34.2"}}}' \
  -e "(require '[portal.api :as p]) (def p (p/open))
        (add-tap #'p/submit) (tap> *input*) @(promise)"
----- Error --------------------------------------------------------------------
Type:     java.lang.Exception
Message:  Could not find namespace: portal.api.
Location: <expr>:1:1

----- Context ------------------------------------------------------------------
1: (require '[portal.api :as p]) (def p (p/open))
   ^--- Could not find namespace: portal.api.
2:         (add-tap #'p/submit) (tap> *input*) @(promise)

----- Stack trace --------------------------------------------------------------
user - <expr>:1:1

borkdude09:11:10

what os is this?

borkdude09:11:59

I will create a reminder for myself to figure this out on ubuntu. Not sure why this happens

jurjanpaul09:11:30

Thank you! Have a good day!

borkdude09:11:41

you can also try: (babashka.deps/add-deps '{:deps {...}})

borkdude09:11:48

inside the -e expression to see if that helps

borkdude09:11:19

it could be a caching thing. can you also try: rm -rf ~/.clojure/.cpcache ?

jurjanpaul09:11:03

Yes, I thought of add-deps too. It wil probably also work. But I was just wondering why the code of your tweet wouldn't work right away. Removing that cache doesn't help unfortunately.

borkdude09:11:17

I have no clue why it doesn't work :(

jurjanpaul09:11:22

Thanks anyway! Nothing urgent or that I can't work around.

borkdude10:11:42

On WSL2 + ubuntu it "just works" for me, so I'm afraid I can't repro it. If you can repro it at some point, let me know.

borkdude10:11:39

Does it help if you add this dep?

org.babashka/spec.alpha {:git/url ""
                         :git/sha "1a841c4cc1d4f6dab7505a98ed2d532dd9d56b78"}

borkdude10:11:24

(last attempt)

jurjanpaul10:11:38

Yes! That works!

borkdude10:11:27

maybe bb should include that lib by default

jurjanpaul10:11:59

Totally beyond my understanding why, but I'm sure you know exactly what's going on. 🙂

borkdude10:11:29

I did see a warning about clojure.spec, but it didn't affect the behavior of portal for me

jurjanpaul10:11:04

Oh, oops, I'm sorry. I was now running it from file with the -cp option (and clj -Spath ...) still in it (which already worked before). Removing that, it still does not work. So this is to let you know that spec.alpha was not the issue after all. Thanks anyway! I will switch my attention to more urgent stuff now...

borkdude10:11:37

ok, then I also don't know. laterzzz :)

jurjanpaul14:11:22

Ouch, found that I had a, long forgotten, explicit BABASHKA_CLASSPATH set in my .bashrc. Without that, the original snippet works fine (obviously). Sorry for wasting your time.

zach22:11:36

Hello! I have a question around bootleg and the convert-to function, which I am using in a babashka script. Apologize if this is the wrong channel, but it seemed random enough that y’all might know: I am trying to convert two pages from a website into hickory data maps. The page structure should be identical for both, since they are coming from the same server-rendered site, it is just their content that’s different. The first url returns a proper hickory data map, but the second url only returns “\n”. Has anyone else encountered this? My example bb script:

(ns bb.example
  (:require [babashka.pods :as pods]
            [babashka.curl :as curl]))

(pods/load-pod "bootleg")
(require '[pod.retrogradeorbit.bootleg.utils :refer [convert-to]])

(defn url->hickory
  [url]
  (-> url curl/get :body (convert-to :hickory)))

(let [working-url ""
      broke-url ""]
  (println (str "result: " (keys (url->hickory working-url))))
  (println (str "result: " (url->hickory broke-url))))
Thank you! (and apologies again if this is not the proper channel!)

zach22:11:46

(and further note: when i view the source for either site, there doesn’t seem to be anything crazy different around it…though it’s a LOT of code so i coudln’t just visually tell. There at least isn’t a missing html tag in the broken one)

borkdude22:11:08

let's continue in a thread

borkdude22:11:54

could this be the reason?

borkdude22:11:37

oh the other site doesn't load for me either

borkdude22:11:07

perhaps it's best to test with plain html on disk to rule out any networking stuff

borkdude22:11:34

I'll be afk now 💤 but I'll check back in tomorrow

zach22:11:43

Ah, that’s smart. I’ll test by slurping instead of curling. Thank you, have a good night!

zach22:11:56

A small update: Had the same issue with the files on disk.

(spit "working.html" (:body (curl/get "")))
(spit "broken.html" (:body (curl/get "")))
(convert-to (slurp "broken.html") :hickory) ;=> "\n"
(convert-to (slurp "working.html") :hickory) ;=> big ol' hickory map 

zach23:11:16

Ah, I think I figured it out! There was a /newline at the end of the broken.html file (and in that url). It seems like this was causing convert-to to act unexpectedly. You can see the behaviour with some other urls, too. will not convert to hickory, will. If I change the fn to:

(-> url 
    curl/get :body 
    clojure.string/trim-newline 
    (convert-to :hickory))
It works!

zach23:11:55

Not sure if this is a bug or I need to adjust my understanding of the fn.

Bob B00:11:57

Judging from the note in <https://github.com/retrogradeorbit/bootleg#data-conversion>, this appears to be sort of a "lossy" conversion. It seems like the HTML gets 'closed', and then the newline starts the next 'form'. This can be demonstrated by converting to a hickory-seq:

(utils/convert-to "<p>1</p>\n" :hickory)
;; => "\n"

(utils/convert-to "<p>1</p>\n" :hickory-seq)
;; => ({:type :element, :attrs nil, :tag :p, :content ["1"]} "\n")
I guess whether that'd be considered a bug or not (in that it's just a newline) is open to interpretation.

zach03:11:01

Ah, i had not seen that note. Thanks for pointing it out! This makes more sense, and the example in the doc is clear.