Search code examples
pythonpython-3.xprintingbokehdashboard

How can I show and update a list of print statements in a Bokeh dashboard?


Is there a way to create a section of a bokeh dashboard that shows the console output from the python session?

I am creating a front-end dashboard with bokeh that runs a process that can take a while and does a lot of stuff. I wanted a section that would show some of the print statements that are executed along the way. Ideally I was hoping for a little widget type object that could display the output directly within the dashboard.


Solution

  • Just a simple example updating a Div element with the content of a list os messages (with html code). I think you can adapt this to your needs:

    from bokeh.layouts import column
    from bokeh.io import curdoc
    from bokeh.models import Button
    from bokeh.models.widgets import Div
    
    div = Div(
        text='',
        width=200,
        height=200
    )
    
    msg_list = []
    
    def update_div():
        msg_num = len(msg_list)
        msg_list.append('{}: New message'.format(msg_num))
        m = ''
        for msg in msg_list:
            m += '<li>{}</li>'.format(msg)
        div.text = '<ul>{}</ul>'.format(m)
    
    bt = Button(
        label="Update div",
        button_type="success",
        width=50
    )
    
    bt.on_click(update_div)
    
    curdoc().add_root(
        column(children=[bt, div])
    )