Skip to content

Multiselect check for defaults goes wrong if not a list #1397

@arnaudmiribel

Description

@arnaudmiribel

Summary

When using st.multiselect(), there's an error being thrown out if default values are given in a tuple instead of a list. See additional info for a solution. I'm happy to do a PR if you're ok with my point.

Steps to reproduce

  1. Create an app.py with the following:
import streamlit as st
INTENSITIES = ("a", "b", "c")
st.multiselect(
    label="Choose among:",
    options=INTENSITIES,
    default=INTENSITIES,
)
  1. Now run streamlit run app.py.

Expected behavior:

The code above should work and print the following:

image

It does work if given list objects as in:

st.multiselect(
    label="Choose among:",
    options=list(INTENSITIES),
    default=list(INTENSITIES),
)

Actual behavior:

Error shows:

StreamlitAPIException: Every Multiselect default value must exist in options

Is this a regression?

Not sure about this. Didn't try before.

Debug info

  • Streamlit version: 0.58.0
  • Python version: Python 3.6.9 :: Anaconda, Inc.
  • Using Conda
  • OS version: macOS Mojave 10.14.6
  • Browser version: Google Chrome 81.0.4044.92

Additional info

Code responsible seems to be here when casting to list:

if not isinstance(default_values, list):
                default_values = [default_values]

Should rather be:

if not isinstance(default_values, list):
                default_values = list(default_values)

Otherwise, in the tuple case, ("a", "b", "c") is converted to [("a", "b", "c")] hence the error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions