Python Bokeh标记文本值无法更新 [英] Python Bokeh markup text value can't update

查看:61
本文介绍了Python Bokeh标记文本值无法更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Bokeh服务器来承载SQL查询功能,因此我想在网页上打印查询状态,因此我使用了Markup小部件并更新了它的文本值.但是,正在运行查询,请稍候..."的信息永远不会显示,完成函数执行后只能显示最后一个值.

I'm building Bokeh server to host an SQL query function, on the webpage I wanna print the query status so I used a Markup widget and updates text value of it. However the message of "Running query, please wait..." never show up, only the last value can display when function execution is done.

如何找到问题所在?

下面是我的代码的一部分:

Below is part of my code:

def submit_query():
    para_status.text = "Running query, please wait..."
    cnx = mysql.connector.connect(**SQL_config)
    query = clause_build()  #call the query clause from another def
    df = pd.read_sql(query, cnx)
    cnx.close()
    if df.size >0:
        para_status.text = ("Data query is done, total " + \
        str(int(df.shape[0])) + " rows.")
        df.to_csv(dpath + './query_result.csv', index = False)
    else:
        para_status.text = "Oops, result is empty, please refine the filters and try again."

para_status = PreText(text = '')

btn_submit = Button(label = "Submit query", button_type="success")
btn_submit.on_click(submit_query)

page = column(btn_submit, para_status)

推荐答案

当回调结束时,散景状态仅与浏览器同步.如果要进行一些更新,然后进行大量阻塞性工作,然后进行另一项更新,则需要进行拆分,以便第一个回调立即完成,然后安排其余工作在返回之后进行.最简单的方法是使用add_next_tick_callback:

Bokeh state only synchronizes with the browser when the callback ends. If you want to do some update, then alot of blocking work, then another update, you will need to split things up so the first callback completes immediately then schedules the rest of the work to happen after the return. The simplest way is with add_next_tick_callback:

from time import sleep
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import Button, Div

d = Div(text="start")

b = Button()

def work():
    sleep(2)
    d.text = "end"

def cb():
    d.text = "middle"
    curdoc().add_next_tick_callback(work)

b.on_click(cb)

curdoc().add_root(column(d, b))

这篇关于Python Bokeh标记文本值无法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆