Another question I m trying to attach
Another question, I'm trying to attach
keydown
listener to TextArea (so that I can submit when "Enter" but allow Shift+Enter to do the default multi-line handling)
According to ipyvuetify
docs (https://github.com/widgetti/ipyvuetify) I should be able to do:
But this doesn't work -
17 Replies
In Solara you'll need use_event for this.
I tried
but this gives a weird error:
What is a stale element?
hooks (like use_event) may not be used conditionally. Is there an if oor loop above this?
(I see we're missing documentation for this)
no conditionals, but there are Row and Hbox contexts above it
can you show the code?
It is based on https://itnext.io/python-how-to-build-a-chatgpt-interface-in-solara-fd6a1e15ef95
I just added the
rv.use_event(...)
I'll take a look.. but I just remembered.. we don't send the key code yet for key presses ๐ฆ
we were just talking about that yesterday!
thank you!
Looks like it's a bug in Solara, your code should work. We will fix this. I think we have a workaround for this...
using use_event in it's own component makes it work
@sl.component
def MyTextarea(input, on_input, on_key_down):
text_area = sl.v.Textarea(v_model=input, on_v_model=on_input, solo=True, hide_details=True, outlined=True, rows=2, auto_grow=True)
sl.v.use_event(text_area, "keydown", on_key_down)
Thank you for the workaround, I can confirm it works.
The key_down is not very useful without the key event data, but I managed to use it for my purpose (checking if the textarea text ends with
\n
)
There is however some weirdness streaming changes - when I click "send" button it triggers stream of changes that beautifully make their way to the browser, but when I run the same function from the keydown event (when the text ends with \n
) then I only see the browser update after all the changes are complete (several seconds) instead of streaming change by changeregarding no event data, i've opened this PR https://github.com/widgetti/ipyvue/pull/76
GitHub
feat: support serialization of more event data by maartenbreddels ยท...
useful for getting key event data, mouse event data, etc.
could you share your full code again?
Regarding the streaming changes - I solved it by using a thread, I'm sorry I can't share a link to git as it contains some company code I can't share,
but recreating it should be easy as the relevant parts are a small change over the article (https://itnext.io/python-how-to-build-a-chatgpt-interface-in-solara-fd6a1e15ef95)
This is how I solved it now:
Again, this problem is kind of hard to explain without showing a video - but I'll try -
The button click is mapped "ask_chatgpt", which calls the backend and runs multiple changes to the "messages" reactive value (streaming the GPT respones one word at a time). This works great and shows up in the browser one word at a time.
Idealy I wanted to call the same function when "Enter" is pressed in the textarea
but when I called it from
So I moved it to
on_v_model
(or from the textarea keydown event using the workaround above).
The multiple changes were not visible in the browser until the end of the function.So I moved it to
use_thread
and now it works great, but I think it should work from the text-area event handler directly without a thread, as it does for the button clicki think both the event handler and the click should not work actually ๐
i'm surprised one does work
it should make use of a thread, or a async task, otherwise the ui cannot respond
I was really impressed with Solara that this streaming of changes work so well,
it would be nice to figure out how is the IconButton on_click doing this dark magic and replicate it to the other handlers, but not urgent for me - the use_thread is a bit clunky to code but it works great
cool to hear ๐
yes, i think we want some kinda of decorator or hook .. that allows sth like this
the most challenging thing is the name ๐