Button triggered changes another variable

excuse me @everyone I wanna ask to you guys, why every time I do press for example a button with key ‘rand1’, it changes the value of another variable whereas that variable is only for button with key ‘rand2’? What should I do?


Hello,

I’m a Streamlit newb trying to answer your question. I’m here trying to help because, I asked a different question, and when I do ask for help, I try to help someone else. Anyways, back to your question.

First, I appreciate you adding your code to the question. However, in the future, could you please copy/paste the actual code? I don’t know about everyone out there, but it saves me the time of typing everything up myself. Also, it helps make sure I have your correct variable names, so when I copy back my reply, you should be able to easily copy it in without breaking to much. I’m assuming your ‘fav__1’ variable has two _ in it. So your code is:

import streamlit as st
import random

col1, buff, col2 = st.columns([2,1,2])

with col1:
    fav__1 = st.text_input('Fav1:', key='fav1')
    convert1 = list(fav__1.split(' '))
    random1 = random.choice(convert1)
    st.button("Submit", key='rand1')
    st.write('Favorite:', fav__1)
    st.write('fav_txt:', random1)

with col2:
    fav__2 = st.text_input('Fav2:', key='fav2')
    convert2 = list(fav__2.split(' '))
    random2 = random.choice(convert2)
    st.button("Submit", key='rand2')
    st.write('Favorite:', fav__2)
    st.write('fav_txt:', random2)

So to help you, technically speaking, each time you press a button on the page or change something (adding or removing text from the Fav1 field for example), your code runs from top to bottom. If you look at your code, your button doesn’t do anything really. What I mean but that, is you could call your button, “Monkey Farts”, and it would do the same thing as it is doing right now. Your button just tells Streamlit, “Hey I made change, update the page”, and everything runs. And by everything, I mean everything, the text from fav 1 and fav 2 is split, and then random.choice picks something, and then it is written as you asked in your st.write.

So, there a couple of things you can do. First, assign your st.button as a variable, like so:

submit1 = st.button("Submit", key='rand1')

Then add an if statement:

import streamlit as st
import random

col1, buff, col2 = st.columns([2,1,2])

with col1:
    fav__1 = st.text_input('Fav1:', key='fav1')
    convert1 = list(fav__1.split(' '))
    random1 = random.choice(convert1)
    sub1 = st.button("Submit", key='rand1')
    if sub1:
        st.write('Favorite:', fav__1)
        st.write('fav_txt:', random1)

with col2:
    fav__2 = st.text_input('Fav2:', key='fav2')
    convert2 = list(fav__2.split(' '))
    random2 = random.choice(convert2)
    sub2 = st.button("Submit", key='rand2')
    if sub2:
        st.write('Favorite:', fav__2)
        st.write('fav_txt:', random2)

But, this could cause issues later. I would instead suggest looking into using session states and creating functions like so:

import streamlit as st
import random

col1, buff, col2 = st.columns([2,1,2])

def fav1random(fav__1):
    convert1 = list(fav__1.split(' '))
    random1 = random.choice(convert1)
    if 'random1' not in st.session_state:
        st.session_state.random1 = random1
    elif 'random1' in st.session_state:
        st.session_state.random1 = random1

    return random1
    
def fav2random(fav__2):
    convert2 = list(fav__2.split(' '))
    random2 = random.choice(convert2)
    if 'random2' not in st.session_state:
        st.session_state.random1 = random2
    elif 'random2' in st.session_state:
        st.session_state.random2 = random2
    return random2

with col1:
    fav__1 = st.text_input('Fav1:', key='fav1')
    sub1 = st.button("Submit", key='rand1')
    if sub1:
        random1 = fav1random(fav__1)
        st.write('Favorite:', fav__1)
        st.write('fav_txt:', random1)

with col2:
    fav__2 = st.text_input('Fav2:', key='fav2')
    sub2 = st.button("Submit", key='rand2')
    if sub2:
        random2 = fav2random(fav__2)
        st.write('Favorite:', fav__2)
        st.write('fav_txt:', random2)

As I said, I’m a Streamlit newb, so there might be other ways to accomplish this. Also, just a side note, you can access your keys using session state. For instance, st.session_state.fav1 will display everything you have in the Fav1 field.

2 Likes

Wow, what a nice explanation and suggestion! Thanks for your kindness help. I also want to say sorry for my English because I’m not native and I use some of your words in google translate. I startled when got email with long explanation and lil bit think maybe I got blame because the way I ask something but you then reply with kindness, thanks man, you are really awesome.

Back to the code, I did the session state but couldn’t understand how to implement with the logic of session state. I’ve tried if else too but only able to write like this one, trying to use how to write session_state for my case:

"st.session_state object:", st.write(st.session_state)

#if 'key1' not in st.session_state:
#   st.session_state.key1 = #===>dont know what should I do the write with another code

fav1 = st.text_input('Masukan Favorit 1', key='key1', placeholder="Ketik apapun sesuai yang kamu favoritkan")
button = st.button("Click Me!")
if st.session_state['key_1'] == True:
   convertfav1 = Convert(fav1)
   randomfav1 = random.choice(convertfav1)
   st.write('Fav 1 = ', randomfav1)    
#but it didnt help because button2 and button3 did change randomfav1
def Convert(string):
    li = list(string.split(" "))
    return li
with st.form(key='first_fav'):
    fav1 = st.text_input("Masukkan Favorit 1 : ", key='update_value_1')
    convertfav1 = Convert(fav1)
    randomfav1 = random.choice(convertfav1)
    submit1 = st.form_submit_button(label='Submit', on_click=update_input_value)
with st.form(key='second_fav'):
    fav2 = st.text_input("Masukkan Favorit 2 : ", key='update_value_2')
    convertfav2 = Convert(fav2)
    randomfav2 = random.choice(convertfav2)
    submit2 = st.form_submit_button(label='Submit', on_click=update_input_value)
with st.form(key='third_fav'):
    fav3 = st.text_input("Masukkan Favorit 3 : ", key='update_value_3')
    convertfav3 = Convert(fav3)
    randomfav3 = random.choice(convertfav3)
    submit3 = st.form_submit_button(label='Submit', on_click=update_input_value)
st.write(submit1) #===> to check button true or false
st.write(submit2)
st.write(submit3)
if submit1 == True: #====> so I able to see what should I do here
    st.write("Favorite1 = ", randomfav1)
elif submit2 == True:    
    st.write("Fav2 = ", randomfav2)
elif submit3 == True:
    st.write("F3 = ", randomfav3)

but get stuck because I should learn how to analyze problem and logic and at last I realized that what should I do is just to made button different id and think that whenever I click the 1 should put in outputvariable1 and same for the button 2.

Once again, thanks CBrown, you really nice and made me want to improve myself more! Hope you always in health and blessed.

Dear CBrown, I already tried your code and it still triggered each other

but I checked this one how to understand simple session_state : Python In Office - Session State

Anyway, thanks CBrown