プログラミング/julia/gist との連携

更新 (昨日 2 / 今日 0 / 総数 4,882)


プログラミング/julia

nbextensions の導入†

http://jupyter-contrib-nbextensions.readthedocs.io/en/latest/install.html

$ sudo python3 -m pip install jupyter_contrib_nbextensions
$ jupyter contrib nbextension install --user

とすると、jupyter notebook にて Nbextentions というタブを開けるようになる。

jupyter-nbextentions.png

Gist との連携†

http://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/gist_it/readme.html

  • github へログインした後、https://github.com/settings/tokens の "Generate a personal access token" リンクをクリック
    • Token description に "jupyter notebook" など分かりやすい説明を付ける
    • "□ gist | Create gists" をチェック
    • "Generate Token" をクリック
    • Token をコピー
  • jupyter notebook の Nbextentions タブで Gist-it にチェックを付ける
  • gist-it をクリックして現れる "Github personal access token" フィールドに Token を貼り付ける

jupyter-nbextentions-gistit.png

これで、適当な notebook を開くと、Gist it ボタンが現れる。

jupyter-gistit-button.png

ボタンを押して description を記入し、Gist it! を押す。

jupyter-gistit1.png

あれ?ajax エラーが出たぞ???

nbextensions-gistit-ajax-error.png

Chrome の デベロッパーツールで見てみると、api.github.com へ向けた通信のステータスが (canceled) となっていた。

コンソールには github ajax error が表示されて、status が 404 になっていた?!

実際にアクセスしてエラーになる場合には (canceled) にならないので、 実際の通信が行われる前にエラーになっているみたい。

画面に表示されるエラーメッセージ "The ajax request to Github went wrong:" で検索しても黒木さんの https://mathtod.online/@genkuroki/347159 が引っかかるだけっていうのは非常に謎だ。

よほど珍しいことになってる???

Firefox で同じ事をやったら、

「ネットワーク」のところに api.github.com との通信履歴が生じなかった。 やはり実際の通信が生じる前にエラーになっている。

そして、

<script> 要素の ‘src’ 属性が正しい URI ではありません: “https://api.github.com/gists?{"files":{"test-plotly.ipynb":{"content":"{\n  \"cells\": [\n    {\n      \"metadata\": {},\n      \"cell_type\": \"markdown\",\n      \"source\": \"# plotly利用のテスト\"\n    },\n    {\n      \"metadata\": {},\n      \"cell_type\": \"markdown\",\n      \"source\": \"データを作成\"\n    },\n    {\n      \"metadata\": {\n        \"trusted\": true\n      },\n      \"cell_type\": \"code\",\n      \"source\": \"import numpy as np\\ndata_x = np.linspace(0, 100, 10)\\ndata_ys = np.random.random(size=(3, len(data_x)))\\nlabels = ['hoge', 'fuga', 'piyo']\",\n      \"execution_count\": 6,\n      \"outputs\": []\n    },\n    {\n      \"metadata\": {},\n      \"cell_type\": \"markdown\",\n      \"source\": \"グラフ表示のおまじない\"\n    },\n    {\n      \"metadata\": {\n        \"trusted\": true,\n        \"scrolled\": true\n      },\n      \"cell_type\": \"code\",\n      \"source\": \"import plotly.offline as po\\nimport plotly.graph_objs as go\\np…

というエラーも表示された。

何のことだか分からないけど、なぜか post ではなく get でアクセスしに行ってる感じ?

gists の api はこう:https://developer.github.com/v3/gists/

当然、新しい記事を投稿するには post を使う。

でも、https://github.com/ipython-contrib/jupyter_contrib_nbextensions/blob/6af8e5e84e4746476c5b476b7e38f63d7abb2064/src/jupyter_contrib_nbextensions/nbextensions/gist_it/main.js#L444 を見る限り get でアクセスするわけはないんだよなあ・・・

たぶん、ディベロッパーツールの見方が悪いだけだ。

上記のファイルは ~/.local/share/jupyter/nbextensions/gist_it/main.js にあるので、 $ajax の前に次のデバッグ用のコードを追加した。

$ jed ~/.local/share/jupyter/nbextensions/gist_it/main.js
 +        console.log({
 +            url: 'https://api.github.com/gists' + (id ? '/' + id : ''),
 +            type: method,
 +            dataType: 'json',
 +            data: JSON.stringify(data),
 +            beforeSend: add_auth_token,
 +            success: gist_success,
 +            error: gist_error,
 +            complete: complete_callback
 +        });

結果は、

Object { 
 url: "https://api.github.com/gists", 
 type: "POST", 
 dataType: "json", 
 data: "{\"files\":{\"test-plotly.ipynb\":{\"content\":\"{\\n  \\\"cells\\\": [\\n    {\\n      \\\"metadata\\\": {},\\n      \\\"cell_type\\\": \\\"markdown\\\",\\n      \\\"source\\\": \\\"# jupyterからplotlyを利用する(python)\\\"\\n    },\\n    {\\n      \\\"metadata\\\": {},\\n      \\\"cell_type\\\": \\\"markdown\\\",\\n      \\\"source\\\": \\\"https://qiita.com/deaikei/items/b71c1ca2c4c2a2c37526 を元に試す\\\\n\\\\nまずデータを作成\\\"\\n    },\\n    {\\n      \\\"metadata\\\": {\\n        \\\"trusted\\\": true\\n      },\\n      \\\"cell_type\\\": \\\"code\\\",\\n      \\\"source\\\": \\\"import numpy as np\\\\ndata_x = np.linspace(0, 100, 10)\\\\ndata_ys = np.random.random(size=(3, len(data_x)))\\\\nlabels = ['hoge', 'fuga', 'piyo']\\\",\\n      \\\"execution_count\\\": 6,\\n      \\\"outputs\\\": []\\n    },\\n    {\\n      \\\"metadata\\\": {},\\n      \\\"cell_type\\\": \\\"markdown\\\",\\n      \\\"source\\\": \\\"グラフ表示のおまじない\\\"\\n    },\\n    {\\n      \\\"metadata\\\": {\\n        \\\"scrolled\\\": true,\\n        \\\"trusted\\\": true\\n      },\\n      \\\"cell_type\\\": \\\"code\\\",\\n      \\\"source\\\": \\\"import plotly.offline as po\\\\nimport plotly.graph_o…", 
 beforeSend: add_auth_token(), 
 success: gist_success(), 
 error: gist_error(), 
 complete: click() 
}

data の中味は

{
  description: "jupyterからplotlyを利用する(python)",
  files: {
    "test-plotly.ipynb": Object { content: "{\n  \"cells\": [\n ..." }
  },
  public: false
}

となっていて、特に問題なさそうに見える。

でも gist_error で次のエラーが表示される。

jqXHR = {
  ...
  readyState: 4
  status: 404
  statusText: "error"
}

status = 404 は Not Found で、通信もしていないのにこのエラーが出るのはおかしいなあ。。。

黒木さんに教えていただき juliabox で試してみた†

困ったなー、とつぶやいたところ、なんと黒木さんからメッセージを頂いてしまった。

ということで、juliabox でやってみたところ、

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

何の問題も無くできてしまった。

ふむ。

アクセスは https://api.github.com/gists への POST で、 要求ヘッダーは

Accept:  "application/json, text/javascript, */*; q=0.01"
Accept-Encoding:  "gzip, deflate, br"
Accept-Language:  "ja,en-US;q=0.7,en;q=0.3"
Authorization:  "token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Connection:  "keep-alive"
Content-Length:  1329
Content-Type:  "application/x-www-form-urlencoded; charset=UTF-8"
Host:  "api.github.com"
Origin:  "https://juliabox.com"
Referer:  "https://juliabox.com/notebook/notebooks/hello-juliabox.ipynb"
User-Agent:  "Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/59.0"

body は、

{
  "files": {
    "hello-juliabox.ipynb": { 
      "content": "{\n \"cells\": [\n {\n \"metadata\": {\n \"trusted\": true\n }, ..."
    }
  },
  "description": "julia hello",
  "public":true
}

なので、特に変わったところはない。

ファイルが大きすぎたらしい?†

先に試した notebook は plotly を含んでいたため、 実はあれだけでファイルサイズが 5メガバイト もあった。

Hello world だけで試したら何の問題も無かった。

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

でも黒木さんによると、

とのことで、ファイルサイズの問題ではないらしい。

とりあえず進めて、本当に使いたいファイルでエラーが出たらまた詳しく条件を探ろうと思う。

コメント・質問†