Event-driven Streamlit (no reruns)

I have been using Streamlit for a long time, since v1.34 (maybe even earlier). It has benefited my personal projects and several of my company’s internal projects a great deal. In larger apps, however, the rerun model carries a real cost: every interaction re-executes the script, and in scenarios that require high real-time responsiveness while containing a large number of components, that cost becomes hard to ignore.

At first I worked around this by splitting pages and doing local optimizations, but that became increasingly hard to sustain. I also evaluated NiceGUI, but it was not the right fit for my use case.

Turning Streamlit from rerun-based into event-driven is a huge engineering effort, almost impossible for one person, so the idea I had since last year progressed on my own but very slowly – until I started using the AI Agent. Judging from the recent GitHub commit frequency, I succeeded.

It took me about a month (most of the work was actually done in the first week) to implement this feature. You can find the GitHub repository here: streamlit-canary (and the online user manual).

Here are two screenshots of products built on top of it:

The code of a typical Streamlit Canary app:

# demo_click_counter.py
import streamlit_canary as sc
from streamlit_canary import components_v3 as v3

class _State(sc.StateV2):
    def __init__(self):
        self.count = sc.Property(0)

state = _State()

def main():
    v3.PageTitle('Click Counter', default_theme='dark')
    with v3.Row(vertical_alignment='center'):
        with v3.Text(
            sc.bind(state.count, lambda x: 'Click count: {}'.format(x))
        ):
            pass
        with v3.Button('Increase counter', type='primary') as btn:
            @btn.on_click
            def _increase_counter():
                state.count.set(state.count.get() + 1)
                # or a shorthand is also supported: state['count'] += 1

if __name__ == '__main__':
    sc.run(main, port=2201)
1 Like