main.py 997 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/env python3
  2. import leaflet # this module wraps the JavaScript lib leafletjs.com into an easy-to-use NiceGUI element
  3. from nicegui import Client, ui
  4. from nicegui.events import ValueChangeEventArguments
  5. @ui.page('/')
  6. async def main_page(client: Client):
  7. map = leaflet.map()
  8. locations = {
  9. (52.5200, 13.4049): 'Berlin',
  10. (40.7306, -74.0060): 'New York',
  11. (39.9042, 116.4074): 'Beijing',
  12. (35.6895, 139.6917): 'Tokyo',
  13. }
  14. async def handle_location_change(e: ValueChangeEventArguments) -> None:
  15. with e.client:
  16. await map.set_location(e.value)
  17. selection = ui.select(locations, on_change=handle_location_change).style('width: 10em')
  18. await client.handshake() # all code below is executed after client is connected
  19. default_location = next(iter(locations))
  20. # this will trigger the map.set_location event; which is js and must be run after client has connected
  21. selection.set_value(default_location)
  22. ui.run()