Button not working after being pressed

Summary

Button not working

Steps to reproduce

Code snippet:

import streamlit as st


#Session states variables 

#Tracks current page number
if 'current_page_number' not in st.session_state:
    st.session_state.current_page_number = 0

#Inits placeholder container to add / delete as required
if 'current_page_container' not in st.session_state:
    st.session_state.current_page_container = st.empty()

#Bool to manage first render
if 'first_boot' not in st.session_state:
    st.session_state.first_boot = False

#Next page
def step_up():
    st.session_state.current_page_number = st.session_state.current_page_number + 1
    render_page(st.session_state.current_page_number)

#Back page
def step_down():
    if not st.session_state.current_page_number <= 0:
        st.session_state.current_page_number = st.session_state.current_page_number - 1
    render_page(st.session_state.current_page_number)

#Based on page number render 
def render_page(page_number):

    #Idk why but without rendering something first, it wont work
    st.write(' ')
    with st.session_state.current_page_container.container():
        if page_number == 0:
            st.write(f'This is connect page')
            a = st.button('Connect')
            if a:
                st.write('pressed a')
        if page_number == 1:
            st.write(f'This is data class levels')
            b = st.button(str(page_number))

#Load connect page (page_number = 0) upon first boot
if not st.session_state.first_boot:
    render_page(st.session_state.current_page_number)
    st.session_state.first_boot = True
        
#Navigation buttons
col1, mid, col2 = st.columns([1,3,1])
with col1:
    back = st.button('Back', on_click = step_down, use_container_width = True, type = 'primary')

with col2:
    next = st.button('Next', on_click = step_up, use_container_width = True, type = 'primary')

Expected behavior:

Press connect button a st.write() โ€œpressed aโ€

Debug info

  • Streamlit version: 1.18
  • Python version: 3.8

Additional information

Whenever i press the โ€œaโ€ button labeled as โ€œconnectโ€ nothing gets written in the screen. It just displays as blank and only shows the next and back buttons.

Your render_page function is set to occur within your step_up and step_down functions. Those stepping functions are in turn set as callbacks to the back and next buttons. Hence, Streamlit will only render your page in response to the back or next click and then not render anything with any other click.

Iโ€™m not sure what logic you would like to achieve with the first_boot key in session state since you arenโ€™t using the Streamlit-native multipage functionality. However, you could remove the rendering function from both step functions and then replace:

#Load connect page (page_number = 0) upon first boot
if not st.session_state.first_boot:
    render_page(st.session_state.current_page_number)
    st.session_state.first_boot = True

with just:

render_page(st.session_state.current_page_number)

You already initialize the page number to 0 so the first_boot key isnโ€™t really doing anything.

I see, thanks a lot for replying

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