How to apply st.slider to function

Hey @riyo_santo_yosep

There is a lot of code in your repository so it is really hard to understand the problem that you are facing.

Here is the documentation for the slider widget that might help you - st.slider - Streamlit Docs.

If this doesn’t cover your query, I would love to hear more about the problem you are facing. Just reply to this message explaining your problem in detail with any line of code that you have written (focusing on that problem only).


thanks for responding, I have summarized my code, I want to call the slider function and display it in the app function, but there is an error ‘DuplicateWidgetID’

def sidebar():
  # Sidebar - Specify parameter settings
  with st.sidebar.header('2. Set Parameter'):
    split_size = st.sidebar.slider(10, 90, 80, 5)
    number_of_features = st.sidebar.slider(5, 47, 20, 5)
    parameter_n_estimators = st.sidebar.slider(10, 100, 50, 10)
    neighbor = st.sidebar.slider(11, 101, 55, 11)
  return split_size, number_of_features, parameter_n_estimators, neighbor

def load_data():
  df = pd.read_csv('data/data_praproses.csv')
  return df

def choose_feature(df, number_of_features):
  return new_feature

def standarization(new_feature):
  return column_selection

def split(df, column_selection, split_size):
  return X_train, X_test, y_train, y_test

def naive_bayes(X_train, X_test, y_train, y_test):
  return matrik_nb, cm_label_nb, nb

def app():
  if 'data_praproses.csv' not in os.listdir('data'):
    st.markdown("Please upload data through `Home` page!")
    if st.sidebar.button('Train & Test') or st.session_state.load_state:
      st.session_state.load_state = True
      split_size, number_of_features, parameter_n_estimators, neighbor = sidebar()
      # Choose feature
      new_feature = choose_feature(df)
      # Standarization
      # Split data
      X_train, X_test, y_train, y_test = split(column_selection, split_size)
      # Naive Bayes
      matrik_nb, cm_label_nb = naive_bayes(X_train, X_test, y_train, y_test)

      st.write("2a. Naive Bayes report using sklearn")
      st.text('Naive Bayes Report:\n ' + matrik_nb)
      sns.heatmap(cm_label_nb, annot=True, cmap='Blues', fmt='g')
      st.set_option('deprecation.showPyplotGlobalUse', False)

Okay, I understand it now.

So you need to add a unique key argument to your st.slider or any other Streamlit widget when you want to use multiple of the same widget in an app.
The reason is that streamlit needs to differentiate between multiple occurrences of the same widget.

Additionally, I believe you want to give your users an option to select a fixed set of values instead of any value in a given range. For that select_sliders are used. They are like a combination of sliders and a selectbox.

Here is the code for that -

split_size = st.sidebar.select_slider(label="Split Size", options=[5, 10, 80, 90], key='split_size')

And this is how it looks -


1 Like

Per the documentation that @Kanak linked, you need to add an key argument to each of the lines I quoted above, so that Streamlit can keep track of each internal widget.



thank you sir, I understand

thanks sir, by the way, would you like to solve my other problem ?

thanks sir, i understand by the way, would you like to solve my other problem ?