|
1 | 1 | import ipyleaflet as L
|
2 |
| -from htmltools import css |
3 |
| -from shiny import * |
4 |
| - |
5 |
| -from shinywidgets import output_widget, reactive_read, register_widget |
6 |
| - |
7 |
| -app_ui = ui.page_fillable( |
8 |
| - ui.div( |
9 |
| - ui.input_slider("zoom", "Map zoom level", value=4, min=1, max=10), |
10 |
| - ui.output_text("map_bounds"), |
11 |
| - style=css( |
12 |
| - display="flex", justify_content="center", align_items="center", gap="2rem" |
13 |
| - ), |
14 |
| - ), |
15 |
| - output_widget("map"), |
16 |
| -) |
17 |
| - |
18 |
| - |
19 |
| -def server(input, output, session): |
20 |
| - |
21 |
| - # Initialize and display when the session starts (1) |
22 |
| - map = L.Map(center=(52, 360), zoom=4) |
23 |
| - register_widget("map", map) |
24 |
| - |
25 |
| - # When the slider changes, update the map's zoom attribute (2) |
26 |
| - @reactive.Effect |
27 |
| - def _(): |
28 |
| - map.zoom = input.zoom() |
29 |
| - |
30 |
| - # When zooming directly on the map, update the slider's value (2 and 3) |
31 |
| - @reactive.Effect |
32 |
| - def _(): |
33 |
| - ui.update_slider("zoom", value=reactive_read(map, "zoom")) |
34 |
| - |
35 |
| - # Everytime the map's bounds change, update the output message (3) |
36 |
| - @output |
37 |
| - @render.text |
38 |
| - def map_bounds(): |
39 |
| - b = reactive_read(map, "bounds") |
40 |
| - req(b) |
41 |
| - lat = [b[0][0], b[0][1]] |
42 |
| - lon = [b[1][0], b[1][1]] |
43 |
| - return f"The current latitude is {lat} and longitude is {lon}" |
| 2 | +from shiny import reactive, render, req |
| 3 | +from shiny.express import input, ui |
| 4 | + |
| 5 | +from shinywidgets import reactive_read, render_widget |
| 6 | + |
| 7 | +ui.page_opts(title="ipyleaflet demo") |
| 8 | + |
| 9 | +with ui.sidebar(): |
| 10 | + ui.input_slider("zoom", "Map zoom level", value=4, min=1, max=10) |
44 | 11 |
|
| 12 | +@render_widget |
| 13 | +def lmap(): |
| 14 | + return L.Map(center=(52, 360), zoom=4) |
45 | 15 |
|
46 |
| -app = App(app_ui, server) |
| 16 | +# When the slider changes, update the map's zoom attribute |
| 17 | +@reactive.Effect |
| 18 | +def _(): |
| 19 | + lmap.widget.zoom = input.zoom() |
| 20 | + |
| 21 | +# When zooming directly on the map, update the slider's value |
| 22 | +@reactive.Effect |
| 23 | +def _(): |
| 24 | + zoom = reactive_read(lmap.widget, "zoom") |
| 25 | + ui.update_slider("zoom", value=zoom) |
| 26 | + |
| 27 | + |
| 28 | +with ui.card(fill=False): |
| 29 | + # Everytime the map's bounds change, update the output message |
| 30 | + @render.ui |
| 31 | + def map_bounds(): |
| 32 | + b = reactive_read(lmap.widget, "bounds") |
| 33 | + req(b) |
| 34 | + lat = [round(x) for x in [b[0][0], b[0][1]]] |
| 35 | + lon = [round(x) for x in [b[1][0], b[1][1]]] |
| 36 | + return f"The map bounds is currently {lat} / {lon}" |
0 commit comments