How to use session_state in checkbox

how to implement st.session state in this code for reset check in checkbox when the button diagnosa clicked?`

train_data = pd.read_csv(‘data_setfix.csv’)
train_features = train_data.iloc[:, :-1].values
train_labels = train_data.iloc[:, -1].values

column_names = train_data.columns.tolist()

num_columns = 3
gejala = [column_names[i:i+num_columns] for i in range(0, len(column_names)-1, num_columns)]

input_data = []
for col_group in gejala:
    cols = st.columns(len(col_group))
    for i, col_name in enumerate(col_group):
        input_data.append(cols[i].checkbox(col_name))

input_array = np.array(input_data, dtype=np.float64).reshape(1, -1)

k = 5
classifier = FuzzyKNN(k=k)
classifier.fit(train_features, train_labels)

if st.button('Diagnosa'):
    predictions = classifier.predict(input_array)
    result = predictions[0]
    st.subheader(f"Hasil diagnosa untuk gejala yang dimasukkan: Diagnosa = {result}")

`

hi @Iqbal_Kurriana_Putra

You can use st.session_state to store and manage the state of your Streamlit app. In your case, you want to reset the checkbox states when the “Diagnosa” button is clicked.

import streamlit as st
import pandas as pd
import numpy as np
from fuzzy_knn import FuzzyKNN  # Assuming you have a fuzzy_knn library

# Load data
train_data = pd.read_csv('data_setfix.csv')
train_features = train_data.iloc[:, :-1].values
train_labels = train_data.iloc[:, -1].values

column_names = train_data.columns.tolist()

num_columns = 3
gejala = [column_names[i:i+num_columns] for i in range(0, len(column_names)-1, num_columns)]

# Function to reset checkbox states
def reset_checkboxes():
    st.session_state.checkbox_states = {col: False for col in column_names[:-1]}

# Initialize checkbox states in session state
if 'checkbox_states' not in st.session_state:
    reset_checkboxes()

# Display checkboxes
input_data = []
for col_group in gejala:
    cols = st.columns(len(col_group))
    for i, col_name in enumerate(col_group):
        state_key = f"{col_name}_checkbox"
        checked = st.checkbox(col_name, key=state_key, value=st.session_state.checkbox_states[col_name])
        input_data.append(checked)
        st.session_state.checkbox_states[col_name] = checked

# Convert input to array
input_array = np.array(input_data, dtype=np.float64).reshape(1, -1)

# Fuzzy KNN Classifier
k = 5
classifier = FuzzyKNN(k=k)
classifier.fit(train_features, train_labels)

# Diagnose button
if st.button('Diagnosa'):
    predictions = classifier.predict(input_array)
    result = predictions[0]
    st.subheader(f"Hasil diagnosa untuk gejala yang dimasukkan: Diagnosa = {result}")
    # Reset checkbox states
    reset_checkboxes()

Thank you for your answer, it was very helpful. However, I have a new issue. Before I implemented your answer, the display on the diagnosis menu was divided into 3 lines containing symptoms and checkboxes. After implementing your answer, the display on the diagnosis menu became one line. I want it to be like the initial 3 lines. Do you have a solution for this?

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