When setting multiselect.key property, empty list is not accepted as an input

Summary

I want to set “key” property in multiselects, because it provides me the convenience related to session_state handling.
But, if the input to multiselect is empty, rendering the multiselect fails with the following error:

StreamlitAPIException: Every Multiselect default value must exist in options

Steps to reproduce

Code snippet:

facts = []
st.sidebar.multiselect(
    label="Facts:",
    options=[x.id for x in facts],
    key="selected_facts", # if I comment out this line, it works as expected
)

Expected behavior:

No error is returned.
The input is collected from a backend, it usually is not empty, but it may happen that it’s empty.
I expect the same multiselect declaration works for both scenarios.

Debug info

  • Streamlit version: 1.20.0
  • Python version: 3.10
  • Using PipEnv
  • OS version: Ubuntu 22.04
  • Browser version: Chrome latest

I can run your code snippet without issue.

image

I use windows 10.

The exception message tells us that if the default is defined, the value should be in the options.

Example:

facts = [1, 2, 3]
value = ['a']
st.sidebar.multiselect(
    label="Facts:",
    default=value,
    options=facts,
    key="selected_facts", # if I comment out this line, it works as expected
)

StreamlitAPIException : Every Multiselect default value must exist in options

That is expected as ‘a’ is not in facts.

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