posit-dev / py-shinywidgets

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

Support for IPython Audio #126

Closed Nelson-Gon closed 7 months ago

Nelson-Gon commented 7 months ago

Description

I was trying to insert_ui for an Audio IPython widget by doing something like

from IPython.display import display, Audio
ui.insert_ui(ui.tags.html(display(Audio(audio_file, **kwargs))))

My expectation was that this would recognize the generated IPython HTML and render it. It neither rendered not did it throw any error. I have played with output_widget and register_widget but it seems there is no audio support (or at least not one I am aware of) in ipywidgets. The end goal would be to have something like streamlit's audio UI but with autoplay, a feature they still lack

Thanks, NelsonGon

cpsievert commented 7 months ago

The Audio class doesn't inherit from the ipywidgets's Widget class, so it won't be compatible with {shinywidgets}, but since it has a _repr_html_ method, you can render it this way (this requires the development version of {htmltools}):

from IPython.display import Audio

from shiny import App, render, ui

app_ui = ui.page_fluid(
    ui.output_ui("my_audio"),
)

def server(input, output, session):

    @render.ui
    def my_audio():
        return Audio("http://www.nch.com.au/acm/8k16bitpcm.wav")

app = App(app_ui, server)
cpsievert commented 7 months ago

Or if using the new shiny express mode, this just becomes:

from IPython.display import Audio

import shiny
import shiny.express

Audio("http://www.nch.com.au/acm/8k16bitpcm.wav")