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