⚡️ Launched in 1.33: st.experimental_fragment

:calendar: Update April 5: @st.experimental_fragment was released in Streamlit 1.33! :tada:

Hi everyone! :wave:

The Streamlit team is excited to share st.experimental_fragment, a new feature released in Streamlit 1.33 that provides a whole new level of flexibility and control for your apps.

@st.experimental_fragment is a new decorator that turns any function into a “fragment” that can run independently of the wider page. Whenever an input widget inside the fragment changes, only the fragment reruns. This app introduces the feature and some common use cases.

A quick example

with st.spinner("Doing a slow thing"):
    time.sleep(2)

@st.experimental_fragment # Just add the decorator
def simple_chart():
    st.write("When you move the slider, only the chart updates")
    val = st.slider("Number of bars", 1, 20, 4)
    st.bar_chart(np.random.default_rng().random(val))

simple_chart()

This new decorator will turn any function into a Streamlit container that can run independently of the wider page.

partial_2charts

It will also include a run_every= kwarg that allows you to automatically rerun the function and update the app every N seconds.

partial_streaming

Want to learn more?

  • :video_game: View the demo app for more use cases and examples.
  • :open_book: Check out the docs article and API reference for more details. We’ll be adding more tutorials and examples in the coming days.
  • :rocket: Try it yourself: pip install streamlit>=1.33

Happy Streamlit-ing! :balloon:

25 Likes

This will get put to immediate use!! Thank you for sharing!
:+1: :+1: :+1:

3 Likes

This is very exciting!

Some naming ideas:

  • @st.isolate
  • @st.separate
5 Likes

Just wanted to add that with this, no more crazy hacks with st.session_states too!

3 Likes

Love this! It will make stream lit even more central in my toolbox.

Personally I think that there are two routes for the naming:

  • One is to simple and call it st.partial and call it a day: it might be not very particular but it contains the info needed.

  • Else, I think that st.echo or st.refresh or st.rerun might be a little more creative but still show quite well the point of the decorator

2 Likes

There is already a “partial” in the standard library, in the functools library. I don’t have a better name though… st.sublit?

1 Like

I would avoid the name partial due to commonly used concept of partial functions. I suggest st.standalone.

1 Like

On naming, how about:

isolated_rerun
rerun_inplace
rerun_this
local_rerun
selective_rerun

The use of two words further enhanced clarity.

1 Like

I placed a copy of streamlit-1.30.0-py2.py3-none-any.whl in my .streamlit folder and then did pip install streamlit-1.30.0-py2.py3-none-any.whl from that folder. Now when I the command streamlit --version I can see that it is now using Streamlit, version 1.30.0

When I try to run the Simple example code however, I get AttributeError: module 'streamlit' has no attribute 'partial'

What am I doing wrong?

1 Like

This is how I setup.

  1. Place the whl in project folder.
  2. Create requirements.txt and write the whl filename.
  3. Create virtual environment venv to not mess up with other library version when installed globally.
PS F:\Project\streamlit_isolated_rerun> python -m venv venv
  1. Activate venv.

Windows 10.

./venv/scripts/activate
  1. Update pip inside the venv.
(venv) PS F:\Project\streamlit_isolated_rerun> python -m pip install pip -U
  1. Install the libs in the requirements.txt.
pip install -r requirements.txt

That should do it.

2 Likes

Do some exploration and encountered an issue.

Executing a task outside of a decorated function while simultaneously interacting with it can lead to a RuntimeError.

I am aware of the current limitations, but it seems this is a different issue.

1 Like

Thanks @ferdy ! I agree, it looks like a slightly different issue. Will investigate. Not sure we will fix it for the whl file but it’s a good case to test for the final version!

1 Like

Thanks for the detailed instructions. Once I followed them closely, everything fell into place and started working. Isolated_rerun is a great feature!

1 Like

I also noticed the same RuntimeError when simultaneously interacting with the decorated function and executing a task.

1 Like

This is the most useful feature that immediately improved my app efficiency and adoption. Please release ASAP! Thanks for your advise and help, @mliu !

3 Likes

I can’t think of a more exciting name than this👇.
free_engine

Long-awaited API :yum:

2 Likes

Thank you for sharing! :+1: :grinning:

1 Like

Ticket Stream - Live Events Search Application Built with Streamlit → Powered by Ticketmaster

Ticket_Stream_Intro

Whether you’re planning a trip, looking to catch the next big game, or can’t wait for the next visit to your favorite venue featuring live local bands, you can find it all here with instant access to live event listings wherever you go!

Ticket Stream - Built with Streamlit → Powered by Ticketmaster

@jcarroll Putting great work, to work!!

3 Likes

I found an issue where the partial decorator is very inconsistent with normal operation. If an element on a partial-rerun is not rendered after an update, it will not be removed in the diff and will remain on the page. Here is a super simple script to reproduce:

import streamlit as st

@st.partial
def main():
    toggle = st.toggle('This toggle is bugged')
    
    if toggle:
        st.header('You should see "Toggle ON"')
    else:
        st.header('You should NOT see "Toggle ON"')
    
    if toggle:
        st.header('Toggle ON')

main()

When you remove the decorator, it works as expected. When you keep the decorator, the “Toggle ON” header will be rendered after you turn the toggle on and off again.


1 Like