Triggering explicit rerun after entering value for st.number_input

I have the following code:

pdf_view = st.session_state[selected_pdf]
def update_page_number() -> None:
    st.session_state[selected_pdf].active_page = st.session_state.temp_active_page
    st.rerun()

st.number_input(f"Some Label", value=pdf_view.active_page, key="temp_active_page", on_change=update_page_number)

The purpose of this is to update the currently shown page of a pdf file via “streamlit_pdf_viewer”.

However this versions gives me the warning (IN THE STREAMLIT APP) “Calling st.rerun in a callback is a no-op”. However, when i remove that line it actually doesnt work and the pdf does not update. It does update the active number and by clicking other buttons that reload the pdf_viewer i end up at the right value but i need to do directly do this when i press enter on the input (as it does currently if it wasnt making the app messed up with that popup)

Cheers

Hi,
The on change and st.rerun can lead to some problem, you can avoid it (not tested) with something like this :

        # Define a function to handle tab changes
        pdf_view = st.session_state[selected_pdf]

        def update_page_number() -> None:
            st.session_state[selected_pdf].active_page = st.session_state.temp_active_page

        page_number = st.number_input(f"Some Label", value=pdf_view.active_page, key="temp_active_page", on_change=update_page_number)
        if page_number != st.session_state.get('temp_active_page', 0):
            st.rerun()

Unfortunately that also does not seem to work.

As with just removing the “st.rerun” this updates the values in other places where that page number is shown but not the pdf viewer itself.

I have now does this:

pdf_view = st.session_state[selected_pdf]
def update_page_number() -> None:
    st.session_state[selected_pdf].active_page = st.session_state.temp_active_page
    st.session_state["rerun_needed"] = True

st.number_input(f"Some Label", value=pdf_view.active_page, key="temp_active_page", on_change=update_page_number)
if "rerun_needed" in st.session_state and st.session_state["rerun_needed"]:
    st.rerun()

Does not seem very elegant but it gets the job done without a popup.

Argh, just found out that somehow this works locally (also locally in docker) but not when hosted online…

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