Documentation on caching behaviour
Where can i find documentation / code examples on how solara handles component caching?
I noticed that sub components are not re-rendered when their parent is re-rendered if their arguments do not change and if they are in the same order. But if the arguments or the order changes, they get redrawn
I am rendering a list of components, to which i need to add in random locations. When adding to the bottom it works great, only rendering the new one, but when adding in other locations it forces a re-render of the whole list.
How can i explicitly control which elements are redrawn?
4 Replies
Hey @pnjun! You should be able to achieve this using
.key()
on the components. Then as long as the key and the arguments don't change the component won't be recreated. Something like:
Feel free to play around with this py.cafe examplePyCafe - Solara - Solara Random Card Generator
Create & Share Streamlit, Dash and Python Apps Online.
Thanks! I played around with it and it seems that it only works when the .key() is there, even if the arguments are not changed.
https://py.cafe/snippet/solara/v1#c=H4sIABGNF2cAA41SUWvbMBD-K5r24jDXNC1hYAiMlsFeBqMb20NdimKda1FZ8iQ5qQn-7z1ZcpPQpqn9YJ_uu--7-3RbWmoONKeiabVxxGrJDCtUDA1TXDeF8u-3kMtKjTkFyhWKQ0VuRsg1MzwR_GmWF4rgU2rlEEKWkSILn2QW0hvh6qiVjaUFDTwFnRj8ExF_4MklVUE9kmxRZUgngZxs49-Ape82-os9QDKxrzrntLovdTc2GSs6C_cGWOnEGpLL2Ks2HMwRjBTWJTgaMu8zZmsmO5jNxoY8h9e3dVdVctfCC3dmYSTxNlnWtIgJ5yNLSiSo_YM92kobIohQkWdM77HvX80se4QeP7vs54P0RBmnvBqnwWv5qQ2QENlPBfXGS22WBW2NaJjp_ZEfW4rycSlZs-IsP3B3nO61OeQLmftB3tL8HZw6pRYNRRKaUgP_O2Ggweu2uM7THmPG9a1f8HCCMWvbvwI2NK-YtJBS4MJ9V2wlEeUMGk7b3tVaYU3bay44nK3Ps4tFNsdiyXrdOZpv6RqMFR51geJauxuNlNtJzWCU0rIWkhtA0O1LxrEVOoLJjeCupvl8cZ7SRqh_IbwM0Q8QDzXq-FBwLKuEhCtktWCuceGZUGCOKHjo2SpgEdIyz0vpcDekr7s40uKuDu3K2v6w-gPTnZ4odnhqmmmQt9uvI-v86-Ijmg4MHjN5SnTCeVX_DunoKm7W7d3wDEoZiTY0BQAA
PyCafe
Playground for Python web frameworks. Run and edit Python code snippets for web frameworks in a web browser.
Could you explain how the .key() works? is there some documentation / link to the source i can have a look to?
Thanks a lot!
Good to hear. I've added some print statements to your code to validate that everything works as expected:
https://py.cafe/maartenbreddels/solara-key-usage
We don't have docs on this yet, you can read about how this works with ReactJS until then
PyCafe - Solara - Random Card Generator
Create & Share Streamlit, Dash and Python Apps Online.