hey friends. I'd really wish to have some portal-like UI experience in a custom app (React+TS) that needs data viewing capabilities. as far as I understand, I might be able to tape something together with an iframe, but that's... suboptimal. is there any option I'm missing? do you know of any lib that already copied portal's approach?
ugh still haven't made much progress 😅 there are things going on (or not going on) that I need to understand 🔍
yay -- this is lovely. just perfect for experimenting at this stage. thanks a bunch
I did notice something like having the embedded viewer steal certain actions -- like pressing space. am I missing some way to make it not do that? 🤔
not a react specialist by far, I'm happy I've gotten this to work:
import { useRef, useEffect } from 'react'
import { useEvalQuery, EvalQueryParams } from '../queries.js'
import { toEDNStringFromSimpleObject } from 'edn-data';
export default function Output(query: EvalQueryParams) {
const ref = useRef(null)
const { data } = useEvalQuery(query)
useEffect(() => {
(window as unknown).portal_api.embed().renderOutputItem({
mime: 'x-application/edn',
theme: 'portal.colors/catppuccin',
text: (() => toEDNStringFromSimpleObject(data))
}, ref.current)
}, [ref, data])
return (
<>
<h3>Output</h3>
<div>
<div>
<div ref={ref}></div>
</div>
</div>
</>
)
}window.portal_api comes from this index.html addition:
<script type="module" src=""></script>
(just for future reference)
I haven't advertised this too much, but you can embed the Portal UI via javascript kinda like https://github.com/djblue/portal/blob/master/dev/notebook/scittle.clj#L48-L56
I think currently it only supports "x-application/edn" but adding other types shouldn't be too difficult.
You can even pull the js directly from https://djblue.github.io/portal/main.js if you want to quickly bootstrap things
ohh thanks I'll have a look in a bit
that sounds great
Is it possible to set default viewer when using JSON encoding to send data to portal? As far as I understand, encoding throws away metadata which is required to specify the default viewer My use-case is unusual, I am sending JSON requests from non-clojure language to /submit endpoint, so I prefer not to use EDN
look what I found hidden in the dust 😅 https://github.com/srenatus/portal -- but there's https://github.com/srenatus/portal/blob/main/tap.go
@djblue thank you for pointers, will see what I end up with. I am currently thinking towards specifying something like XPath for viewers, i.e. so that with one rule I can change viewers for multiple nested keys
IIRC it's not possible. metadata is an EDN thing, and there's no way to translate this from JSON. I had the same problem at one point and that was the answer 😳
Makes sense, thank you for clarifying! Just out of curiosity, are you still using Portal with Go? Were there any other similar tools that you considered?
I think setting up your own submit endpoint would give you the most flexibility in terms of how to communicate this type of info to Portal. If you don't want to do that, you can always patch the "/submit" handler in portal to intercept the value and do interesting things with it, like call tap> which would allow setting up a proper submit function:
(defmethod portal.runtime.jvm.server/route [:post "/submit"] [request]
(tap> (portal.runtime.jvm.server/body request))
{:status 204
:headers {"Access-Control-Allow-Origin" "*"}})then https://cljdoc.org/d/djblue/portal/0.59.0/doc/guides/default-viewer would work for you 👌
With these things in place, you can experiment with different encodings for your json to represent setting a default viewer. If you find one that you are sufficiently happy with, and is generic enough, we can see about adding it to Portal directly 👍
> Just out of curiosity, are you still using Portal with Go?
nah it was just a hack I had been using for a day or two. didn't stick, although it was kinda need to Tap(x any) from Go