Skip to main content
 首页 » 编程设计

python之在 Flask 中嵌入 Panel 应用程序之图在小部件更改时不更新

2024年11月24日10sharpest

我正在尝试将 Panel 应用程序嵌入到 Flask 应用程序中。当小部件更改时,我正在努力让应用程序更新。为了重现这个问题,我使用了 sea temperature example from Bokeh包含在面板文档中。

当使用 panel.show() 时,Panel 应用程序本身可以正常工作 - 当 slider 小部件值更改时,绘图会更新。

应用程序的仅 Bokeh 版本(不使用 Panel,如示例中所示)在使用 Flask 时也能正常工作。

在我尝试将面板嵌入 Flask 时, slider 不再更新图表。

我怀疑我在 modify_doc 函数中遗漏了一些东西,以便正确处理请求。

这是代码

from flask import Flask, render_template 
 
from bokeh.embed import server_document 
from bokeh.models import ColumnDataSource 
from bokeh.plotting import figure 
import param 
import panel as pn 
from bokeh.server.server import Server 
from tornado.ioloop import IOLoop 
 
from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature 
 
 
class SeaSurface(param.Parameterized): 
 
    smoothing = param.Integer(0,bounds=(0, 30)) 
 
    def __init__(self, **params): 
        super(SeaSurface, self).__init__(**params) 
        self.df = sea_surface_temperature.copy() 
        self.source = ColumnDataSource(data=self.df) 
 
        self.plot = figure(x_axis_type='datetime', y_range=(0, 25), y_axis_label='Temperature (Celsius)', 
                title="Sea Surface Temperature at 43.18, -70.43") 
 
        self.plot.line('time', 'temperature', source=self.source) 
 
    @param.depends('smoothing') 
    def view(self): 
        if self.smoothing == 0: 
            data = self.df 
        else: 
            data = self.df.rolling('{0}D'.format(self.smoothing)).mean() 
 
        self.source.data = ColumnDataSource(data=data).data 
 
        return self.plot 
 
    def panel(self): 
        return pn.Row(self.param, self.view) 
 
 
# uncomment to run app directly out of panel and comment Flask setup below 
# sea = SeaSurface(name='Sea Surface') 
# sea.panel().show() 
 
# Flask setup 
app = Flask(__name__) 
 
def modify_doc(doc): 
    sea = SeaSurface(name='Sea Surface') 
    doc.add_root(sea.panel().get_root()) 
 
 
@app.route('/', methods=['GET']) 
def bkapp_page(): 
    script = server_document('http://localhost:5006/bkapp') 
    return render_template("bokeh_example.html", script=script, template="Flask") 
 
 
def bk_worker(): 
    # Can't pass num_procs > 1 in this configuration. If you need to run multiple 
    # processes, see e.g. flask_gunicorn_embed.py 
    server = Server({'/bkapp': modify_doc}, io_loop=IOLoop(), allow_websocket_origin=["127.0.0.1:8000"]) 
    server.start() 
    server.io_loop.start() 
 
 
from threading import Thread 
Thread(target=bk_worker).start() 
 
if __name__ == '__main__': 
    print('Opening single process Flask app with embedded Bokeh application on http://localhost:8000/') 
    print() 
    print('Multiple connections may block the Bokeh app in this configuration!') 
    print('See "flask_gunicorn_embed.py" for one way to run multi-process') 
    app.run(port=8000) 
 

顺便说一句,面板看起来很棒 - 认为这是 Python 仪表板的答案

请您参考如下方法:

几乎在我发布后就解决了我自己的问题。

modify_doc 函数应该在对 get_root() 的调用中包含原始 doc 变量,因此 get_root(doc) 而不是 get_root(),如下所示:

def modify_doc(doc): 
    sea = SeaSurface(name='Sea Surface') 
    doc.add_root(sea.panel().get_root(doc))