The button inside a button seems to reset the whole app.Why?

@nthmost I believe this is related to a problem I’ve been having with streamlit, where my dashboard also refreshes whenever I change any option (checkbox, button, selectbox, etc.). Essentially, what my board does is take a query from the user, return some results from a website, run my classifier on the returned results, and displays them. The output of each classified piece of content has a button I’d like users to be able to click to give feedback (i.e. that the classification is incorrect). However, it’s non-functional when clicking the checkbox refreshes the panel and clears the display of all the comments. This is the relevant section of my code (from my main function) that triggers the classification and display of the results.

    # Perform classification task
    results, outputs, messages = [], [], []
    if st.button("Classify"):
        with st.spinner("Loading Model"):
            model, tokenizer = get_model_and_tokenizer(select_model)

        with st.spinner("Analyzing..."):
            results = classify(texts, model, tokenizer)
            outputs = [None] * len(results)
            messages = [None] * len(outputs)
        
    # Display results
    for idx, res in enumerate(results):
        classification = max(res['scores'], key=res['scores'].get)
        output_str =f"""
        ### {idx+1} %s

        | ?   | category 1   | category 2   |
        |:---:|:-----------------:|:-------------:|
        |{res['scores']['?']} | {res['scores']['ct']} | {res['scores']['c2']} |
        ***
        """
        st.markdown('----\n----')
        st.markdown(res['raw'])

        if classification == '?':
            st.info(output_str % "Cannot determine category")
        elif classification == 'not antisemitic':
            st.success(output_str % "This looks like category 1 comments in my training set")
        elif classification == 'antisemitic':
            st.error(output_str % "This is similar to category 2 comments in my training set")
        else:
            raise ValueError("Unexpected classification value", classification)
        if output_options['tokens']:
            st.info(f"#### {current_model} Tokenization\n {res['tokens']}")
        
        messages[idx] = st.empty()
        outputs[idx] = st.checkbox(f'Mark classification as incorrect', False, idx+1)


    for cb, mess in zip(outputs, messages):
        if cb:
            mess.markdown("### Thanks for marking this!")
            save_feedback(results[idx], mess)

If there’s any easy way to fix this behavior, I would appreciate knowing what it is. I’ve already tried setting the value of the Classify button to a variable, as well as moving the loop into or out of a function call. No matter what, hitting any control or widget leads to a re-run of the entire main function, by the looks of it.

3 Likes