# Bug (?) preventing iteration over displayed elements with recursive function

**URL:** <https://discuss.streamlit.io/t/bug-preventing-iteration-over-displayed-elements-with-recursive-function/9045>\
**Category:** Using Streamlit\
**Created:** [January 21, 2021, 3:51pm UTC](https://discuss.streamlit.io/t/bug-preventing-iteration-over-displayed-elements-with-recursive-function/9045 "2021-01-21T15:51:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulwcoles](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@paulwcoles](https://discuss.streamlit.io/u/paulwcoles)\
**Post date:** [January 21, 2021, 3:51pm UTC](https://discuss.streamlit.io/t/bug-preventing-iteration-over-displayed-elements-with-recursive-function/9045/1 "2021-01-21T15:51:39Z")

</div>

Hi! I’m trying to have page elements in an iterable display element by element until a user is done. I’ve implemented this with a recursive function, that I’ve simplified here:

```auto
def write_i(i):
    st.write(i)
    if i == 5:
        return
    if st.button('Next?', key=i):
        i += 1
        write_i(i)

```

If I call `write_I(0)`, I’d expect 0 to print and the button ‘Next?’ to appear then, on clicking, for 1 to appear and the button, and so on until 5 is printed.

It seems to work for the first two iterations: I see `0, Next?` then '1, Next?` – but on clicking that ‘Next’ button, the page starts again at 0.

Am I missing something? Thanks!

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/randyzwitch/32/58772_2.png) [@randyzwitch](https://discuss.streamlit.io/u/randyzwitch)\
**Post date:** [January 22, 2021, 3:34pm UTC](https://discuss.streamlit.io/t/bug-preventing-iteration-over-displayed-elements-with-recursive-function/9045/2 "2021-01-22T15:34:52Z")

</div>

Hi @paulwcoles, welcome to the Streamlit community!

This isn’t a bug per se, but a reflection of how the Streamlit evaluation model works. I’ve briefly written about that design decision here:

> [@Streamlit vs. Other Apps](https://discuss.streamlit.io/t/streamlit-vs-other-apps/7601/2):
>
> Hi @Bryson_Cale, welcome to the Streamlit community! Our launch article covers some of your questions: [https://towardsdatascience.com/coding-ml-tools-like-you-code-ml-models-ddba3357eace?source=friends\_link&sk=f7774c54571148b33cde3ba6c6310086](https://towardsdatascience.com/coding-ml-tools-like-you-code-ml-models-ddba3357eace?source=friends_link&sk=f7774c54571148b33cde3ba6c6310086) In short, the top-down model vs. the callback model of interaction really depends on the user’s background. As you mention, you’ve experienced the event-driven experience first, so it makes sense to you. In contrast, Streamlit hypothesized that “data sci…

What is happening is that within your recursive function, you are calling a Streamlit widget (`st.button`), which is then re-running Streamlit from the top of your script. So instead of getting the behavior you are expecting, you are seeing the code be re-run and the state being lost.

As part of our 2021 roadmap, we’re working on a robust state management functionality, but in the meantime, you can take a look at the following example about maintaining state between runs. If you persist your variable `i` in state across runs, I think you should get the behavior you are looking for.

> [@Multi-page app with session state](https://discuss.streamlit.io/t/multi-page-app-with-session-state/3074):
>
> Edit: Streamlit 0.84.0 has officially introduced session state. Go check it out: [Session State for Streamlit balloon](https://blog.streamlit.io/session-state-for-streamlit/) Hello there wave For a project I needed to build multi-page app with a settings page. As I’ve seen this [topic](https://discuss.streamlit.io/t/preserving-state-across-sidebar-pages/107) requesting the feature, I decided to share the solution I came up with. It features a custom version of @thiago’s [SessionState](https://gist.github.com/tvst/036da038ab3e999a64497f42de966a92). It still relies on hacks, but it should work in most cases. It was tested with Streamlit version 0.61.0 (and now 0.65). Things you must…

Best,  
Randy

---

<div class="post-metadata">

**Author:** ![paulwcoles](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@paulwcoles](https://discuss.streamlit.io/u/paulwcoles)\
**Post date:** [January 22, 2021, 3:49pm UTC](https://discuss.streamlit.io/t/bug-preventing-iteration-over-displayed-elements-with-recursive-function/9045/3 "2021-01-22T15:49:44Z")

</div>

That makes sense – I hadn’t thought about it as a stateless app. I’ll implement something similar to the SessionState class you referenced.

Thanks so much for the help – I’m new to it, but loving Streamlit!

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/randyzwitch/32/58772_2.png) [@randyzwitch](https://discuss.streamlit.io/u/randyzwitch)\
**Post date:** [February 7, 2022, 3:52pm UTC](https://discuss.streamlit.io/t/bug-preventing-iteration-over-displayed-elements-with-recursive-function/9045/4 "2022-02-07T15:52:34Z")

</div>


