How to check if my Bokeh Server Application is completely loaded and rendered?

Question:

I would like to integrate my Bokeh Server Application in Electron. So what I did is to run bokeh server using python-shell like this

mainWindow = new BrowserWindow({
    width: 1000,
    height: 700,
    show: false,
})

var PythonShell = require('python-shell');
var options = {
    mode: 'text',
    pythonPath: 'python3',
    pythonOptions: ['-m'],
    scriptPath: '',
    args: ['serve','bokeh_project/']
};

PythonShell.run('bokeh', options, function (err, results) {
    if (err) throw err;
    console.log('results: %j', results);
});

mainWindow.loadURL('http://localhost:5006');

mainWindow.once('did-finish-load', () => {
    mainWindow.show()
})

The problem here is that the window never pops up because electron does not detect the server as loaded.

Asked By: ChesuCR

||

Answers:

From Bokeh developer (I didn’t try it yet):

Noting that DocumentReady was added in 2.2 but currently only for Bokeh server apps, e.g.

curdoc().on_event(DocumentReady, ...)

We would like to add for CustomJS but this will be tricky due to implicit documents, might require requiring explicit document manipulation (to have a target doc to call js_on_event on)

I have also found a bunch of workarounds.

Workaround 1

So I had to add this setTimeout as a workaround. If I do not use this the page would be stuck forever.

setTimeout(function () {
    mainWindow.show();
    mainWindow.loadURL('http://localhost:5006');
}, 3000);

Workaround 2

It checks if the bokeh port is still closed. But the elements may be not loaded and completely loaded

var portscanner = require('portscanner')
var _checkServerStatus = setInterval(function() {
  portscanner.checkPortStatus(5006, '127.0.0.1', function(error, status) {
    if (status == 'open') {  // status = 'open' or 'close'
        clearInterval(_checkServerStatus);
        console.log('Server running');
        mainWindow.loadURL(bokehUrl); 
    }
  });
}, 100);  

Workaround 3

Finally I found another workaround to check if all the elements are completely rendered. The answer is in this question:

oldLog = console.log;
console.log = function (message) {
    if(message.localeCompare('Bokeh items were rendered successfully') == 0){
        window.top.postMessage('show-bokeh-iframe', '*')
        console.log = oldLog;
    }
    oldLog.apply(console, arguments);
};

Workaround 4

There is a GH issue where where the writer ask for calling a callback when bokeh is completely loaded and rendered. The user foobarbecue suggests to verify if the bokeh page is rendered with MutationObserver, but I have never used it.

Answered By: ChesuCR