Skip to content

Commit

Permalink
fix: improve search by population
Browse files Browse the repository at this point in the history
  • Loading branch information
flekschas committed Sep 18, 2024
1 parent 930aaf7 commit 6548fce
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions notebooks/5-Search.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"metadata": {},
"source": [
"The beauty of this approach is that you search across any column in the data frame. For instance, in the following example we enhanced the search to allow:\n",
"- Searching by population via `n:<number>` or `n:<number>-<number>`. E.g., `n:1000000-10000000` will select all cities with a population between 1 to 10 million.\n",
"- Searching by population via `>N`, `>=N`, `<N`, `<=N` or `==N`. E.g., `>10000000` will select all cities with a population above 10 million.\n",
"- Searching by continent. E.g., `Australia` will select all cities in Australia"
]
},
Expand All @@ -110,29 +110,31 @@
"source": [
"import ipywidgets\n",
"import jscatter\n",
"import re\n",
"\n",
"scatter2 = jscatter.Scatter(data=cities, x='Mercator X', y='Mercator Y')\n",
"scatter2.color(by='Continent').size(2).axes(False).legend(True).height(640)\n",
"scatter2.color(by='Continent').size(3).axes(False).legend(True).height(720)\n",
"\n",
"searchbox = ipywidgets.Text(\n",
" value='',\n",
" placeholder='Search by city name',\n",
" placeholder='Find cities by name or population',\n",
" description='Search:',\n",
" disabled=False \n",
")\n",
"\n",
"def search_change_handler(change):\n",
" if change['new']:\n",
" if change['new'] in cities.Continent.unique():\n",
" match = re.search(r\"^([<>=]=?)(\\d+)$\", change['new'])\n",
"\n",
" if match:\n",
" comparator = match.group(1)\n",
" number = match.group(2)\n",
" city_idxs = cities.query(f'Population {comparator} {number}').index\n",
" elif change['new'] in cities.Continent.unique():\n",
" city_idxs = cities.query(f'Continent == \"{change['new']}\"').index\n",
" elif change['new'].startswith('n:'):\n",
" if '-' in change['new']:\n",
" n1, n2 = change['new'][2:].split('-')\n",
" city_idxs = cities.query(f'Population >= {n1} and Population <= {n2}').index\n",
" else:\n",
" city_idxs = cities.query(f'Population == {change['new'][2:]}').index\n",
" else:\n",
" city_idxs = cities.query(f'Name == \"{change['new']}\"').index\n",
" city_idxs = cities.query(f'Name == \"{change['new']}\"', ).index\n",
" \n",
" scatter2.selection(city_idxs)\n",
" scatter2.zoom(to=city_idxs, animation=500, padding=0.5)\n",
" else:\n",
Expand Down Expand Up @@ -165,15 +167,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "bb3973ad-d8fd-428d-aedf-2636d28a0980",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "834e2354-497b-448b-b1f2-6c0815ca3a4f",
"id": "9095d87d-e376-4279-8a89-2ee8f34ffa4f",
"metadata": {},
"outputs": [],
"source": []
Expand Down

0 comments on commit 6548fce

Please sign in to comment.