Open aiakubovich opened 8 months ago
Hey @aiakubovich, is there a reason for using st.form
for this? You can also feed your handle_feedback
function into the on_submit
parameter. Let me know if that works for you
Hi @jeffkayne, if I use:
streamlit_feedback(feedback_type="faces",
optional_text_label="[Optional] Please provide an explanation",
align="flex-start",
key='fb_k',
on_submit = handle_feedback)
function:
def handle_feedback():
st.write(st.session_state.fb_k)
st.toast("✔️ Feedback received!")
does not output anything (i do not see printed st.write
or st.toast
pop-up). So on_submit
does not work for some reason
Hey @aiakubovich, what versions are you using?
Everything is working on my side with versions streamlit==1.30.0 streamlit-feedback==0.1.2
Here you can see me running this with the following code:
import streamlit as st
from streamlit_feedback import streamlit_feedback
def handle_feedback(user_response):
st.write(st.session_state.fb_k)
st.toast("✔️ Feedback received!")
streamlit_feedback(
feedback_type="faces",
optional_text_label="[Optional] Please provide an explanation",
align="flex-start",
key='fb_k',
on_submit=handle_feedback
)
https://github.com/trubrics/streamlit-feedback/assets/43336277/458a4e26-9108-47ba-9f56-50becc2d88b2
Hey @jeffkayne, thank you for looking at it.
Your code works for me as well but there needed to be st.chat_message
in code to reproduce that problem. Let me give you minimal reproducible code:
import streamlit as st
from streamlit_feedback import streamlit_feedback
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
def display_answer():
for i in st.session_state.chat_history:
with st.chat_message("human"):
st.write(i["question"])
with st.chat_message("ai"):
st.write(i["answer"])
# If there is no feedback show N/A
if "feedback" in i:
st.write(f"Feedback: {i['feedback']}")
else:
st.write("Feedback: N/A")
def create_answer(question):
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
message_id = len(st.session_state.chat_history)
st.session_state.chat_history.append({
"question": question,
"answer": f"{question}_Answer",
"message_id": message_id,
})
def fbcb():
message_id = len(st.session_state.chat_history) - 1
if message_id >= 0:
st.session_state.chat_history[message_id]["feedback"] = st.session_state.fb_k
display_answer()
if question := st.chat_input(placeholder="Ask your question here .... !!!!"):
create_answer(question)
display_answer()
## thumbs up/down work with this approach
with st.form('form'):
streamlit_feedback(feedback_type="thumbs", optional_text_label="[Optional]", align="flex-start", key='fb_k')
st.form_submit_button('Save feedback', on_click=fbcb)
## thumbs up/down do NOT work with this approach
# streamlit_feedback(feedback_type="thumbs", optional_text_label="[Optional]", align="flex-start", key='fb_k', on_submit='streamlit_feedback')
here is how it looks like: [1a]
[2a]
but if i use streamlit_feedback(feedback_type="thumbs", align="flex-start", key='fb_k', on_submit='streamlit_feedback')
instead:
[1b]
[2b]
[3b]
So I want to get rid of with st.form('form'):
but still have the same feedback functionality.
I'm not sure what you are trying to achieve. You must take into account that the streamlit_feedback
component triggers a page reload immediately (this is how streamlit components work). Now on this rerun of the script, when you get to the streamlit_feedback
line, it runs the on_submit
callback if specified.
The reason there is a different functionality in the code with an st.form
is that within an st.form
any actions or changes of state are not applied until st.form_submit_button
is called.
I hope this helps
I am also facing the same issue does anyone found the answer?
Thank you for awesome package. I was able to add streamlit-feedback into chatbot app via
st.form
:It works but there two problems: 1) To get it work user first need click on SUBMIT and only then to "Save feedback".
If user click "Save feedback" then
st.session_state.fb_k
will be None2) Feedback inside
st.form
does not look very good and I am looking to ways to get rid ofst.form
but still have the same functionaly.I looked in examples.py in the repo and similar issues but did not find anything that would help to resolve the problem.
Full app code: