Make value of multiple number inputs dependent of each other

Here’s a related post Multiple sliders with dependant values

More to your question, here’s an example (st.session_state aliased as ss). I used number inputs instead of sliders since I need a bit more code to throttle changes made by sliders or make it more robust. So here’s just an example of the mechanics:

import streamlit as st
from streamlit import session_state as ss

TOTAL = 100

def update(last):
    change = ss.A + ss.B + ss.C - TOTAL
    sliders = ['A','B','C']    
    last = sliders.index(last)
    # Modify to 'other two'
    # Add logic here to deal with rounding and to protect against edge cases (if one of the 'others'
    # doesn't have enough room to accomodate the change)
    ss[sliders[(last+1)%3]] -= change/2
    ss[sliders[(last+2)%3]] -= change/2


st.number_input('A', key='A', min_value=0, max_value=100, value = 50, on_change=update, args=('A',))
st.number_input('B', key='B', min_value=0, max_value=100, value = 25, on_change=update, args=('B',))
st.number_input('C', key='C', min_value=0, max_value=100, value = 25, on_change=update, args=('C',))
1 Like