Skip to content

Commit 81726a1

Browse files
committed
upgrades and adds leaflet compatibility layer
1 parent 2f7ba82 commit 81726a1

File tree

6 files changed

+77
-67
lines changed

6 files changed

+77
-67
lines changed

ngwidgets/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.19.4"
1+
__version__ = "0.20.0"

ngwidgets/leaflet.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

ngwidgets/leaflet.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
"""
2-
https://github.com/zauberzeug/nicegui/blob/main/examples/map/leaflet.py
2+
(Re) Created on 2024-12-03
33
4-
@author: rodja
4+
compatibility layer
55
6-
modified 2023-08-16 to handle set_zoom and draw_path
76
@author: wf
87
"""
9-
108
from typing import List, Tuple
11-
129
from nicegui import ui
1310

11+
class leaflet(ui.leaflet):
12+
"""Leaflet map wrapper for backwards compatibility with older nicegui versions"""
1413

15-
class leaflet(ui.element, component="leaflet.js"):
16-
"""
17-
nicegui Leaflet integration
18-
19-
see https://leafletjs.com/
20-
"""
21-
22-
def __init__(self, version="1.7.1") -> None:
23-
super().__init__()
24-
ui.add_head_html(
25-
f'<link href="https://unpkg.com/leaflet@{version}/dist/leaflet.css" rel="stylesheet"/>'
26-
)
27-
ui.add_head_html(
28-
f'<script src="https://unpkg.com/leaflet@{version}/dist/leaflet.js"></script>'
29-
)
14+
def __init__(self, version: str = "1.7.1") -> None:
15+
"""Initialize map with default center and zoom
16+
Args:
17+
version: Kept for backwards compatibility, not used"""
18+
super().__init__(center=(0.0, 0.0), zoom=9)
19+
self._path_layer = None
3020

3121
def set_location(self, location: Tuple[float, float], zoom_level: int = 9) -> None:
32-
lat, lon = location
33-
self.run_method("set_location", lat, lon, zoom_level)
34-
35-
def set_zoom_level(self, zoom_level: int):
36-
self.run_method("set_zoom_level", zoom_level)
22+
"""Set map center and zoom
23+
Args:
24+
location: (latitude, longitude) tuple
25+
zoom_level: Zoom level 1-18"""
26+
self.center = location
27+
self.zoom = zoom_level
28+
29+
def set_zoom_level(self, zoom_level: int) -> None:
30+
"""Set map zoom level
31+
Args:
32+
zoom_level: Zoom level 1-18"""
33+
self.zoom = zoom_level
3734

3835
def draw_path(self, path: List[Tuple[float, float]]) -> None:
39-
"""Draw a path on the map based on list of lat-long tuples."""
40-
self.run_method("draw_path", path)
36+
"""Draw path on map
37+
Args:
38+
path: List of (lat, lon) points"""
39+
if self._path_layer:
40+
self.remove_layer(self._path_layer)
41+
self._path_layer = self.generic_layer({
42+
'type': 'polyline',
43+
'latLngs': path,
44+
'options': {'color': 'red', 'weight': 3, 'opacity': 0.7}
45+
})

ngwidgets/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Version:
1717
name = "ngwidgets"
1818
version = ngwidgets.__version__
1919
date = "2023-09-10"
20-
updated = "2024-11-15"
20+
updated = "2024-12-03"
2121
description = "NiceGUI widgets"
2222

2323
authors = "Wolfgang Fahl"

ngwidgets/widgets_demo.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,43 @@ def show():
754754

755755
await self.setup_content_div(show)
756756

757+
async def show_leaflet(self):
758+
"""Show leaflet demo with locations and path drawing"""
759+
760+
def show():
761+
self.locations = {
762+
(48.486375, 8.375567): "Baiersbronn",
763+
(52.5200, 13.4049): "Berlin",
764+
(52.37487, 9.74168): "Hannover",
765+
(40.7306, -74.0060): "New York",
766+
(39.9042, 116.4074): "Beijing",
767+
(35.6895, 139.6917): "Tokyo"
768+
}
769+
self.zoom_level = 9
770+
771+
self.map = ui.leaflet().classes("w-full h-96")
772+
with ui.row():
773+
ui.select(
774+
self.locations,
775+
label="Location",
776+
on_change=lambda e: self.map.center(
777+
e.value, zoom=self.zoom_level
778+
)
779+
).classes("w-40")
780+
ui.select(
781+
{i: i for i in range(9,18)},
782+
label="Zoom",
783+
value=self.zoom_level,
784+
on_change=lambda e: self.map.zoom(e.value)
785+
).classes("w-40")
786+
ui.button("Draw Path", on_click=lambda: self.map.generic_layer({
787+
'type': 'polyline',
788+
'latLngs': [list(self.locations.keys())[1:3]],
789+
'options': {'color': 'red'}
790+
}))
791+
792+
await self.setup_content_div(show)
793+
757794
async def home(self):
758795
"""
759796
provide the main content page
@@ -770,6 +807,7 @@ def setup_home():
770807
"DictEdit": "/dictedit",
771808
"HideShow Demo": "/hideshow",
772809
"Lang": "/langs",
810+
"Leaflet Map": "/leaflet",
773811
"ListOfDictsGrid": "/grid",
774812
"Log Element Handler Demo": "/log_handler",
775813
"Tristate Demo": "/tristate",
@@ -848,6 +886,10 @@ async def show_progress(client: Client):
848886
async def show_langs(client: Client):
849887
return await self.page(client, NiceGuiWidgetsDemo.show_langs)
850888

889+
@ui.page("/leaflet")
890+
async def show_leaflet(client: Client):
891+
return await self.page(client, NiceGuiWidgetsDemo.show_leaflet)
892+
851893
@ui.page("/log_handler")
852894
async def show_log_handler_demo(client: Client):
853895
return await self.page(

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ readme = "README.md"
1616
license= "Apache-2.0"
1717
dependencies = [
1818
# https://nicegui.io/
19-
"nicegui>=2.1.0",
19+
"nicegui>=2.7.0",
2020
# orjson
2121
# https://pypi.org/project/orjson/
2222
"orjson>=3.9.10",

0 commit comments

Comments
 (0)