Skip to content

Multi select item cannot be ordered. #79

@pmareke

Description

@pmareke

Checklist

  • I have searched the existing issues for similar feature requests.
  • I added a descriptive title and summary to this issue.

Summary

There is a limitation in the Multi Select item as the class needs to have a set (which order is not guaranteed in Python).

If you pass a class (ExampleModel) with a list in the default property using the pydantic Field class, Streamlit works like a charm, but if you pass an instance (ExampleModel (...)) with a list of values for the set (something like a pre-defined sublist of all the possible values) the list does not have the same order as the one in the instance.

Why?

It should be possible to render the component with a list of values in the desired order.

How?

from enum import Enum
from typing import Set

import streamlit as st
from pydantic import BaseModel, Field

import streamlit_pydantic as sp


class SelectionValue(str, Enum):
    FOO = "foo"
    BAR = "bar"
    BAZ = "baz"

    @staticmethod
    def all() -> list:
        return sorted([item.value for item in SelectionValue])


class ExampleModel(BaseModel):
    multi_selection: Set[SelectionValue] = Field(
        ...,
        description="Allows multiple items from a set.",
        default=SelectionValue.all()
    )

model = ExampleModel(multi_selection=[SelectionValue.BAR, SelectionValue.FOO])
data = sp.pydantic_form(key="my_form", model=model)
if data:
    st.json(data.model_dump_json())

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:needs-triageHas not been triaged yettype:enhancementRequests for feature enhancements or new features

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions