This git repo is the complementary source code for
- https://youtu.be/zxwSezObVkQ (macOS: tasks 1-9)
- https://youtu.be/78RaVmb-c30 (Windows: tasks 1-6)
- https://youtu.be/0vcT2tB7iZ4 (Windows: tasks 7-10)
The project will show how to create an interactive chart plotting cases of COVID-19 by country or state.
- Install Python 3.9.2 from https://www.python.org/downloads/release/python-392/
- Add python to path when installing, or know where it is for later
- (3.6 or later should be fine, anaconda also fine)
- Install Git 2.31 from https://git-scm.com/download/
- Install IDE eg PyCharm Community Edition 2020.3.5 from https://www.jetbrains.com/pycharm/download/
- Clone repository and setup virtual environment
cd ~/PycharmProjects
git clone https://github.com/timcu/bpaml-dash-graph.git
cd bpaml-dash-graph
git checkout challenge1
- Get from VCS >
- Git > Branches... > Checkout tag or revision
- challenge1
python -m venv venv # Windows
python3 -m venv venv # Mac or Linux
conda create --name venv-dash-graph python # Anaconda
source venv/bin/activate # Mac or Linux
conda activate venv-dash-graph # Anaconda
source venv/Scripts/activate # Windows Git-Bash
venv\Scripts\activate.bat & :: Windows command prompt
venv\Scripts\Activate.ps1 & :: Windows powershell
pip install -r requirements.txt # Windows, Mac or Linux without Anaconda
conda install --name venv-dash-graph --file requirements.txt # Anaconda
- PyCharm > Preferences > Project > Python Interpreter
- Project Interpreter > Show all > +
- Virtualenv: New environment
- Location: ./venv
- Base Interpreter: ...bin/python3.9 or AppData\Local\Programs\Python\Python39\python.exe
- Virtualenv: New environment
- Project Interpreter > Show all > +
- PyCharm > Preferences > Project > Python Interpreter
- Project Interpreter > Show all > +
- Conda: New environment
- Location: ~\Anaconda3\envs\venv-dash-graph # ~ means home directory
- Python version: 3.9
- Conda executable: ~\Anaconda3\Scripts\conda.exe # ~ means home directory
- Conda: New environment
- Project Interpreter > Show all > +
Tasks are listed in file tasks.txt and are visible on each branch. Tasks are listed in README.md in master branch but not on other branches.
Command line:
git switch challenge2
PyCharm GUI:
- Git > Branches... > Checkout tag or revision > challenge2
-
Create a simple layout with a single
<h1>
and a single<div>
element -
Run the application from the command line (python bpaml_dash_graph.py)
-
Run the application from PyCharm using ctrl-shift-F10 (PC) or ctrl-shift-r (macOS)
-
Create a branch to commit your changes
git checkout -b mytask2attempt # -b creates a branch. GUI: Git > Branches... > New Branch git commit -a -m "my task 2 solution" # -a adds all changes before commit. GUI: Commit tab (top left).
- If GUI commit tab is not visible in top left then might need to turn on new behaviour in PyCharm. GUI: File > Settings... > Version Control > Commit > Non-modal commit
-
Checkout task 3 and compare differences
git checkout challenge3 # check out branch called challenge3 (similar to switch) git diff challenge2 challenge3 # show what has changed between challenge2 and challenge3. GUI: Log
-
Code from meetup053 has been added into a class in data_cache.py. Initialise using:
import data_cache data = data_cache.DataCache()
-
Get DataFrame from DataCache.df_for_case_type()
-
Get all unique countries from column "Country/Region" in DataFrame
-
Sort the countries in place
-
Create an
html.Label
with the text "Country" -
Create a dash_core_components
Dropdown
with- id="input-country",
- value="" (initial selection),
- options= list of dictionaries for the countries. "label" is what is shown, "value" is what is returned to app
- [{"label": "Australia", "value": "Australia"}, {"label": "New Zealand", "value": "New Zealand"}]
-
Create a branch to commit your changes
git checkout -b mytask3attempt git commit -a -m "my task 3 solution"
-
Checkout task 4
git switch challenge4
-
Create a
html.Div
with the initial text empty and an id of "text-selected-country" -
Create a function which takes the country name and returns "You have selected {country}"
-
Use the
@app.callback
decorator to call the function when "value" of "input-country" changes and stores result in "children" of "text-selected-country" -
Commit your changes to your own branch and checkout challenge 5
git checkout -b mytask4attempt git commit -a -m "my task 4 solution" git switch challenge5
-
Create an
html.Label
with the text "State/Province" -
Create a dash_core_components
Dropdown
with- id="input-state",
- value="" (initial selection),
- options= list of dictionaries for the states [{"label": state, "value": state}, ...]
-
Use the
@app.callback decorator
to call a function when "value" of "input-country" changes and stores result in "options" of "input-state" -
Define a function in
utilities.py
called location_name(country=None, state=None) which returns location name -
Also change
update_text_selected_country()
to take two input values. Use functionlocation_name
inutilities.py
-
Commit your changes to your own branch and checkout task6
git checkout -b mytask5attempt git commit -a -m "my task 5 solution" git switch challenge6
-
figure_cumulative_doubling
has been provided from meetup053 -
add a dash_core_components
Graph
object with id="graph-doubling-days" and a figure=fig_for_location(data=data) -
define a function
update_graph(country, state)
which returns a figure based on selected country and state -
Use the
@app.callback
decorator to call the function when "value" of "input-country" or "input-state" changes -
Commit your changes to your own branch and checkout 7
git checkout -b mytask6attempt git commit -a -m "my task 6 solution" git switch challenge7
-
add a dash_core_components
Slider
object with id="input-averaged-days", min, max, value, marks {"1":"1", "2":"2", ...} -
modify
update_graph
to take an additional argumentaveraged_days
-
Commit your changes to your own branch and checkout challenge 8
git checkout -b mytask7attempt git commit -a -m "my task 7 solution" git switch challenge8
-
add a dash_core_components
Input
object with id="input-num-start", min=1, value=100, type="number" -
modify
update_graph
to take an additional argumentnum_start
-
if
num_start
is blank then use default value infig_for_location
function -
Commit your changes to your own branch and checkout challenge 9
git checkout -b mytask8attempt git commit -a -m "my task 8 solution" git switch challenge9
-
add dash_core_components
RadioItems
for id="input-case-type" and id="input-yaxes-type" -
default
case_type
is "confirmed", defaultyaxes_type
is "log" -
options for
case_type
[{"label": "confirmed", "value": "confirmed"}, ] for confirmed, recovered, deaths -
options for
yaxes_type
are linear or log -
labelStyle={"display": "inline-block"}
-
Divide input fields into 3 divs with style={"breakInside": "avoid"}, all inside one div with style={"columnCount": 3}
-
Commit your changes to your own branch and checkout challenge 10
git checkout -b mytask9attempt git commit -a -m "my task 9 solution" git switch challenge10
-
data_cache.py
has been modified to accept "active" as a case type (from meetup096). -
figure_cumulative_doubling.py
has been modified to accept list of days for drawing doubling guides -
add case type "active" to RadioItems "input-case-type"
-
add dash_core_components CheckList for id="input-doubling-guides"
-
options for doubling guides [{"label": str(days), "value": days}, ] for 4, 5, 6, 8, 10, 12
-
use inline-block labels
-
initial value [6, 12]
-
Commit your changes to your own branch and checkout master to see the solution
git checkout -b mytask10attempt git commit -a -m "my task 10 solution" git switch master