streamlit: Exception raised when using multiselect with enums

Summary

Get “streamlit.errors.StreamlitAPIException: Every Multiselect default value must exist in options” when changing values in a multiselect based on a list of enums

Steps to reproduce

Code snippet:

import streamlit as st

from enum import Enum


class Colors(Enum):
    yellow = 1
    blue = 2


selected_colors = st.multiselect(
    "choose colors",
    list(Colors),
)

If applicable, please provide the steps we should take to reproduce the bug:

  1. Launch the streamlit
  2. Modify a value in the multiselect
  3. The exception should be raised in the logs

Expected behavior:

No exception should be raised

Actual behavior:

The streamlit exception “Every Multiselect default value must exist in options” is raised in the logs only (not the front).

Is this a regression?

No, I checked streamlit 1.3, 1.7, 1.10, and they had the same issue.

Debug info

  • Streamlit version: version 1.7.0
  • Python version: 3.9.11
  • Using PyEnv
  • OS version: Ubuntu 20.04
  • Browser version: Chrome Version 103.0.5060.53

Additional information

Only works with enums, not with value list.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Yep, that makes sense. If you’re using a DATAclass I think it makes sense to use cache_DATA (which serializes) rather than cache_resource (which doesn’t).

On Sep 20, 2023 at 14:04, Michael Petrochuk @.***> wrote:

I tried to quickly create a script and I do think cache_data works well. I’ve been using cache_resource where I have been running into this error:

“”" Reproducible script for dataclasses issue. Usage: $ streamlit run blah.py “”" import datetime import streamlit as st from bam import Data @st.cache_resource() def load(): return Data(1, 2) def main(): data = load() # NOTE: With cache_resource this assertion breaks, and that’s been tough to reason with. # # Traceback (most recent call last): # File “/Users/michaelpetrochuk/Code/Text-to-Speech/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 552, in _run_script # exec(code, module.dict) # File “/Users/michaelpetrochuk/Code/Text-to-Speech/blah.py”, line 24, in <module> # main() # File “/Users/michaelpetrochuk/Code/Text-to-Speech/blah.py”, line 20, in main # assert isinstance(data, Data) assert isinstance(data, Data) if name == “main”: main()

import dataclasses @dataclasses.dataclass(frozen=True) class Data: value: int other_value: int def refresh_1(): # NOTE: We add this function to trigger a streamlit refresh. pass

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>