{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipyleaflet import Map, GeoJSON\n", "import json\n", "import os\n", "import requests\n", "\n", "if not os.path.exists(\"europe_110.geo.json\"):\n", " url = \"https://github.com/jupyter-widgets/ipyleaflet/raw/master/examples/europe_110.geo.json\"\n", " r = requests.get(url)\n", " with open(\"europe_110.geo.json\", \"w\") as f:\n", " f.write(r.content.decode(\"utf-8\"))\n", "\n", "with open(\"europe_110.geo.json\", \"r\") as f:\n", " data = json.load(f)\n", "\n", "m = Map(center=(50.6252978589571, 0.34580993652344), zoom=3)\n", "geo_json = GeoJSON(\n", " data=data,\n", " style={\n", " \"color\": \"green\",\n", " \"opacity\": 1,\n", " \"weight\": 1.9,\n", " \"dashArray\": \"9\",\n", " \"fillOpacity\": 0.1,\n", " },\n", ")\n", "m.add(geo_json)\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import Text, HTML\n", "from ipyleaflet import WidgetControl, GeoJSON\n", "\n", "html1 = HTML(\n", " \"\"\"\n", "

EU population density

\n", " Hover over a state\n", "\"\"\"\n", ")\n", "html1.layout.margin = \"0px 20px 20px 20px\"\n", "control1 = WidgetControl(widget=html1, position=\"bottomright\")\n", "m.add(control1)\n", "\n", "\n", "def update_html(feature, **kwargs):\n", " html1.value = \"\"\"\n", "

EU population density

\n", "

{}

\n", " {} people / mi^2\n", " \"\"\".format(\n", " feature[\"properties\"][\"name\"], feature[\"properties\"][\"pop_est\"]\n", " )\n", "\n", "\n", "geo_json.on_hover(update_html)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.0" } }, "nbformat": 4, "nbformat_minor": 4 }