Skip to content

Commit fe9c704

Browse files
committed
adding feature in on hover with ex
1 parent 3f9293f commit fe9c704

File tree

3 files changed

+124
-2
lines changed

3 files changed

+124
-2
lines changed

examples/Choropleth.ipynb

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"cell_type": "code",
4646
"execution_count": null,
4747
"metadata": {
48-
"scrolled": true
48+
"scrolled": false
4949
},
5050
"outputs": [],
5151
"source": [
@@ -109,6 +109,48 @@
109109
"source": [
110110
"layer.border_color = 'white'"
111111
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": null,
116+
"metadata": {},
117+
"outputs": [],
118+
"source": [
119+
"from ipywidgets import Text, HTML\n",
120+
"from ipyleaflet import WidgetControl, GeoJSON \n",
121+
"\n",
122+
"\n",
123+
"html = HTML('''\n",
124+
" <h4> Name </h4>\n",
125+
" Hover over a state\n",
126+
"''')\n",
127+
"html.layout.margin = '0px 10px 10px 10px'\n",
128+
"control = WidgetControl(widget=html, position='topright')\n",
129+
"m.add_control(control)\n",
130+
"\n",
131+
"def update_html(feature, id, **kwargs):\n",
132+
" html.value = '''\n",
133+
" State name: \n",
134+
" <b>{}\\n</b>\n",
135+
" {} \n",
136+
" '''.format(id, feature['properties']['name'])\n",
137+
"\n",
138+
"layer.on_hover(update_html)"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": null,
144+
"metadata": {},
145+
"outputs": [],
146+
"source": []
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": null,
151+
"metadata": {},
152+
"outputs": [],
153+
"source": []
112154
}
113155
],
114156
"metadata": {
@@ -127,7 +169,7 @@
127169
"name": "python",
128170
"nbconvert_exporter": "python",
129171
"pygments_lexer": "ipython3",
130-
"version": "3.7.0"
172+
"version": "3.7.3"
131173
}
132174
},
133175
"nbformat": 4,

examples/GeoJson_EU_on_hover.ipynb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from ipyleaflet import Map, GeoJSON\n",
10+
"import json\n",
11+
"import os\n",
12+
"import requests\n",
13+
"\n",
14+
"if not os.path.exists('europe_110.geo.json'):\n",
15+
" url = 'https://github.com/jupyter-widgets/ipyleaflet/raw/master/examples/europe_110.geo.json'\n",
16+
" r = requests.get(url)\n",
17+
" with open('europe_110.geo.json', 'w') as f:\n",
18+
" f.write(r.content.decode(\"utf-8\"))\n",
19+
"\n",
20+
"with open('europe_110.geo.json', 'r') as f:\n",
21+
" data = json.load(f)\n",
22+
"\n",
23+
"m = Map(center=(50.6252978589571, 0.34580993652344), zoom=3)\n",
24+
"geo_json = GeoJSON(data=data, style = {'color': 'green', 'opacity':1, 'weight':1.9, 'dashArray':'9', 'fillOpacity':0.1})\n",
25+
"m.add_layer(geo_json)\n",
26+
"\n",
27+
"m"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"from ipywidgets import Text, HTML\n",
37+
"from ipyleaflet import WidgetControl, GeoJSON \n",
38+
"\n",
39+
"html1 = HTML('''\n",
40+
" <h4>EU population density</h4>\n",
41+
" Hover over a state\n",
42+
"''')\n",
43+
"html1.layout.margin = '0px 20px 20px 20px'\n",
44+
"control1 = WidgetControl(widget=html1, position='bottomright')\n",
45+
"m.add_control(control1)\n",
46+
"\n",
47+
"def update_html(feature, **kwargs):\n",
48+
" html1.value = '''\n",
49+
" <h4>EU population density</h4>\n",
50+
" <h4><b>{}</b></h4>\n",
51+
" {} people / mi^2\n",
52+
" '''.format(feature['properties']['name'], feature['properties']['pop_est'])\n",
53+
"\n",
54+
"geo_json.on_hover(update_html)"
55+
]
56+
}
57+
],
58+
"metadata": {
59+
"kernelspec": {
60+
"display_name": "Python 3",
61+
"language": "python",
62+
"name": "python3"
63+
},
64+
"language_info": {
65+
"codemirror_mode": {
66+
"name": "ipython",
67+
"version": 3
68+
},
69+
"file_extension": ".py",
70+
"mimetype": "text/x-python",
71+
"name": "python",
72+
"nbconvert_exporter": "python",
73+
"pygments_lexer": "ipython3",
74+
"version": "3.7.3"
75+
}
76+
},
77+
"nbformat": 4,
78+
"nbformat_minor": 2
79+
}

js/src/layers/GeoJSON.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var LeafletGeoJSONView = featuregroup.LeafletFeatureGroupView.extend({
3232
}
3333
that.send({
3434
event: e.type,
35+
feature: feature,
3536
properties: feature.properties,
3637
id: feature.id
3738
});

0 commit comments

Comments
 (0)