posit-dev / py-shinywidgets

Render ipywidgets inside a PyShiny app
MIT License
41 stars 2 forks source link

@reactive.Effect not rendered #102

Closed pckroon closed 11 months ago

pckroon commented 12 months ago

Hi all,

I'm still running into #56, so as directed I'm opening a new issue.

For the code below, the slider is there and works, but there is no string 'The value of the slider is: {reactive_read(s, 'value')}' to be found.

from shiny import *
from shinywidgets import *
import ipywidgets as ipy
from ipywidgets.widgets.widget import Widget

app_ui = ui.page_fluid(output_widget("slider"), ui.output_text("value"))

def server(input: Inputs, output: Outputs, session: Session):
    s: Widget = ipy.IntSlider(
        value=7,
        min=0,
        max=10,
        step=1,
        description="Test:",
        disabled=False,
        continuous_update=False,
        orientation="horizontal",
        readout=True,
        readout_format="d",
    )

    register_widget("slider", s)

    @reactive.Effect
    def _():
        return f"The value of the slider is: {reactive_read(s, 'value')}"

app = App(app_ui, server, debug=True)
cpsievert commented 12 months ago

@reactive.Effect is for triggering side-effect actions (not returning a value for display in an output). Try changing:

@reactive.Effect

to

@output("value")
@render.text
pckroon commented 12 months ago

That makes a uniformly grey page, with the following console output:

INFO:     Started server process [1317270]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:59456 - "GET / HTTP/1.1" 200 OK
INFO:     ('127.0.0.1', 59466) - "WebSocket /websocket/" [accepted]
SEND: {"config": {"workerId": "", "sessionId": "e5fc3c14e345e05874667c332cadba15e40864a8292e50f3eaa7957908e5cb08", "user": null}}
INFO:     connection open
RECV: {"method":"init","data":{".clientdata_output_slider_width":1896,".clientdata_output_slider_height":0,".clientdata_output_slider_bg":"rgb(255, 255, 255)",".clientdata_output_slider_fg":"rgb(33, 37, 41)",".clientdata_output_slider_accent":"rgb(13, 110, 253)",".clientdata_output_slider_font":{"families":["system-ui","-apple-system","Segoe UI","Roboto","Helvetica Neue","Noto Sans","Liberation Sans","Arial","sans-serif","Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji"],"size":"16px"},".clientdata_output_slider_hidden":false,".clientdata_output_value_hidden":false,".clientdata_pixelratio":1,".clientdata_url_protocol":"http:",".clientdata_url_hostname":"127.0.0.1",".clientdata_url_port":"8000",".clientdata_url_pathname":"/",".clientdata_url_search":"",".clientdata_url_hash_initial":"",".clientdata_url_hash":"",".clientdata_singletons":""}}
SEND: {"busy": "busy"}
SEND: {"busy": "busy"}
_send_error_response: 'str' object has no attribute '__name__'
remove_session: e5fc3c14e345e05874667c332cadba15e40864a8292e50f3eaa7957908e5cb08
INFO:     connection closed
cpsievert commented 12 months ago

Ack, whoops, sorry, it needs to be @output(id="value"), not @output("value")

pckroon commented 12 months ago

That works! The example where I got my initial code in https://github.com/rstudio/py-shinywidgets/blob/main/examples/ipywidgets/app.py should probably be changed?

nealrichardson commented 12 months ago

That works! The example where I got my initial code in https://github.com/rstudio/py-shinywidgets/blob/main/examples/ipywidgets/app.py should probably be changed?

Sounds like it, would you be interested in making a PR for that?

FTR I put the corrected example on shinylive here in order to see it working.

pckroon commented 12 months ago

Sounds like it, would you be interested in making a PR for that?

Sure, I'll have some time later this week.