Downloading Jupyter notebook from external URL using CLI

Viewed 6842

I need to download .ipynb-file in a terminal and than run it (using nbconvert or something like that).

  1. I tried use wget and curl for this, but I got web page (with tags and Javascript code) instead of runnable Notebook.

  2. Then I used Jupyter REST API. To get a notebook's content I used /api/contents/<path>/<file> I.e. I simply changed notebooks to api/contents in URL. I got a content of the notebook with some additional text (I'll have to filter it later), and... in a form of one line. So, I have a lot of work to convert this one line into a working notebook.

Is it possible to download Jupyter notebook via terminal and to get working .ipynb-file (the same as file can be downloaded by download as .ipynb in Jupyter Web UI).

3 Answers

You have to use the Authorization header and Content-Type application/json and then the notebook will be in the "content" part of the dictionary that is returned. You can use jq to get the content part of the dictionary. If on Mac OS X just brew install jq

curl -H "Authorization: Token {YOUR_TOKEN}" -H "Content-Type: application/json" -XGET https://{YOUR_JUPYTER_DOMAIN}/user/{USERNAME}/api/contents/{PATH_TO_IPYNB} | jq ".content"

Also you can add output redirection after the jq part > my_downloaded.ipynb

And then just open it with jupyter notebook

For /user make sure your personal server is started, for this you have to login to your user account or go into your Control panel, if it is not on or has been culled. For /services the server is usually being proxied so it should always be on.

The Url to use for /services

https://{YOUR_JUPYTER_DOMAIN}/services/{SERVICENAME}/api/contents/{PATH_TO_IPYNB}

I've also found simple solution. But it is applicable only if the notebook isn't secured by a token. The notebooks can be downloaded by wget.

Assume that our notebook is located by the following URL:

http://<DOMAIN>:<PORT>/notebooks/my_notebook.ipynb 

We can simply replace "notebooks" to "files" and download the source ipynb-file by the following command:

wget http://<DOMAIN>:<PORT>/files/my_notebook.ipynb 
Related