プログラミング/julia/gist との連携 のバックアップソース(No.1)

更新

[[プログラミング/julia]]

* nbextensions の導入 [#l72e9293]

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

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

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

&ref(プログラミング/julia/jupyter-nbextentions.png,border,50%);

* Gist との連携 [#n259433c]

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 を貼り付ける

&ref(プログラミング/julia/jupyter-nbextentions-gistit.png,border,50%);

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

&ref(プログラミング/julia/jupyter-gistit-button.png,border,50%);

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

&ref(プログラミング/julia/jupyter-gistit1.png,border,50%);

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

Chrome の デベロッパーツールで見てみると、api.github.com へ向けた通信のステータスが
(canceled) となっていた。コンソールには github ajax error が表示されて、status が
404 になっていた?!

実際にアクセスしてエラーになる場合には (canceled) にならないので、
何かがおかしい気がする・・・

gists の api はこうなっている:https://developer.github.com/v3/gists/

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

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

&ref(プログラミング/julia/nbextensions-gistit-ajax-error.png,border,50%);

Firefox で同じ事をやったら、

 <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 でアクセスしに行ってる感じ?

でも、https://github.com/ipython-contrib/jupyter_contrib_nbextensions/blob/master/src/jupyter_contrib_nbextensions/nbextensions/gist_it/main.js#L444 を見る限りそんなわけはないんだよなあ・・・

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

 LANG:console
 $ 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 で試してみた [#v9742a0e]

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

https://twitter.com/genkuroki/status/981373531605757959

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

https://gist.github.com/osamutake/e270ac92e0fb6a937dc6c81e922906ae

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

ふむ。

アクセスは 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 ではうまく行った [#p3561eec]

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

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

https://gist.github.com/osamutake/e6f656fafca463f7a738aaced256b43a

とりあえず進めて、またエラーが出始めたら詳しく条件を探ろうと思う。


Counter: 3270 (from 2010/06/03), today: 1, yesterday: 0