One button triggers other buttons

Summary

I do have a main loop that receives decodes CAN messages and I am trying to use streamlit to display them. Additionally I am trying to add 3 buttons that will send specific CAN messages however I am running into problems. So I run the program and click button1, button2, and button3 one after the other. Button1 activates when I press button1, but then button1 and button2 activates after I press button 2 and all three buttons activate after I press button3 and every consecutive button press after that activates all three regardless of which button I press.

I am attaching a minimal working example. Any help would be appreciated.

Steps to reproduce

Code snippet:



import time  # to simulate a real time data, time loop

import numpy as np  # np mean, np random
import pandas as pd  # read csv, df manipulation
import streamlit as st  # 🎈 data web app development
    

st.set_page_config(
    page_title="Real-Time Data Science Dashboard",
    page_icon="βœ…",
    layout="wide",
)

dataset_url = "https://raw.githubusercontent.com/Lexie88rus/bank-marketing-analysis/master/bank.csv"

@st.cache_data
def get_data() -> pd.DataFrame:
    return pd.read_csv(dataset_url)

df = get_data()

st.title("Real-Time / CAN Dashboard")


st.markdown("Inputs")
slid1, slid2, slid3 = st.columns(3)
with slid1:
    cutoff = st.slider('Charge Cutoff Voltage:', 11.8, 12.8, 12.8)
    
but1, but2, but3 = st.columns(3)

with but1:
    button1 = st.button("Set Cutoff", "_set_cutoff")

with but2:
    button2 = st.button("ON Signal", "_on_signal")
        
with but3:
    button3 = st.button("OFF Signal", "_off_signal")
    
placeholder = st.empty()

df = df[df["job"] == "admin."]


# near real-time / live feed simulation
for seconds in range(200):

    df["age_new"] = df["age"] * np.random.choice(range(1, 5))
    df["balance_new"] = df["balance"] * np.random.choice(range(1, 5))

    # creating KPIs
    avg_age = np.mean(df["age_new"])

    count_married = int(
        df[(df["marital"] == "married")]["marital"].count()
        + np.random.choice(range(1, 30))
    )

    balance = np.mean(df["balance_new"])

    with placeholder.container():

        col1, col2, col3 = st.columns(3)
        with col1:
            if button1:
                st.write(str(cutoff) + "V was sent")
                button1 = False
                print("button1")
            else:
                st.write("...")
                
        with col2:
            if button2:
                st.write("ON signal was sent")
                button2 = False
                print("button2")
            else:
                st.write("...")
                
        with col3:
            if button3:
                st.write("OFF signal was sent")
                button3 = False
                print("button3")
            else:
                st.write("...")

        
        kpi1, kpi2, kpi3 = st.columns(3)
        kpi1.metric(
            label="Metric 1",
            value=round(avg_age),
            delta=round(avg_age) - 10,
        )
        
        kpi2.metric(
            label="Metric 2",
            value=int(count_married),
            delta=-10 + count_married,
        )
        
        kpi3.metric(
            label="Metric 3",
            value=f"$ {round(balance,2)} ",
            delta=-round(balance / count_married) * 100,
        )
        time.sleep(1)

I run this file as streamlit run main.py and click the buttons one by one and see their effects in command line.

Expected behavior:

I print button number for every button press. I should be seeing button1 in the command line when i press button 1 as well as button2 and button3 for the other two buttons.

Actual behavior:

The first button I press works as expected but the second button press activates both the first and the second button, then the consequent presses activate all buttons.

Debug info

  • Streamlit version: 1.26.0
  • Python version: 3.11.4
  • Using Conda
  • OS version: Windows 10
  • Browser version: Firefox 117.0.1

Hi @kefecik, with regard to the buttons, see if this can help you: Streamlit (extras.streamlit.app)

Cheers

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