This notebook is an interactive demo/tutorial of the features included in PR#3619 of the ipython/ipython project on Github. It is also described in IPython's IPEP 16.
import httplib
import json
port = 8888
conn = httplib.HTTPConnection('localhost', port)
The notebook web service (api/notebooks
) handles all requests to '.ipynb' files. The following HTTP requests are available:
GET
POST
PATCH
PUT
DELETE
The cell below makes a GET
request to the server asking for the json model of the notebook named "Untitled0" in "foo" directory.
URL = '/api/notebooks/foo/Untitled0.ipynb'
http_request = 'GET'
conn.request(http_request, URL)
r = conn.getresponse()
body = r.read()
body = json.loads(body)
print json.dumps(body, sort_keys=True, indent=4)
{ "content": { "metadata": { "name": "Untitled0" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": "2+2", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": "4" } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": "", "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }, "last_modified": "Tue Jul 23 17:46:27 2013", "notebook_name": "Untitled0.ipynb", "notebook_path": "foo/" }
The cell below sends a GET
HTTP request from the client to the server and prints a list of notebook models in 'foo' directory
URL = '/api/notebooks'
http_request = 'GET'
conn.request(http_request, URL)
r = conn.getresponse()
body = r.read()
body = json.loads(body)
print json.dumps(body, sort_keys=True, indent=4)
[ { "last_modified": "Tue Jul 23 16:12:46 2013", "notebook_name": "Multi-directory IPython notebook.ipynb", "notebook_path": "" }, { "last_modified": "Wed Jul 24 00:43:59 2013", "notebook_name": "Web service design.ipynb", "notebook_path": "" } ]
This call returns a list of dicts describing each session.
URL = '/api/sessions'
http_request = 'GET'
conn.request(http_request, URL)
r = conn.getresponse()
body = r.read()
body = json.loads(body)
print json.dumps(body, sort_keys=True, indent=4)
[ { "kernel": { "kernel_id": "04bb597b-3080-4349-83ba-33dc5b5f584e", "ws_url": "" }, "notebook_name": "Web service design.ipynb", "notebook_path": "", "session_id": "047f0ad0-a36e-4a13-be6b-f073f04d1b1b" } ]
URL = '/api/kernels'
http_request = 'GET'
conn.request(http_request, URL)
r = conn.getresponse()
body = r.read()
body = json.loads(body)
print json.dumps(body, sort_keys=True, indent=4)
[ { "kernel_id": "04bb597b-3080-4349-83ba-33dc5b5f584e", "ws_url": "" } ]
Lists all sub-directories found in the root directory and other files (i.e. '.jpg' in this example)
URL = '/api/contents'
http_request = 'GET'
conn.request(http_request, URL)
r = conn.getresponse()
body = r.read()
body = json.loads(body)
print json.dumps(body, sort_keys=True, indent=4)
[ { "MIME-type": "", "last_modified": "Wed Jul 24 01:48:54 2013", "name": "foo", "path": "", "size": 204, "type": "dir" }, { "MIME-type": "", "last_modified": "Wed Jul 24 01:50:05 2013", "name": "ipy_0.13.png", "path": "", "size": 166682, "type": "file" }, { "MIME-type": "", "last_modified": "Wed Jul 24 01:50:36 2013", "name": "qtlogo.png", "path": "", "size": 83247, "type": "file" }, { "MIME-type": "", "last_modified": "Tue Jul 23 16:12:46 2013", "name": "Multi-directory IPython notebook.ipynb", "path": "", "size": 5253, "type": "notebook" }, { "MIME-type": "", "last_modified": "Wed Jul 24 00:43:59 2013", "name": "Web service design.ipynb", "path": "", "size": 16853, "type": "notebook" } ]
Remember, all these have multidirectory support...
URL = '/api/contents/foo'
http_request = 'GET'
conn.request(http_request, URL)
r = conn.getresponse()
body = r.read()
body = json.loads(body)
print json.dumps(body, sort_keys=True, indent=4)
[ { "MIME-type": "", "last_modified": "Wed Jul 24 01:49:04 2013", "name": "bar", "path": "foo", "size": 204, "type": "dir" }, { "MIME-type": "", "last_modified": "Tue Jul 23 17:46:27 2013", "name": "Untitled0.ipynb", "path": "foo", "size": 620, "type": "notebook" }, { "MIME-type": "", "last_modified": "Tue Jul 23 17:46:34 2013", "name": "Untitled1.ipynb", "path": "foo", "size": 91, "type": "notebook" } ]