# Make value of multiple number inputs dependent of each other

Hi!

I have multiple number inputs - for example 3 with default values of 25, 35 and 40 so they add up to 100. My goal is, that if the user for example changes the 25 to 30 that the addional 5 will be deducted equally from 35 and 40 → 32.5 and 37.5 so that the total is 100 again.

I have tried mutliple things but nothing comes even close to a solution.

Any ideas how to solve that?

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

Thank you very much for your time and help! This is a great solution and exactly what I was looking for!