I want to customize the audio player (essentially remove the volume control and the time display).
The following code is working but when I remove the “controls” argument in the <audio>
and add javascript to add a play/pause button, the javascript code is never executed. What is the correct way to call javascript in panel?
import panel as pn
import os
def create_audio_player(wav_file_path):
"""
Creates an audio player from a static WAV file.
Args:
wav_file_path: Absolute path to the WAV file.
Returns:
Panel HTML pane containing the audio player, and the audio directory.
"""
if not os.path.exists(wav_file_path):
return pn.pane.Alert(f"Audio file not found: {wav_file_path}", alert_type="danger"), None
audio_filename = os.path.basename(wav_file_path)
audio_dir = os.path.dirname(os.path.abspath(wav_file_path))
html_pane = pn.pane.HTML(
f"""
<audio controls>
<source src="/audio/{audio_filename}" type="audio/wav">
Your browser does not support the audio element.
</audio>
"""
)
return html_pane, audio_dir
if __name__ == '__main__':
wav_file_path = "/home/toto/wav/20101018.2048.LCP_CaVousRegarde.wav" #replace with your audio file.
html_pane, audio_dir = create_audio_player(wav_file_path)
if audio_dir is not None:
pn.serve(
pn.Column(pn.pane.Markdown("## Audio Player"), html_pane),
static_dirs={"audio": audio_dir}, # Serve the audio directory at /audio
dev=True,
)
else:
pn.serve(html_pane)