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.
As far as I know all libpython-clj objects have Java wrappers on them.
Are you getting a specific error or is nothing showing up?
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
My best guess is that it's a serialization issue 🤔
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
I don't know if that would be problematic under these circumstances.
(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 youI 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 🤔
This makes sense since portal will keep track of all objects it sends to the client in a hash map
(hash-map (py/->py-dict {}) 1) is the root of the problem
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?
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
Though I wonder whether doing so would be at the expense of Pandas' representations
I think all python objects will probably cause issues. I wonder why they don't proxy hashing 🤔
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'Yeah its been quite some time since I was last a Python user but that particular error is fresh in my mind
IIRC Pandas also has various ways to reshape DFs into more generally friendly shapes anyway
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!
I'll see about reducing the constraints on objects in the portal cache 🤔
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'}