A first web app#

The creation of a web application is all handled “behind the scenes” by the streamlit software. A user needs to write a streamlit script that specifies placement of widgets on the screen.

1. A reminder of how to use the model#

Below is the basic script we create to use the model wrapper functions.

from model import Experiment, multiple_replications

# set number of resources
n_operators = 13
n_nurses = 9

# set chance of nurse
chance_callback = 0.4

# set number of replications
n_reps = 5

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

# run multiple replications of experment
results = multiple_replications(exp, n_reps=n_reps)

# show results
results.describe()
01_mean_waiting_time 02_operator_util 03_mean_nurse_waiting_time 04_nurse_util
count 5.000000 5.000000 5.000000 5.000000
mean 3.646470 93.278090 50.642276 97.149156
std 0.980486 1.882226 11.421730 1.116457
min 2.257624 91.742451 39.468692 95.732083
25% 3.126866 91.959186 42.481209 96.576196
50% 3.760358 92.500454 47.704197 96.920853
75% 4.435740 93.895775 55.708134 97.996692
max 4.651762 96.292581 67.849147 98.519955

2. Modifying the script: import streamlit and create a title#

The first step is to add the following import at the top of the module:

import streamlit as st

If we rename and save model.py as basic_app.py we can then run it using the following from the command line

streamlit run basic_app.py

The web app will run at the following address and port: http://localhost:8501

If the app does not launch automatically you can paste into your browser.

This will present you with a very unexciting blank canvas!

Go back to basic_app.py and add in the following code to create a page title

st.title("Urgent care call centre")

Tip: you can leave the app running while you do this. Just save the file and the app will prompt you to reload it as changes have been made.

3. Adding a button and displaying results#

At the moment the model runs once - when the app starts. We will now add a streamlit.button to control when the app runs.

In basic_app.py find the following code

# run multiple replications of experment
results = multiple_replications(exp, n_reps=n_reps)

# show results
results.describe()

modify the code by adding the following if statement

if st.button("Run simulation"):

    # run multiple replications of experment
    results = multiple_replications(exp, n_reps=n_reps)

    # show results
    print(results.describe())

The change means that the app will now include a “Run simulation” button underneath the title. When it is click the model will run.

There’s an important detail about how streamlit works with button and other widgets that you will need to learn to work with it effectively. We will cover that shortly.

To display the results on screen we will use the st.dataframe data widget. This displays the dataframe returned from results.describe() on the web app page. Modify the code as follows:

     st.dataframe(results.describe())

The final basic_app.py script is below and when run will produce an output similar to the figure below it.

"""
The code in this streamlit script modifies the basic script 
we had for running a scenario
"""
import streamlit as st
from model import Experiment, multiple_replications

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

# set number of resources
n_operators = 13
n_nurses = 9

# set chance of nurse
chance_callback = 0.4

# set number of replications
n_reps = 5

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

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

    # run multiple replications of experment
    results = multiple_replications(exp, n_reps=n_reps)

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

image

4. Next steps.#

Next we will look at how we use streamlit input widgets to make our simulation interactive.