Skip to content

jackdbd/dash-earthquakes

Repository files navigation

Dash Earthquakes

License: MIT CI

A Plotly Dash application showing earthquake data from the US Geological Survey.

The GeoJSON summary feed from the USGS website is updated every 15 minutes and it refers to the 4.5+ magnitude earthquakes occurred in the past month.

A GIF file showing a short demo on how to use the Dash Earthquakes application

App on Cloud Run.

Built with:

Environment variables

This project requires the following environment variables:

I define all required environment variables in a .envrc file, so direnv picks them automatically.

Installation

This project uses pyenv to specify the Python interpreter, pyenv-virtualenv to manage the Python virtual environment, and poetry to manage the project's dependencies.

If you don't already have it, install python 3.8.5 using pyenv:

pyenv install 3.8.5

Create a virtual environment for this project, then activate it:

pyenv virtualenv 3.8.5 dash_earthquakes

Every time you work on this project, remember to activate the virtual environment:

pyenv activate dash_earthquakes

# and when you stop working on this project, run:
pyenv deactivate

Install all the dependencies from the poetry.lock file.

poetry install

Tasks

This project uses the task runner Poe the Poet to run poetry scripts.

Development

Run the app locally using a development server (Dash uses a Flask development server):

poetry run poe dev

Run all tests with pytest:

poetry run poe test

Format all code with black:

poetry run poe format

Build

Build the container image using the Dockerfile:

poetry run poe container_build

Create a container and run it:

poetry run poe container_run

Deployment

Trigger a build on Cloud Build. Cloud Build will build a container image, push it to Artifact Registry, then deploy the app to Cloud Run:

gcloud builds submit ./ \
  --config cloudbuild.yaml \
  --project $GCP_PROJECT_ID \
  --substitutions _MAPBOX_ACCESS_TOKEN=$MAPBOX_ACCESS_TOKEN,_PLOTLY_API_KEY=$PLOTLY_API_KEY,_PLOTLY_USERNAME=$PLOTLY_USERNAME \
  --async

Troubleshooting

Run a vulnerability scan with trivy (you will need to install it):

poetry run poe container_scan

Explore the container image with dive (you will need to install it):

poetry run poe container_explore

If you are on Ubuntu you might get ModuleNotFoundError: No module named '_bz2' and/or UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError. These errors are caused by pandas when it tries to import these compression libraries. If you get these errors you need to install the libbz2-dev package and the liblzma-dev package, then re-compile your python interpreter.

Here is how you can do it:

# deactivate and remove the virtual environment
pyenv deactivate
pyenv virtualenv-delete dash_earthquakes

# remove the "broken" python interpreter
pyenv uninstall 3.8.5

# install the compression libraries
sudo apt-get install libbz2-dev liblzma-dev

# download and compile the python interpreter
pyenv install 3.8.5

# re-create the virtual environment and activate it
pyenv virtualenv 3.8.5 dash_earthquakes
pyenv activate dash_earthquakes

# re-install all the dependencies
poetry install