Tidying up controls#

When you have lots of sliders and other input widgets it is good practice to make use of a sidebar. This will organise the interactive app a bit more cleanly.

In some cases the simulation model may also take a few minutes to run. In these instances it is helpful to provide some feedback to the user that the model is running in the background.

1. Adding a sidebar#

We can move the sliders and other inputs to a sidebar using a python with block ans the st.sidebar command:

Note a with block requires code within it to be indented.

with st.sidebar:

    # set number of resources
    n_operators = st.slider('Call operators', 1, 20, 13, step=1)
    n_nurses = st.slider('Nurses', 1, 15, 9, step=1)

    # set chance of nurse
    chance_callback = st.slider('Chance of nurse callback', 0.1, 1.0, 0.4,
                                step=0.05, help='Set the chance of a call back')

    # set number of replications
    n_reps = st.slider("No. of replications", 5, 100, step=1)

2. Adding a spinner and success box#

We can setup our app so that a “model running” message is displayed while the multiple_replications function executes.

The spinner function uses a with statement and the st.spinner(message) function.

The success box using the st.success(message) function.

The modified code is:

if st.button("Run simulation"):

    with st.spinner('Simulating the urgent care system...'):
        # run multiple replications of experment
        results = multiple_replications(exp, n_reps=n_reps)
    
    st.success('Done!')

3. Full listing#

The full code listing and a screenshot of the example app are below

"""
The code in this streamlit script adds in functionality to display markdown text.
"""
import streamlit as st
from model import Experiment, multiple_replications

# We add in a title for our web app's page
st.title("Urgent care call centre")

# ##############################################################################
# MODIFICATION: side bar

with st.sidebar:

    # set number of resources
    n_operators = st.slider('Call operators', 1, 20, 13, step=1)
    n_nurses = st.slider('Nurses', 1, 15, 9, step=1)

    # set chance of nurse
    chance_callback = st.slider('Chance of nurse callback', 0.1, 1.0, 0.4,
                                step=0.05, help='Set the chance of a call back')

    # set number of replications
    n_reps = st.slider("No. of replications", 5, 100, step=1)

################################################################################

# create experiment
exp = Experiment(n_operators=n_operators, n_nurses=n_nurses,
                 chance_callback=chance_callback)

# A user must press a streamlit button to run the model
if st.button("Run simulation"):

    # ##########################################################################
    # MODIFICATION: add a spinner and then display success box
    with st.spinner('Simulating the urgent care system...'):
        # run multiple replications of experment
        results = multiple_replications(exp, n_reps=n_reps)#
    
    st.success('Done!')
    ###########################################################################

    # show results
    st.dataframe(results.describe())

image