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}")
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?
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.