Button like UI appeared undesirably

Summary

I have created a streamlit app with multiple pages where I used Sessionstate. The problem is, buttons are displayed multiple times at random places. Actually they doesn’t look like original buttons. The buttons are also printed in pages where they are not used.

Undesirable behavior:

Found RerunException causing the error
session.request_rerun() also exhibit same problem.

Sample

Button like UI should not appear multiple times.

I have not used buttons in this page. But appeared.

Hi @vigneshwaran,

Welcome to the Streamlit Community! If I understand your question correctly, on the main part of the web app (not in the side bar) your getting print outs of the valid and not valid over and over again and you want to get rid of these?

I think a minimal working example of your code would be very helpful for bug hunting the culprit of all the unwanted statements :grin::beetle:!

Happy Streamlit-ing!
Marisa

Sorry for the late notice. Found there is a problem in caching spacy model.

import streamlit as st
import session_state
import numpy as np
import pandas as pd
from pathlib import Path
import spacy


@st.cache(allow_output_mutation=True)
def load_dataset(file):
    return pd.read_json(file)

@st.cache(allow_output_mutation=True, suppress_st_warning=True)
def load_model(name: str) -> spacy.language.Language:
    """Load a spaCy model."""
    return spacy.load(name)


@st.cache(allow_output_mutation=True, suppress_st_warning=True)
def process_text(model_name: str, text: str) -> spacy.tokens.Doc:
    """Process a text and create a Doc object."""
    nlp = load_model(model_name)
    return nlp(text)

dictionary = {'valid': [], 'not_valid': []}

df = load_dataset('https://github.com/ryanwholey/jeopardy_bot/raw/master/JEOPARDY_QUESTIONS1.json')
this_session = session_state.get_session(
    a = [],
    b = True,
    N = 500,
)


this_state = session_state.get_state(this_session)

nlp = load_model('en')


N = st.slider("N", 0, 200, value=20)
opt = np.random.choice(df.shape[0]-1, N)

if N != this_state.N:
    this_state.N = N
    this_state.b = True
    this_session.request_rerun()

if this_state.b:
    this_state.a = opt

this_state.b = False

for i in range(this_state.N):
    # st.text(this_state.a[i])
    ques = df.iloc[this_state.a[i]].question
    
    st.write(process_text('en', ques))

    valid = st.button("VALID", key=f'VALID_{i}')
    not_valid = st.button("NOT VALID", key=f'NOTVALID_{i}')

    if valid or not_valid:
        if valid:
            dictionary['valid'].append(this_state.a[i])
        else:
            dictionary['not_valid'].append(this_state.a[i])
        st.write("Updated")

if st.button("NEXT"):
    this_state.b = True
    this_session.request_rerun()

Hey @vigneshwaran!

I’m a bit confused.

Based on your screenshot my initial guess was that your script was simply taking too long to run, in which case the faded buttons are working as intended. But I don’t see the “running man” animation on the top right, so that theory can’t be right. Something bigger must be afoot! Or did you hide that animation somehow?

Found RerunException causing the error
session.request_rerun() also exhibit same problem.

Are you saying this_session.request_rerun() is the source of the problem? What makes you think so?

Found there is a problem in caching spacy model.

Does this mean you found the issue? If so, what was it? I’d love to understand it!



Otherwise, if you’re still experiencing this issue, can you provide an even smaller example where the bug can be seen?

There’s so much going on in the code above, it’s hard to even get it running :smiley:

For starters, I don’t know where you got the session_state code. So I tried removing everything that seemed unrelated to the bug itself and ended up with this:

import streamlit as st

for i in range(10):
    st.write(f"This is iteration {i}")

    valid = st.button("VALID", key=f'VALID_{i}')
    not_valid = st.button("NOT VALID", key=f'NOTVALID_{i}')

    if valid or not_valid:
        st.write("Updated")

if st.button("NEXT"):
    st.experimental_rerun()

But when I run this code but I don’t see the buggy behavior :cry:

Session state is modified to get session and state separately.

You can reproduce from https://github.com/vigneshwaran/streamlit_buttons/

The problem arises somewhere between session state and spacy.