Source codes for web app:
Folder structure:
.
├── LICENSE
├── cloudbuild.yaml
├── README.md
├── backapp
│ ├── app.yaml
│ ├── config.py
│ ├── cron.yaml
│ ├── main.py
│ ├── migration.py
│ ├── operations.py
│ └── requirements.txt
└── frontapp
├── Dockerfile
├── app_data.py
├── app_funcs.py
├── main.py
└── requirements.txt
This app is responsible for the backend data pipeline of the app.
Source codes in backapp
folder.
Main logics in backapp/main.py
(the flask app) and backapp/operations.py
:
- Request high level crime report json data from https://polisen.se/api/events.
- Parse the json data and then scrape the webpage in "url" field for crime report details. For example information in this web page.
- Grab street and city names from the detail data and request the latitude and longitude using Open Street Map, by for example requesting the following python "fstring":
f"https://nominatim.openstreetmap.org/search/405 {search_term}?format=json&limit=1"
Where search_term
variable is the street and city names.
- Translate the details data into English using Google Translate API.
- Combine all the information into a dataframe and push to Google Cloud BigQuery and google cloud storage bucket as parquet file.
This app is deployed using app engine and scheduled by cron jobs.
# deploy app.yaml
gcloud app deploy app.yaml --version=v1
# deploy cron.yaml
gcloud app deploy cron.yaml
Interactive visualization in the webapp: https://crimemap.se .
Source codes in frontapp
folder.
Main logic:
- Read data from Google Cloud storage bucket. See
frontapp/app_data.py
. - Get map data form Mapbox API and other data processing steps. See
frontapp/app_func.py
. - Visualize data interactively using dash and plotly in
frontapp/main.py
.
This app is deployed as a cloud run app with cloud build. ./cloudbuild.yaml
contains the following build steps:
- Install dependency.
- Build docker image according to
.frontapp/Dockerfile
. - Push the image to GCP artifact registry.
- Deploy the image to cloud run.
Cloud build triggers are set for the following events:
- Daily at 8:20 and 20:20.
- On push to master branch of this github repository.