StreamlitAntdComponents icon indicating copy to clipboard operation
StreamlitAntdComponents copied to clipboard

Model change

Open CHerSun opened this issue 2 years ago • 0 comments

Hi.

First of all, thanks a lot for this project. I really like Ant.D components and Streamlit lacks some capabilities there. This really helps. You did a huge, valuable job there.

Back to the model. Currently you use specific items with exact specific structure. If items are modified (like, inherited from and added fields) - the code crashes. Also, you return either string label or index for selected item. This isn't too convenient.

Streamlit way about that is to accept a collection of any items, apply format_func to them to get labels for rendering, and return back the selected item itself. And it's really quite neat. I understand, that for things like hierarchical menu it's quite difficult to accept arbitrary items, so dict / MenuItem looks more than reasonable. But it requires quite a lot of boiler plate code to first populate wanted menu with all data, then to convert that to MenuItem, then to get selected index, convert that back from a tree, convert that back to original item just to get related data / view.

My use case just in case: hierarchical menu with a view (function) attached and permissions check (also a function). Permissions are checked before forming final menu, removing anything user shouldn't see. And a view is what I need later, when user selects specific MenuItem - I just run that function to render everything.

Could you consider changes to the current model:

Minimal:

Add payload to your items (like MenuItem), which you don't touch at all and ignore it when serializing things to JS. This way we can add any extra info we want right into your objects to be readily fetched later without ruining current serialization. This lifts the need to convert objects and keeping track on that.

Preferred:

Consider if maybe you could return actual MenuItem (and other related objects, like TreeItem etc) for the selected item. Maybe as an option on what to return, like Literal['label']|Literal['index']|Literal['item'] = 'label' by default (to keep backwards compatibility as much as possible). Along with payload that would remove the need to identify the item in the original collection.

CHerSun avatar Jul 27 '23 07:07 CHerSun