{ "cells": [ { "cell_type": "markdown", "id": "10f1342b-dda5-4b73-9770-87b554b6b058", "metadata": {}, "source": [ "# A first web app\n", "\n", "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." ] }, { "cell_type": "markdown", "id": "7d0615ba-79e7-45b5-bcdf-7dc8782f2549", "metadata": {}, "source": [ "## 1. A reminder of how to use the model\n", "\n", "Below is the basic script we create to use the model wrapper functions." ] }, { "cell_type": "code", "execution_count": 3, "id": "3525e19a-8a84-4870-85ff-d1c16aa2a6da", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
01_mean_waiting_time02_operator_util03_mean_nurse_waiting_time04_nurse_util
count5.0000005.0000005.0000005.000000
mean3.14636792.80923753.37151497.422884
std1.3011411.6270118.3003900.923472
min1.76630990.46331045.34818595.971377
25%2.53093692.01815848.66276097.204975
50%2.64485193.33406549.31115297.618858
75%3.64670193.49391457.79533197.901794
max5.14304094.73673765.74014498.417413
\n", "
" ], "text/plain": [ " 01_mean_waiting_time ... 04_nurse_util\n", "count 5.000000 ... 5.000000\n", "mean 3.146367 ... 97.422884\n", "std 1.301141 ... 0.923472\n", "min 1.766309 ... 95.971377\n", "25% 2.530936 ... 97.204975\n", "50% 2.644851 ... 97.618858\n", "75% 3.646701 ... 97.901794\n", "max 5.143040 ... 98.417413\n", "\n", "[8 rows x 4 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from model import Experiment, multiple_replications\n", "\n", "# set number of resources\n", "n_operators = 13\n", "n_nurses = 9\n", "\n", "# set chance of nurse\n", "chance_callback = 0.4\n", "\n", "# set number of replications\n", "n_reps = 5\n", "\n", "# create experiment\n", "exp = Experiment(n_operators=n_operators, n_nurses=n_nurses,\n", " chance_callback=chance_callback)\n", "\n", "# run multiple replications of experment\n", "results = multiple_replications(exp, n_reps=n_reps)\n", "\n", "# show results\n", "results.describe()" ] }, { "cell_type": "markdown", "id": "d8ece3d8-aea8-47af-84ea-f88bc09e2af9", "metadata": {}, "source": [ "## 2. Modifying the script: import `streamlit` and create a title\n", "\n", "The first step is to add the following import at the top of the module:\n", "\n", "```python\n", "import streamlit as st\n", "```\n", "\n", "If we rename and save `model.py` as `basic_app.py` we can then run it using the following from the command line\n", "\n", "```\n", "streamlit run basic_app.py\n", "```\n", "\n", "The web app will run at the following address and port: http://localhost:8501 \n", "\n", "> If the app does not launch automatically you can paste into your browser.\n", "\n", "This will present you with a very unexciting blank canvas! \n", "\n", "Go back to `basic_app.py` and add in the following code to create a page title\n", "\n", "```python\n", "st.title(\"Urgent care call centre\")\n", "```\n", "\n", "> 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." ] }, { "cell_type": "markdown", "id": "30e2e6cf-9ef7-46ca-970e-9dfc2a0cc9f8", "metadata": {}, "source": [ "## 3. Adding a button and displaying results\n", "\n", "At the moment the model runs once - when the app starts. We will now add a `streamlit.button` to control when the app runs.\n", "\n", "In `basic_app.py` find the following code\n", "\n", "```python\n", "# run multiple replications of experment\n", "results = multiple_replications(exp, n_reps=n_reps)\n", "\n", "# show results\n", "results.describe()\n", "```\n", "\n", "modify the code by adding the following if statement \n", "\n", "```python\n", "if st.button(\"Run simulation\"):\n", "\n", " # run multiple replications of experment\n", " results = multiple_replications(exp, n_reps=n_reps)\n", "\n", " # show results\n", " print(results.describe())\n", "```\n", "\n", "The change means that the app will now include a \"Run simulation\" button underneath the title. When it is click the model will run.\n", "\n", "> 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.\n", "\n", "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:\n", "\n", "```python\n", " st.dataframe(results.describe())\n", "```\n", "\n", "The final `basic_app.py` script is below and when run will produce an output similar to the figure below it.\n", "\n", "```python\n", "\"\"\"\n", "The code in this streamlit script modifies the basic script \n", "we had for running a scenario\n", "\"\"\"\n", "import streamlit as st\n", "from model import Experiment, multiple_replications\n", "\n", "# ##############################################################################\n", "# MODIFICATION: We add in a title for our web app's page\n", "st.title(\"Urgent care call centre\")\n", "################################################################################\n", "\n", "# set number of resources\n", "n_operators = 13\n", "n_nurses = 9\n", "\n", "# set chance of nurse\n", "chance_callback = 0.4\n", "\n", "# set number of replications\n", "n_reps = 5\n", "\n", "# create experiment\n", "exp = Experiment(n_operators=n_operators, n_nurses=n_nurses,\n", " chance_callback=chance_callback)\n", "\n", "################################################################################\n", "# MODIFICATION: A user must press a streamlit button to run the model\n", "if st.button(\"Run simulation\"):\n", "\n", " # run multiple replications of experment\n", " results = multiple_replications(exp, n_reps=n_reps)\n", "\n", " # show results\n", " st.dataframe(results.describe())\n", "################################################################################\n", "```" ] }, { "cell_type": "markdown", "id": "e11cbc01-58d4-490d-af8e-a79ee9703256", "metadata": {}, "source": [ "![image](../../imgs/basic_app.png)" ] }, { "cell_type": "markdown", "id": "ea0b2c1c-6c1c-4536-b1b9-59be0ec555bb", "metadata": {}, "source": [ "## 4. Next steps.\n", "\n", "Next we will look at how we use `streamlit` input widgets to make our simulation interactive." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }