Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

Python Bokeh markup text value can't update

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.

How can I find where the problem is?

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...