You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQt5嵌入Bokeh图表报错:s.replaceAll is not a function

在PyQt5的QtWebEngineWidget中嵌入Bokeh图表时出现JavaScript错误

我尝试创建一个PyQt5应用,在QtWebEngineWidget中显示Bokeh图表,但运行后出现以下错误:

js: Uncaught (in promise) TypeError: s.replaceAll is not a function

为验证可行性,我参考了一段旧代码,为适配新版Bokeh,将:

p = plotting.figure(plot_width=300, plot_height=300)

修改为:

p = plotting.figure(width=300, height=300)

点击“Submit”按钮后,未显示预期的图表,终端输出上述错误。

使用的虚拟环境版本信息:

  • Python: 3.12.6
  • PyQt5: 5.15.11
  • PyQtWebEngine: 5.15.7
  • Bokeh: 3.6.0

完整代码如下:

import pandas as pd
from bokeh import plotting, embed, resources
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        button = QtWidgets.QPushButton("Submit")
        self.m_output = QtWebEngineWidgets.QWebEngineView()

        button.clicked.connect(self.on_button_clicked)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(button)
        lay.addWidget(self.m_output)
        self.resize(640, 480)

    @QtCore.pyqtSlot()
    def on_button_clicked(self):
        p = plotting.figure(width=300, height=300)
        data = {"Day": [0, 1, 2, 3, 0, 1], "Num": [0, 0, 1, 1, 2, 3]}
        df = pd.DataFrame(data)
        p.hexbin(df.Day, df.Num, size=0.5)
        html = embed.file_html(p, resources.CDN, "my plot")
        self.m_output.setHtml(html)

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    w = Widget()
    w.show()

    sys.exit(app.exec_())

内容的提问来源于stack exchange,提问作者ole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 17:30:04