1

有没有办法自定义 panel.pane.Audio 元素?我尝试传递诸如宽度或高度之类的参数,例如:

pn_audio  = pn.pane.Audio(audio_data, sample_rate=sps, width=800)

但作为散景应用程序启动后没有效果。

这是一些可用的代码:

import numpy as np
import holoviews as hv
import holoviews.plotting.bokeh
from holoviews import opts
import panel as pn

from bokeh.application import Application
from bokeh.application.handlers.function import FunctionHandler
from bokeh.server.server import Server


import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)

def generate_audio():
    sps = 25600 # Samples per second
    duration = 10 # Duration in seconds

    modulator_frequency = 2.0
    carrier_frequency = 120.0
    modulation_index = 2.0

    time = np.arange(sps*duration) / sps
    modulator = np.sin(2.0 * np.pi * modulator_frequency * time) * modulation_index
    carrier = np.sin(2.0 * np.pi * carrier_frequency * time)
    waveform = np.sin(2. * np.pi * (carrier_frequency * time + modulator))

    waveform_quiet = waveform * 0.3
    waveform_int = np.int16(waveform_quiet * 32767)

    return waveform_int, sps

def modify_doc(doc):
    audio_data, sps = generate_audio()

    hv_curve = hv.Curve( audio_data).opts(width=800)

    pn_curve  = pn.panel(hv_curve)
    pn_audio  = pn.pane.Audio(audio_data, sample_rate=sps)
    # pn_txt = pn.widgets.TextInput(name='A widget', value='A string')

    plot = pn.Column(*[pn_curve,pn_audio])

    doc.add_root(plot.get_root())
    return doc

app = Application(FunctionHandler(modify_doc))
server = Server(app, address=IPAddr,
                allow_websocket_origin=['{}:5006'.format(IPAddr)],port=5006)



server.start()
print('Opening Bokeh application on {}'.format(IPAddr))
server.io_loop.add_callback(server.show, "/")
server.io_loop.start()

我从命令行启动应用程序

python start_bokeh_server.py

目标是让音频元素与上图一样宽。现在它看起来像那个散景应用程序

关于如何完成这项工作的任何想法,而无需深入研究面板的 javascript 部分?提前致谢。

4

0 回答 0