Trouble using streamlit-feedback: Either resetting a single, or storing state of multiple widgets

Hi there,

I’m a born again programmer of a, ahem, certain vintage, so please excuse me if I missed something, I’ve hunted high and low for a solution that makes sense for me.

I have built a chatbot app, which works nicely, and wanted to add a feedback widget, so I chose streamlit-feedback. I’ve found it quite time-consuming and troublesome, however, I would imagine I’m either using it wrongly or I just don’t get it. I’ve reduced this to as small an example as I could but I’m, of course, open to suggestions.

The idea is that for each chatbot response, I have a streamlit-feedback instance that the user interacts with. The result of this feedback can then be stored in an expandable component, and it works well, until I collapse and expand the components and the feedback is reset to an initial state. I’ve also tried using a single feedback instance, and just storing the resulting text and score but this doesn’t work either. I even tried replacing the expander with just a horizontal line but that seems to redraw the page each time it’s added.

Here’s the code and some screenshots of the multiple expander version.

I’d be most grateful of any help or pointers to some docs that might.

Cheers!

Jim

import streamlit as st
from streamlit_feedback import streamlit_feedback
import uuid

def _submit_feedback(feedback_dict, emoji=None):
    st.toast(f"Feedback submitted: {feedback_dict}", icon=emoji)
    return feedback_dict

feedback_kwargs = {
    "feedback_type": "thumbs",
    "optional_text_label": "Please provide extra information",
    "on_submit": _submit_feedback,
}

def get_feedback(key):
    return streamlit_feedback(
                **feedback_kwargs,
                key=key,
            )

# Add an input field
input_field = st.text_input("Enter some text")

if "saved_texts" not in st.session_state:
    st.session_state["saved_texts"] = []

if st.button("Save"):
    # Generate a unique key for the feedback widget
    widget_key = str(uuid.uuid4())

    # Save the input text and widget key in the session state
    st.session_state["saved_texts"].append((input_field, widget_key))
    
# Reverse the order of the saved texts. most recent should be displayed first
saved_texts = list(reversed(st.session_state["saved_texts"]))

# Iterate over the saved texts and display them with a feedback widget
for text, key in st.session_state["saved_texts"]:
    with st.expander("Saved Text and Feedback", expanded=True):
        col1, col2 = st.columns(2)
        with col1:
            st.write(text)
        with col2:  
            feedback = get_feedback(key)

            if feedback is not None:
                # Display the feedback text in grey
                st.markdown(f"<p style='color: grey;'>{feedback['text']}</p>", unsafe_allow_html=True)




This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.