streamlit / streamlit

Streamlit — A faster way to build and share data apps.
https://streamlit.io
Apache License 2.0
35.46k stars 3.08k forks source link

Time or event (websocket/redis) callbacks #2927

Open schaumb opened 3 years ago

schaumb commented 3 years ago

Problem

I want to create outside trigger event changes (like timeout, websocket connection, redis subscribed channel, other streamlit app) without owning the report thread, and without blocking user interactions (buttons, checkboxes etc.) and with streamlit's low code mindset

Solution

Preferred solution:

We created a solution here: https://github.com/FloWide/streamlit_callbacks

with simple usages:

from streamlit.callbacks.callbacks import later, rerun
import streamlit as st
from datetime import datetime

st.write(datetime.now())

later(2.0, rerun) # After 2 sec calls a rerun
import streamlit.callbacks.websocket as ws
import streamlit as st

print_message = st.empty()
def msg_handler(msg):
  print_message.write(f"Arrived message from ws: {msg}")

ws.on_message('ws://localhost:10000', msg_handler)

More examples at README.md


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.

kmcgrady commented 3 years ago

Hey @schaumb Thanks for the report!

That seems like a really interesting idea. Our product has a large backlog of enhancement requests to go through, but I will make note of this enhancement request for them to consider.

randyzwitch commented 3 years ago

Hi @schaumb -

In version 0.69, we merged a community PR from @SimonBiggs called st.experimental_rerun, which might do your first use case.

Best, Randy

schaumb commented 3 years ago

Hi @randyzwitch,

Thanks, I know that function exist. The callbacks rerun is for another reason. st.experimental_rerun asks runner thread for stop the code, and do it again. callbacks.rerun is runs on callbacks thread which will delegate a rerun after a condition is met, so It is made for another reason.