Multiple number_input but only the last one created works normal

import streamlit as st

if not st.session_state.get('total'):
    st.session_state['total'] = 10
if not st.session_state.get('a'):
    st.session_state['a'] = 1
if not st.session_state.get('b'):
    st.session_state['b'] = 1

def update_remain_point(n):
    print(f'cur_{n}')
    if f'cur_{n}' in st.session_state:
        cur_point = st.session_state[f'cur_{n}']
        new_total = cur_point - st.session_state[n]
        st.session_state['total'] -= new_total
        st.session_state[n] = cur_point

st.write('remain:', st.session_state.get('total'))
for n in ['a', 'b']:
    st.session_state[n] = st.number_input(n, min_value=1.0, max_value=10.0, step=1.0, on_change=lambda: update_remain_point(n), key=f'cur_{n}')

It looks like this. What I want to achieve is

  1. every time I click + button of a or b, the remain will
    reduce 1
  2. every time I click - button of a or b, the remain will
    add 1

In this example, only when I click + and - of b works.
You can see I do print(f'cur_{n}') in the code and I found that no matter I click + and - of a or b, it always print cur_b.

Why does this happen? I think when I click a's + and -, it should print cur_a.

Seems like the last number_input kills all other number_input. Plz help me with this. Thank u guys.

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