Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update components via session_state #5690

Open
coolkau opened this issue Nov 11, 2022 · 7 comments
Open

Update components via session_state #5690

coolkau opened this issue Nov 11, 2022 · 7 comments
Labels
area:widgets feature:state type:enhancement Requests for feature enhancements or new features

Comments

@coolkau
Copy link

coolkau commented Nov 11, 2022

Problem

IT seems that the overall code style is to provide a default value to the widget or component. Then, after the first run, the value can be updated via the relevant key in the session_state.

Ie. I update st.session_state["widget_key"] via a callback. Then the widget will show the updated value

This works with widgets. However, it is not clear how to achieve this with components. Components dont change the value, they will be out of sync with the session state.

I looked around none of the community components will upate; none of them is able to interact with other widgets via callbacks/ session_State. One widget solved this by having an explicit value parameter to update; this works but might not be the "streamlitty" way of doing things.

Solution

Provide a way to pull the new stession_state into the react state upon re-rendering. If this exists, provide documentation or tutorials for this, as I did not manage to get this to work and it seems nobody else did (At least I did not find any example for this).


Community voting on feature requests enables the Streamlit team to understand which features are most important to our users.

If you'd like the Streamlit team to prioritize this feature request, please use the 👍 (thumbs up emoji) reaction in response to the initial post.

@willhuang1997 willhuang1997 added type:enhancement Requests for feature enhancements or new features area:widgets labels Nov 11, 2022
@willhuang1997
Copy link
Collaborator

Hi @coolkau ,

Is this the same as this issue? It does seem to be but can you confirm for me?

#3977

@coolkau
Copy link
Author

coolkau commented Nov 11, 2022

Nope its not,
the other one is adressing the issue that custom components dont have a callback. (also a big wish but there is a simple hack in that issue :) )

Mine is pretty much a bug. Meaning if you use callbacks to update the session-state key, the custom component with that key does not update. At that point the app is essentially broken.

def callback():
     st.session_state["key"] = False

component(detault_value=True, key="key)

If the callback triggers, the component value does not update to False. This is true for all components I downloaded; I tried to play a bit to see if I can set the react_state to some prop, but did not see anything which looked like something useful.

You basically need to decide if you

  • either want to add the ability to pass a new session_state into the react component (or create enbablement how to do this),
  • or remove the key property from components -- and thus removing the ability to interact via callbacks / the session state.

(Unless I am missing something strikingly obvious, which might be the case as well)

@carolinedlu
Copy link
Collaborator

Hey @coolkau, is this essentially the same as #5634?

@coolkau
Copy link
Author

coolkau commented Nov 14, 2022

Nope, also that one is different. Let me know if I should elaborate.

@willhuang1997 willhuang1997 added type:bug Something isn't working type:enhancement Requests for feature enhancements or new features and removed type:enhancement Requests for feature enhancements or new features type:bug Something isn't working labels Nov 15, 2022
@willhuang1997
Copy link
Collaborator

@coolkau , so if I understand correctly, it seems you want to have custom components be able to update their session state value just like widgets and this currently doesn't work? Let me know if I understand it properly or where I am going wrong.

@coolkau
Copy link
Author

coolkau commented Nov 15, 2022

No, let me try this again:
There are two states, the "backend one", the one from streamlit, the sessoin_state. And the frontend one, which is in the iFrame, in the React Component. Those should be in sync.

Normally you would have a 2 way connection beween the the central store (e.g. redux) and the widget.
In the case of streamlit you push a change in component state back to streamlit, which then triggers a rerun of the entire script.

In theory you dont need to push a change of the steamlit change to the componented state. You should get a new component after every change/rerun. However it seems that not the entire component is wiped when rerunning. This means the session state changes, but the component still has the state of the earlier run. I tried to find a way how to get updates from the steamlit state but did not find a way

@nicedouble
Copy link

+1 for this issue.Hope streamlit can publish v2 custom component api,so that can change custom component value by session state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:widgets feature:state type:enhancement Requests for feature enhancements or new features
Projects
None yet
Development

No branches or pull requests

5 participants