I’ve created a custom component (3d model viewer) that uses useReducer to manage its state. I’d like to send events to the component that would be interpreted as a reducer action. Through this method I’d be able to create a python api for manipulating the view after creating the component and loading the data.
# pseudo code / not a python pro
def st_viewer_clear(st_viewer):
st_viewer.dispatch( { "type": "remove-all" } )
It seems possible to trigger an event in the component the same way Streamlit sends events that are handled by Streamlit.onMessageEvent().
I can register an event listener from within my component but I can’t figure out how to emit a new event from python. Is this possible?
The Python page needs to rerun, loading the component afresh which will send over any new initializer prop values. You should pass these prop values as session state K-V pairs so they survive the rerun.
[HACK] Otherwise, have your component periodically poll an external service that serves as a command dispatch queue. Your Streamlit app can push commands into this queue, which the component will pick up and execute. I’ve used Airtable and FastAPI microservices for this purpose.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.