-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
render_widget now attaches it's return value (and as_widget() equival…
…ent) to the decorated function
- Loading branch information
Showing
5 changed files
with
128 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,50 @@ | ||
import ipyleaflet as L | ||
from htmltools import css | ||
from shiny import * | ||
from shiny import App, reactive, render, req, ui | ||
|
||
from shinywidgets import output_widget, reactive_read, register_widget | ||
from shinywidgets import output_widget, reactive_read, render_widget | ||
|
||
app_ui = ui.page_fillable( | ||
ui.div( | ||
app_ui = ui.page_sidebar( | ||
ui.sidebar( | ||
ui.input_slider("zoom", "Map zoom level", value=4, min=1, max=10), | ||
), | ||
ui.card( | ||
ui.output_text("map_bounds"), | ||
style=css( | ||
display="flex", justify_content="center", align_items="center", gap="2rem" | ||
), | ||
fill=False | ||
), | ||
ui.card( | ||
output_widget("lmap") | ||
), | ||
output_widget("map"), | ||
title="ipyleaflet demo" | ||
) | ||
|
||
|
||
def server(input, output, session): | ||
|
||
# Initialize and display when the session starts (1) | ||
map = L.Map(center=(52, 360), zoom=4) | ||
register_widget("map", map) | ||
@output | ||
@render_widget | ||
def lmap(): | ||
return L.Map(center=(52, 360), zoom=4) | ||
|
||
# When the slider changes, update the map's zoom attribute (2) | ||
@reactive.Effect | ||
def _(): | ||
map.zoom = input.zoom() | ||
lmap.widget.zoom = input.zoom() | ||
|
||
# When zooming directly on the map, update the slider's value (2 and 3) | ||
@reactive.Effect | ||
def _(): | ||
ui.update_slider("zoom", value=reactive_read(map, "zoom")) | ||
zoom = reactive_read(lmap.widget, "zoom") | ||
ui.update_slider("zoom", value=zoom) | ||
|
||
# Everytime the map's bounds change, update the output message (3) | ||
@output | ||
@render.text | ||
def map_bounds(): | ||
b = reactive_read(map, "bounds") | ||
b = reactive_read(lmap.widget, "bounds") | ||
req(b) | ||
lat = [b[0][0], b[0][1]] | ||
lon = [b[1][0], b[1][1]] | ||
return f"The current latitude is {lat} and longitude is {lon}" | ||
lat = [round(x) for x in [b[0][0], b[0][1]]] | ||
lon = [round(x) for x in [b[1][0], b[1][1]]] | ||
return f"The map bounds is currently {lat} / {lon}" | ||
|
||
|
||
app = App(app_ui, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters