{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Selecting data for use in Python: Singapore taxi location data\n",
    "\n",
    "Often it's easier to use a visual application to draw a shape than define a geometry in code. Here we'll explore how pydeck can be used to select data and pass that selected data back to the Jupyter kernel for use in Pandas.\n",
    "\n",
    "Currently this notebook runs against live data, and there is a slight chance that the source data will be unavailable.\n",
    "\n",
    "## Contents\n",
    "\n",
    "- [Getting the data](#Getting-the-data)\n",
    "- [Plotting the data](#Plotting-the-data)\n",
    "- [Interaction](#Interaction)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting the data\n",
    "\n",
    "Here we'll use the live taxi location API provided by the government of Singapore."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pydeck as pdk\n",
    "\n",
    "DATA_URL = 'https://api.data.gov.sg/v1/transport/taxi-availability'\n",
    "COLOR_RANGE = [\n",
    "  [255, 255, 178, 25],\n",
    "  [254, 217, 118, 85],\n",
    "  [254, 178, 76, 127],\n",
    "  [253, 141, 60, 170],\n",
    "  [240, 59, 32, 212],\n",
    "  [189, 0, 38, 255]\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import requests\n",
    "\n",
    "json = requests.get(DATA_URL).json()\n",
    "df = pd.DataFrame(json[\"features\"][0][\"geometry\"][\"coordinates\"])\n",
    "df.columns = ['lng', 'lat']\n",
    "\n",
    "viewport = pdk.data_utils.compute_view(df[['lng', 'lat']])\n",
    "layer = pdk.Layer(\n",
    "    'ScreenGridLayer',\n",
    "    df,\n",
    "    get_position=['lng', 'lat'],\n",
    "    cell_size_pixels=20,\n",
    "    color_range=COLOR_RANGE,\n",
    "    pickable=True,\n",
    "    auto_highlight=True)\n",
    "r = pdk.Deck(layers=[layer], initial_view_state=viewport)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Two way communication\n",
    "\n",
    "Click the above visualization and then execute the cell below to pass data from the application to Python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.DataFrame(r.selected_data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
