starcore-explorer-bad/content/pyodide/ipyleaflet.ipynb
2023-06-24 01:19:43 +08:00

260 lines
6.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -q bqplot ipyleaflet"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from urllib.request import urlopen\n",
"import json\n",
"from datetime import datetime\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"from js import fetch\n",
"\n",
"from ipywidgets import Dropdown\n",
"\n",
"from bqplot import Lines, Figure, LinearScale, DateScale, Axis\n",
"\n",
"from ipyleaflet import Map, GeoJSON, WidgetControl"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"URL = \"https://raw.githubusercontent.com/jupyter-widgets/ipyleaflet/master/examples/nations.json\"\n",
"\n",
"res = await fetch(URL)\n",
"text = await res.text()\n",
"\n",
"data = pd.read_json(text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def clean_data(data):\n",
" for column in ['income', 'lifeExpectancy', 'population']:\n",
" data = data.drop(data[data[column].apply(len) <= 4].index)\n",
" return data\n",
"\n",
"def extrap_interp(data):\n",
" data = np.array(data)\n",
" x_range = np.arange(1800, 2009, 1.)\n",
" y_range = np.interp(x_range, data[:, 0], data[:, 1])\n",
" return y_range\n",
"\n",
"def extrap_data(data):\n",
" for column in ['income', 'lifeExpectancy', 'population']:\n",
" data[column] = data[column].apply(extrap_interp)\n",
" return data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = clean_data(data)\n",
"data = extrap_data(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"date_start = datetime(1800, 12, 31)\n",
"date_end = datetime(2009, 12, 31)\n",
"\n",
"date_scale = DateScale(min=date_start, max=date_end)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"date_data = pd.date_range(start=date_start, end=date_end, freq='A', normalize=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"country_name = 'Angola'\n",
"data_name = 'income'\n",
"\n",
"x_data = data[data.name == country_name][data_name].values[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x_scale = LinearScale()\n",
"\n",
"lines = Lines(x=date_data, y=x_data, scales={'x': date_scale, 'y': x_scale})\n",
"\n",
"ax_x = Axis(label='Year', scale=date_scale, num_ticks=10, tick_format='%Y')\n",
"ax_y = Axis(label=data_name.capitalize(), scale=x_scale, orientation='vertical', side='left')\n",
"\n",
"figure = Figure(axes=[ax_x, ax_y], title=country_name, marks=[lines], animation_duration=500,\n",
" layout={'max_height': '250px', 'max_width': '400px'})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def update_figure(country_name, data_name):\n",
" try:\n",
" lines.y = data[data.name == country_name][data_name].values[0]\n",
" ax_y.label = data_name.capitalize()\n",
" figure.title = country_name\n",
" except IndexError:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"URL = \"https://raw.githubusercontent.com/jupyter-widgets/ipyleaflet/master/examples/countries.geo.json\"\n",
"\n",
"res = await fetch(URL)\n",
"text = await res.text()\n",
"\n",
"countries = json.loads(text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = Map(zoom=3)\n",
"\n",
"geo = GeoJSON(data=countries, style={'fillColor': 'white', 'weight': 0.5}, hover_style={'fillColor': '#1f77b4'}, name='Countries')\n",
"m.add_layer(geo)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"widget_control1 = WidgetControl(widget=figure, position='bottomright')\n",
"\n",
"m.add_control(widget_control1)\n",
"\n",
"def on_hover(event, feature, **kwargs):\n",
" global country_name\n",
"\n",
" country_name = feature['properties']['name']\n",
" update_figure(country_name, data_name)\n",
"\n",
"geo.on_hover(on_hover)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dropdown = Dropdown(\n",
" options=['income', 'population', 'lifeExpectancy'],\n",
" value=data_name,\n",
" description='Plotting:'\n",
")\n",
"\n",
"def on_click(change):\n",
" global data_name\n",
"\n",
" data_name = change['new']\n",
" update_figure(country_name, data_name)\n",
"\n",
"dropdown.observe(on_click, 'value')\n",
"\n",
"widget_control2 = WidgetControl(widget=dropdown, position='bottomleft')\n",
"\n",
"m.add_control(widget_control2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (Pyodide)",
"language": "python",
"name": "python"
},
"language_info": {
"codemirror_mode": {
"name": "python",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8"
},
"orig_nbformat": 4,
"toc-showcode": false
},
"nbformat": 4,
"nbformat_minor": 4
}