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