Fork me on GitHub
#portal
<
2023-01-25
>
Ben Lieberman18:01:19

Are there any obvious reasons why I might not be able to tap a Pandas DF into Portal? I'm dipping into libpython-clj and it seems to not play very nicely.

Ben Lieberman18:01:08

As far as I know all libpython-clj objects have Java wrappers on them.

djblue18:01:10

Are you getting a specific error or is nothing showing up?

Ben Lieberman18:01:49

Nothing, and in fact it seems to break my portal instance, bc if I try to tap trivial clojure values thereafter, it also does nothing

djblue19:01:52

My best guess is that it's a serialization issue :thinking_face:

Ben Lieberman19:01:35

ok, I'll think on it/explore some more. That seems reasonable to me because I do believe that libpython-clj forwards any of java.lang.Object's .toString calls to __str__ in Python

Ben Lieberman19:01:47

I don't know if that would be problematic under these circumstances.

djblue19:01:04

(ns portal.runtime.jvm.server)
(defn- rpc-handler-local [request]
  (let [session (rt/open-session (:session request))
        send!   (fn send! [ch message]
                  (server/send! ch (rt/write message session)))]
    (server/as-channel
     request
     {:on-receive
      (fn [ch message]
        (let [body  (rt/read message session)
              id    (:portal.rpc/id body)
              op    (get ops (:op body) not-found)]
          (binding [rt/*session* session]
            (op body (fn [response]
                       (try
                         (send!
                          ch
                          (assoc response
                                 :portal.rpc/id id
                                 :op :portal.rpc/response))
                         (catch Exception ex
                           (send!
                            ch
                            (assoc response
                                   :return (Throwable->map ex)
                                   :portal.rpc/id id
                                   :op :portal.rpc/response)))))))))
      :on-open
      (fn [ch]
        (swap! c/connections assoc (:session-id session) (partial send! ch))
        (when-let [f (get-in session [:options :on-load])]
          (f)))
      :on-close
      (fn [_ch _status]
        (swap! c/connections dissoc (:session-id session)))})))
might help debug this issue for you

👀 2
djblue00:01:38

I get TypeError: unhashable type: 'dict' when I try libpython-clj locally, I think this is probably why you are having issues. Not sure what the fix is here :thinking_face:

djblue00:01:38

This makes sense since portal will keep track of all objects it sends to the client in a hash map

djblue01:01:47

(hash-map (py/->py-dict {}) 1) is the root of the problem

Ben Lieberman01:01:34

Sorry been away from my computer. So if I understand right, you mean that Portal can't work with Python dicts at all or is this some idiosyncracy of Pandas?

Ben Lieberman01:01:13

I don't know the libpython-clj API well yet as this is my first day using it but they have a set of factory functions for converting between Java and Python objects that I will look into

Ben Lieberman01:01:38

Though I wonder whether doing so would be at the expense of Pandas' representations

djblue01:01:59

I think all python objects will probably cause issues. I wonder why they don't proxy hashing :thinking_face:

djblue01:01:12

Ohh, https://github.com/clj-python/libpython-clj/blob/e6ec1aca703fdf736ea971be22f0624190fac381/src/libpython_clj2/python/bridge_as_jvm.clj#L230-L231 the issue is python can't hash dicts:

>>> hash({})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'

Ben Lieberman02:01:59

Yeah its been quite some time since I was last a Python user but that particular error is fresh in my mind

Ben Lieberman02:01:58

IIRC Pandas also has various ways to reshape DFs into more generally friendly shapes anyway

Ben Lieberman02:01:28

So I will explore those as this isn't a huge priority for me. But it would be super cool to get DFs into Portal one day!

djblue02:01:44

I'll see about reducing the constraints on objects in the portal cache :thinking_face:

2
djblue03:01:14

I got https://github.com/djblue/portal/commit/6f63556688ce2ff97bc5e7cc15aa66ecd472e8ff working, but not sure if I'm a fan especially because portal doesn't know what to do with the value {'a': 'b'}