How to pause a simulation app and resume from where you stopped

I am trying to add full functionality to program. M app runs a simulation. I would like to be to pause the simulation and resume the simulation from the last point.

I stumbled on this post: Stop or Cancel button

How can I implement a pause button that pauses my simulation at the last point and resumes from th last point when I click the resume button?

Additionally, I get the following error: ModuleNotFoundError: No module named 'SessionState'
when using he SessionState. I tried to install it but no successs

Kindly advise please


Hi @JohnAnih,

Do you have SessionState module in your path ?

I usually put the session state in a utils module and import it from there. You can take reference from this repo,
Here the _SessionState class resides in and I import it from there in

Hope it helps !

PS. the repo is compatible with streamlit 0.63.1 for latest version change the import in like following,

from streamlit.ReportThread import get_report_ctx
# change that 👆 to this 👇
from streamlit.report_thread import get_report_ctx

from streamlit.server.Server import Server
# change that 👆 to this 👇
from streamlit.server.server import Server

Thank you very much. How do you recommend I pause the simulation?

Can you share a minimal example of what you are trying to do ?

Based on some assumptions like you want to run your simulation with some termination condition and want a button to start/stop and resume from the last state, I came up with this,

Not the most elegant solution by far but gets the job done.

import streamlit as st
from time import sleep
from state import provide_state

import numpy as np

def main(state):
    # initial conditions
    state.iter = 0 if state.iter is None else state.iter = [] if is None else

    # repopulate chart on next run with last data
    chart = st.line_chart()
    for data in

    stop = st.checkbox("Stop Update")

    # iteration for simulation
    while state.iter < 100:
        if stop:
        # update the simulated chart
        data = np.random.randn(10, 2)
        # keep last iteration of simulation in state
        state.iter += 1


This code produces this,

1 Like

I am getting error: **ModuleNotFoundError** : No module named 'state'
will share a minal working example shortly

I have the SessionState in my path FYI

provide_state is a decorator that I am using from this file,

You can just save this file locally and change imports accordingly.

1 Like

okay thanks. Is it a must the functionality is wrapped into a function?

Here is a snipnet of my code:


pause_simulation = st.sidebar.checkbox('Pause Simulation')

state = SessionState.get(pid=None)


if pause_simulation:

    while pause_simulation== True:

        p = psutil.Process(

        p.suspend() =

        st.write("paused process with pid: ",



        st.write("resumed state with pid:",

This actually pauses the program but fails to resume the program.