nbconvert --to html で html コンバートしたノートブックで plotly のグラフが描画されない問題の解決

環境

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.6
BuildVersion:   18G4032

$ pip list | grep -E "jupyter|plotly"
jupyter                            1.0.0
jupyter-client                     5.2.4
jupyter-console                    6.0.0
jupyter-core                       4.4.0
jupyterlab                         2.1.2
jupyterlab-server                  1.1.3
plotly                             4.9.0

現象

  • 以下のコマンドで nbconvert --to html で html コンバートしたノートブックで plotly のグラフが描画されない
$ jyputer nbconvert --to html hoge.ipynb
  • 作成した html を開くと以下のようにグラフが表示されない

f:id:tmks0820:20200730161842p:plain

  • エラーを確認すると以下の通り
Uncaught Error: Script error for: plotly
http://requirejs.org/docs/errors.html#scripterror
    C https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js:8
    onScriptError https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js:29

f:id:tmks0820:20200730161958p:plain

解決策

  • plotly.graph_objects.Figure.show を実行するときに fig.show(renderer="jupyterlab")renderer オプションを付ければよい。

  • jupyter を使っている場合には、fig.show(renderer="jupyter") と指定すれば良い。