posit-dev / py-shinywidgets

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

Problem Rendering Widgets as Outputs #73

Closed bleonard314 closed 1 year ago

bleonard314 commented 1 year ago

Description

I am trying to replicate the code examples provided and am able to register widgets without issue.

https://github.com/rstudio/py-shinywidgets/blob/main/examples/ipyleaflet/app.py

However, I do not get an output from this code, even if I modify the code to replicate just the leaflet example.

https://github.com/rstudio/py-shinywidgets/blob/main/examples/outputs/app.py

I believe this may be related to the following message I receive when running the code this way:

"GET /lib/ipywidget-libembed-amd-1.0.1%5Clibembed-amd.js HTTP/1.1" 404 Not Found

What I Did

import warnings

import pandas as pd
import numpy as np

from shiny import *
from shinywidgets import *
from htmltools import head_content, HTML

app_ui = ui.page_fluid(
    ui.layout_sidebar(
        ui.panel_sidebar(
            ui.input_radio_buttons(
                "framework",
                "Choose an ipywidget package",
                [
                    "ipyleaflet",
                ],
                selected="ipyleaflet",
            )
        ),
        ui.panel_main(
            ui.output_ui("figure"),
        ),
    ),
    title="ipywidgets in Shiny",
)

def server(input: Inputs, output: Outputs, session: Session):
    @output(id="figure")
    @render.ui
    def _():
        return output_widget(input.framework())

    @output(id="ipyleaflet")
    @render_widget
    def _():
        from ipyleaflet import Map, Marker

        m = Map(center=(52.204793, 360.121558), zoom=4)
        m.add_layer(Marker(location=(52.204793, 360.121558)))
        return m

app = App(app_ui, server)
cpsievert commented 1 year ago

Hmm, I'm not able to replicate the issue. How are you running the application?

bleonard314 commented 1 year ago

I'm running it through VS Code using the Shiny plug-in. I'll try cloning the project and running it on another system from the command line to see if the same error occurs. Thanks!

cpsievert commented 1 year ago

Would be interesting to know whether you can reproduce that 404 by using shiny run app.py from the command line...

bleonard314 commented 1 year ago

Okay, just had time to try a fresh clone, create a venv, install packages from the requirements, and shiny run app.py from the command line. Repeated this process on both my local Windows 10 machine and AVD. Received the same issue as before with the following log. Note that I am able to interact with the UI and select radio buttons, but am unable to view any UI outputs.


INFO:     Started server process [16180]
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:52592 - "GET / HTTP/1.1" 200 OK
INFO:     127.0.0.1:52594 - "GET /require.min.js HTTP/1.1" 200 OK
INFO:     127.0.0.1:52592 - "GET /shiny.min.css HTTP/1.1" 200 OK
INFO:     127.0.0.1:52595 - "GET /shiny.js HTTP/1.1" 200 OK
INFO:     127.0.0.1:52596 - "GET /jquery-3.6.0.min.js HTTP/1.1" 200 OK
INFO:     127.0.0.1:52605 - "GET /bootstrap.bundle.min.js HTTP/1.1" 200 OK
INFO:     127.0.0.1:52606 - "GET /bootstrap.min.css HTTP/1.1" 200 OK
INFO:     127.0.0.1:52606 - "GET /favicon.ico HTTP/1.1" 404 Not Found
INFO:     ('127.0.0.1', 52640) - "WebSocket /websocket/" [accepted]
INFO:     connection open
INFO:     127.0.0.1:52606 - "GET /lib/ipywidget-libembed-amd-1.0.1%5Clibembed-amd.js HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:52606 - "GET /lib/ipywidget-output-binding-0.1.4%5Coutput.js HTTP/1.1" 404 Not Found
bleonard314 commented 1 year ago

The presence of the hex code %5C makes me suspect that the issue may have to do with relative paths for windows vs. linux (backward vs. forward slash).

bleonard314 commented 1 year ago

One more thing for debugging. Replacing %5C with %2F or / locates these files. Perhaps a function like file.path is being too smart and converting part of an HTML path to a windows file path.

http://127.0.0.1:8000/lib/ipywidget-libembed-amd-1.0.1%2Flibembed-amd.js

bleonard314 commented 1 year ago

Okay, issue might actually have to do with htmltools not this package and it may be an issue with normalizePath not file.path. See suspected lines of code below. Perhaps try explicitly state normalizePath(path, winslash = "/", mustWork = TRUE). I also see that there is a comment about dependency$name needing to not have any slashes of either kind. Hopefully this is helpful, but I'm just shooting in the dark.

https://github.com/rstudio/htmltools/blob/95f27969c71b81bd07b216195e57e49efd1e9307/R/html_dependency.R#L344-L382

Akshay-Kalwar commented 1 year ago

I am still facing the same issue how can I get it resolved

wch commented 1 year ago

@Akshay-Kalwar Have you tried installing the latest version of htmltools (0.1.4)?

Akshay-Kalwar commented 1 year ago

@Akshay-Kalwar Have you tried installing the latest version of htmltools (0.1.4)?

Sorry my bad its working now thank you