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

Thanks

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, https://github.com/ash2shukla/streamlit-heroku
Here the _SessionState class resides in utils.py and I import it from there in main.py.

Hope it helps !

PS. the repo is compatible with streamlit 0.63.1 for latest version change the import in utils.py 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


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

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

    stop = st.checkbox("Stop Update")

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

main()

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, https://github.com/ash2shukla/streamlit-heroku/blob/be0fd199cd772c7241f0aac1f59c626923c0df11/src/utils.py#L95

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:

st.sidebar.title("Controls")

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

state = SessionState.get(pid=None)

    

if pause_simulation:

    while pause_simulation== True:

        p = psutil.Process(state.pid)

        p.suspend()

        state.pid = p.pid

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

    else:

        p.resume(state.pid)

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

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