streamlit st.text_input: value not updating as expected

New to streamlit, this is my issue: I’m trying to provide the user with an interface to annotate some data. For each item the user should give a label and may optionally leave a comment, and they can navigate across the different items with ‘prev’ and ‘next’ buttons. A basic version of the code is here:

import streamlit as st

N=10

choices_values = ['No label', 'YES','NO', 'NA/unclear']
choices_position = { choice:i for i,choice in enumerate(choices_values) }
map_choice_to_value = { 'No label': None, 'YES': 1, 'NO': 0, 'NA/unclear': -1}
map_value_to_choice = { value:key for key, value in map_choice_to_value.items() }

# go to next pair
def next_pair():
    st.session_state.current_index += 1

# go to previous pair
def prev_pair():
    st.session_state.current_index -= 1

# save 
def change_label():
    choice = st.session_state.select_label
    st.session_state.labels[st.session_state.current_index] = map_choice_to_value[choice]

def edit_comment_area():
    st.session_state.comments[st.session_state.current_index] = st.session_state.comment_area



top_text = st.markdown('**Loading data...**')
# initialize state variables
if 'labels' not in st.session_state:
    st.session_state.labels = [None] * N
if 'comments' not in st.session_state:
    st.session_state.comments = [None] * N
if 'current_index' not in st.session_state:
    st.session_state.current_index = 0
n_labels = sum(x is not None for x in st.session_state.labels)
top_text.markdown(f"**Item {st.session_state.current_index+1}/{N}** ({n_labels}/{N} annotated)")


# User input: label choice
current_label = st.session_state.labels[st.session_state.current_index]
st.radio('Are these two reports similar?', 
                    options= choices_values,
                    index = choices_position[map_value_to_choice[current_label]],
                    key = 'select_label',
                    on_change=change_label)

# User input: optional comments
st.text(st.session_state.comments[st.session_state.current_index])
st.text_area('Comment (optional)', 
                key='comment_area',
                value = st.session_state.comments[st.session_state.current_index],
                on_change=edit_comment_area)

# UI: Previous and next buttons to navigate across the pairs
#
prev_button = st.button('Previous', 
                        on_click=prev_pair, 
                        disabled=st.session_state.current_index == 0)
next_button = st.button('Next', 
                        on_click=next_pair,
                        disabled=st.session_state.current_index == N-1)

The labels part work well: when the user changes a label for item X, navigates to other items, then comes back to item X, they will find the last label they selected for X.

However the comments don’t work: when the user enters a comment it is correctly stored in st.session_state.comments and it can be printed by st.write, but even though the value arg in st.text_area is set to the right text, it does not show up in the text area as expected.

I noticed a few things: entering a comment for item X then another comment for the previous or next item and immediately going back makes the comment appear in the text_area for X. However navigating again in other comments makes both disappear, even though st.session_state.comments contains the correct value.

Am I missing something? How do I obtain the expected behaviour?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật