Order of execution not correct

I want my Show more button to display below the components which are rendered in the display_papers() function.

However, this means that when the num_displayed amount only increases after the papers are displayed. So I have to click the show more button twice before seeing any visual update

Code snippet:

    def display_papers():
        
        print("Papers to display: ", st.session_state.num_displayed)
        for i in range(st.session_state.num_displayed):
            paper = st.session_state.papers[i]
            st.write("---")
            st.markdown(create_card(paper["authors"], paper["publicationDate"], paper["title"], paper["uri"]), unsafe_allow_html=True)

  if len(st.session_state.papers):
        display_papers()
        if st.button("Show more"):
            st.session_state.num_displayed += 5

You could use a callback function:

def increase_papers():
    if "num_displayed" not in st.session_state:
        st.session_state["num_displayed"] = 5
    else:
        st.session_state["num_displayed"] += 5

st.button("Show more", on_click=increase_papers)

5 Likes

That did it! Can’t believe I didnt see this in the docs…

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