In matplotlib 3.3.3 the all_figures.html tornado template used by webagg contains:
var websocket = new websocket_type("{{ ws_uri }}" + fig_id + "/ws");
var fig = new mpl.figure(fig_id, websocket, mpl_ondownload, figure_div);
fig.focus_on_mouseover = true;
fig.canvas.setAttribute("tabindex", fig_id);
}
However, this code may result in websocket not being ready/connected at the time that mpl.figure attempts to use it. Testing in Firefox, for example, yields Uncaught DOMException: An attempt was made to use an object that is not, or is no longer, usable
.
import matplotlib
matplotlib.use('webagg')
import matplotlib.pyplot as plt
matplotlib.rcParams['webagg.address']='0.0.0.0'
matplotlib.rcParams['webagg.port']=8988
matplotlib.rcParams['webagg.open_in_browser']=False
plt.plot([1,2,3])
plt.show()
then browse to http://localhost:8988
should yield the error in the javascript console. Nothing gets rendered.
To fix the problem, we wait for the socket to be open:
var websocket = new websocket_type("{{ ws_uri }}" + fig_id + "/ws");
websocket.onopen = function(e){
var fig = new mpl.figure(fig_id, websocket, mpl_ondownload, figure_div);
fig.focus_on_mouseover = true;
fig.canvas.setAttribute("tabindex", fig_id);
}
single_figure.html needs a similar fix.
Should I open a bug?
A.