This tutorial will demonstrate how interactive web dashboard frameworks like Plotly Dash work, by building a simplified version of Dash itself from scratch using Python, the Flask framework, and a little bit of vanilla JavaScript.
If you haven't already, please complete the setup instructions outlined in SETUP.md before continuing.
The 7 exercises to be completed for this tutorial are located in the directories exercise1/ through exercise7/.
Each exercise folder contains the following:
-
A partial copy of the NanoDash codebase (under
exerciseN/nanodash/), containing one or more spots for you to "fill-in-the-blanks" by implementing part of the NanoDash logic -
A sample app (
exerciseN/app.py) which will run correctly once the exercise has been completed- To run the app from the repository root:
python exerciseN/app.py
- To run the app from the repository root:
-
A tests file (
test_exerciseN.py) which will pass once the exercise has been completed.- To run the tests for exercise N from the repository root:
python -m pytest exerciseN/
- To run the tests for exercise N from the repository root:
We recommend using the command python -m pytest rather than just pytest, because it ensures using the Python in your virtual environment rather than the system Python.
Each exercise focuses on implementing a specific part of the NanoDash framework.
Goal: Set up a simple Flask server that serves one static HTML page.
Tasks:
- Implement the
full_html()function of theNanoDashclass inexercise1/nanodash/nanodash.pyto return a valid HTML webpage.
Files to modify
exercise1/nanodash/nanodash.py
Command to run tests
python -m pytest exercise1/
Command to run app
python exercise1/app.py
Goal: Implement basic input components as Python objects, to be used as building blocks for interactive dashboards.
Tasks:
- Review the implementations of the
Page,HeaderandTextclasses inexercise2/nanodash/components.py - Implement the
html()method of theTextInputclass - Implement the
html()method of theDropdownclass
Files to modify:
exercise2/nanodash/components.py
Command to run tests
python -m pytest exercise2/
Command to run app
python exercise2/app.py
Goal: Implement the Graph component, which uses Plotly.js to display Plotly figures in the browser.
Tasks:
- Implement the
html()method of theGraphclass inexercise3/nanodash/components.py
Files to modify:
exercise3/nanodash/components.py
Command to run tests
python -m pytest exercise3/
Command to run app
python exercise3/app.py
Goal: Implement the Javascript logic to capture the state of all components on the page, and bundle it into a JSON request to send to the Flask server.
Don't worry — we've provided some useful helper functions inside the Javascript file; all you need to do is put them together in the right way.
Tasks:
- Implement the
getState()Javascript function inexercise4/nanodash/static/index.js.
Files to modify:
exercise4/static/index.js
Command to run tests
python -m pytest exercise4/
Command to run app
python exercise4/app.py
Goal: Implement the Python logic which receives the page state from the frontend, runs the necessary callbacks, and sends the results back to the frontend. Also implement the logic which allows a user to add a callback to their app.
Tasks:
- Implement the
handle_change()Python function inexercise5/nanodash/nanodash.py - Implement the
add_callback()Python function inexercise5/nanodash/nanodash.py
Files to modify:
exercise5/nanodash/nanodash.py
Command to run tests
python -m pytest exercise5/
Command to run app
python exercise5/app.py
Goal: Implement the Javascript logic to update the page's UI components based on the callback results received from the server.
Tasks:
- Implement the
updateValues()Javascript function inexercise6/nanodash/static/index.js
Files to modify:
exercise6/nanodash/static/index.js
Command to run tests
python -m pytest exercise6/
Command to run app
python exercise6/app.py
Goal: Use the NanoDash framework to write your own interactive dashboard. You can modify the framework or add new components if you like.
Tasks:
- Pick a dataset
- Modify
exercise7/app.pyto define an app layout, add graphs and interactive components, and add at least one callback
Files to modify:
exercise7/app.py
There are no tests for this exercise since your app can do whatever you like!
- Intro to HTML: https://developer.mozilla.org/en-US/docs/Learn_web_development/Getting_started/Your_first_website/Creating_the_content#creating_your_first_html_document
- Plotly.js documentation: https://plotly.com/javascript/
- Plotly.py documentation: https://plotly.com/python/
- Flask documentation: https://flask.palletsprojects.com/
- Pittsburgh data: https://data.wprdc.org/