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 2.791417 92.105187 39.512995 96.800153
std 0.912102 1.772361 11.117251 0.708716
min 1.634700 90.108331 23.970179 96.065088
25% 2.622755 91.298766 37.424467 96.325704
50% 2.696908 91.666991 38.937995 96.521772
75% 2.814228 92.611987 42.198773 97.371930
max 4.188494 94.839861 55.033562 97.716269

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.