-
Notifications
You must be signed in to change notification settings - Fork 6
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 to the decorated funct…
…ion (#119) Co-authored-by: Barret Schloerke <[email protected]>
- Loading branch information
Showing
15 changed files
with
495 additions
and
335 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
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,37 +1,24 @@ | ||
import altair as alt | ||
from shiny import App, render, ui | ||
import shiny.express | ||
from shiny import render | ||
from vega_datasets import data | ||
|
||
from shinywidgets import output_widget, reactive_read, register_widget | ||
from shinywidgets import reactive_read, render_altair | ||
|
||
source = data.cars() | ||
|
||
app_ui = ui.page_fluid( | ||
ui.output_text_verbatim("selection"), | ||
output_widget("chart") | ||
) | ||
# Output selection information (click on legend in the plot) | ||
@render.text | ||
def selection(): | ||
pt = reactive_read(jchart.widget.selections, "point") | ||
return "Selected point: " + str(pt) | ||
|
||
def server(input, output, session): | ||
|
||
# Replicate JupyterChart interactivity | ||
# https://altair-viz.github.io/user_guide/jupyter_chart.html#point-selections | ||
# Replicate JupyterChart interactivity | ||
# https://altair-viz.github.io/user_guide/jupyter_chart.html#point-selections | ||
@render_altair | ||
def jchart(): | ||
brush = alt.selection_point(name="point", encodings=["color"], bind="legend") | ||
chart = alt.Chart(source).mark_point().encode( | ||
return alt.Chart(data.cars()).mark_point().encode( | ||
x='Horsepower:Q', | ||
y='Miles_per_Gallon:Q', | ||
color=alt.condition(brush, 'Origin:N', alt.value('grey')), | ||
).add_params(brush) | ||
|
||
jchart = alt.JupyterChart(chart) | ||
|
||
# Display/register the chart in the app_ui | ||
register_widget("chart", jchart) | ||
|
||
# Reactive-ly read point selections | ||
@output | ||
@render.text | ||
def selection(): | ||
pt = reactive_read(jchart.selections, "point") | ||
return "Selected point: " + str(pt) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,36 @@ | ||
import ipyleaflet as L | ||
from htmltools import css | ||
from shiny import * | ||
|
||
from shinywidgets import output_widget, reactive_read, register_widget | ||
|
||
app_ui = ui.page_fillable( | ||
ui.div( | ||
ui.input_slider("zoom", "Map zoom level", value=4, min=1, max=10), | ||
ui.output_text("map_bounds"), | ||
style=css( | ||
display="flex", justify_content="center", align_items="center", gap="2rem" | ||
), | ||
), | ||
output_widget("map"), | ||
) | ||
|
||
|
||
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) | ||
|
||
# When the slider changes, update the map's zoom attribute (2) | ||
@reactive.Effect | ||
def _(): | ||
map.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")) | ||
|
||
# Everytime the map's bounds change, update the output message (3) | ||
@output | ||
@render.text | ||
def map_bounds(): | ||
b = reactive_read(map, "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}" | ||
from shiny import reactive, render, req | ||
from shiny.express import input, ui | ||
|
||
from shinywidgets import reactive_read, render_widget | ||
|
||
ui.page_opts(title="ipyleaflet demo") | ||
|
||
with ui.sidebar(): | ||
ui.input_slider("zoom", "Map zoom level", value=4, min=1, max=10) | ||
|
||
@render_widget | ||
def lmap(): | ||
return L.Map(center=(52, 360), zoom=4) | ||
|
||
app = App(app_ui, server) | ||
# When the slider changes, update the map's zoom attribute | ||
@reactive.Effect | ||
def _(): | ||
lmap.widget.zoom = input.zoom() | ||
|
||
# When zooming directly on the map, update the slider's value | ||
@reactive.Effect | ||
def _(): | ||
zoom = reactive_read(lmap.widget, "zoom") | ||
ui.update_slider("zoom", value=zoom) | ||
|
||
|
||
with ui.card(fill=False): | ||
# Everytime the map's bounds change, update the output message | ||
@render.ui | ||
def map_bounds(): | ||
b = reactive_read(lmap.widget, "bounds") | ||
req(b) | ||
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}" |
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
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 |
---|---|---|
|
@@ -4,13 +4,32 @@ | |
__email__ = "[email protected]" | ||
__version__ = "0.2.4.9000" | ||
|
||
from ._as_widget import as_widget | ||
from ._dependencies import bokeh_dependency | ||
from ._shinywidgets import ( | ||
as_widget, | ||
output_widget, | ||
reactive_read, | ||
register_widget, | ||
from ._output_widget import output_widget | ||
from ._render_widget import ( | ||
render_altair, | ||
render_bokeh, | ||
render_plotly, | ||
render_pydeck, | ||
render_widget, | ||
) | ||
from ._shinywidgets import reactive_read, register_widget | ||
|
||
__all__ = ("output_widget", "register_widget", "render_widget", "reactive_read", "bokeh_dependency", "as_widget") | ||
__all__ = ( | ||
# Render methods first | ||
"render_widget", | ||
"render_altair", | ||
"render_bokeh", | ||
"render_plotly", | ||
"render_pydeck", | ||
# Reactive read second | ||
"reactive_read", | ||
# UI methods third | ||
"output_widget", | ||
# Other methods last | ||
"as_widget", | ||
"bokeh_dependency", | ||
# Soft deprecated | ||
"register_widget", | ||
) |
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
Oops, something went wrong.