okld / streamlit-elements

Create a draggable and resizable dashboard in Streamlit, featuring Material UI widgets, Monaco editor (Visual Studio Code), Nivo charts, and more!
MIT License
679 stars 77 forks source link

mui.Select callback event doesn't send information #26

Open cardosofede opened 1 year ago

cardosofede commented 1 year ago

Hi, I just started with this library today and it's amazing!

This is the code that I'm using in a custom card to start strategies in Hummingbot:

with mui.Select(label="Scripts", onChange=lazy(lambda x: self.set_strategy(x, bot_name))):
  for script in scripts:
      mui.MenuItem(script, value=script)

and this is the method set_strategy:

@staticmethod
    def set_strategy(event, bot_name):
        st.session_state.active_bots[bot_name]["selected_strategy"] = event.target.value

When I press another button that triggers the execution I'm receiving this error:

  File "/Users/dardonacci/Documents/work/dashboard/ui_components/bot_performance_card.py", line 66, in <lambda>
    with mui.Select(label="Scripts", onChange=lazy(lambda x: self.set_strategy(x, bot_name))):
  File "/Users/dardonacci/Documents/work/dashboard/ui_components/bot_performance_card.py", line 15, in set_strategy
    st.session_state.active_bots[bot_name]["selected_strategy"] = event.target.value
  File "/Users/dardonacci/anaconda3/envs/dashboard/lib/python3.9/site-packages/streamlit_elements/core/callback.py", line 138, in __getattr__
    return self.__getitem__(value)

I also inspected the event that arrives in the function and is the dict --> {"isTrusted":false}

I don't know react so probably I'm missing something in how to use mui.Select, any help is welcome!

chrda81 commented 1 year ago

I had the same problem and found the answer mentioned here https://github.com/okld/streamlit-elements/issues/2

I have a TextField with the select=True option and can access the value by using onChange=sync(None, 'select_value'). The value from the 'select_value' returning dict can then be accessed by .props.value

if st.session_state.select_value is not None:
    # Select must use the second sync var
    select_value = st.session_state.select_value.props.value

I hope this helps for the mui.Select too, or you switch to the TextField with select option.