Skip to content

Instantly share code, notes, and snippets.

@mhsattarian
Forked from psychemedia/SimpleAPI.ipynb
Last active November 3, 2019 22:29
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mhsattarian/ca0cf359343d7b3894175660ac07be36 to your computer and use it in GitHub Desktop.
Using Jupyter Notebooks as Rest API Endpoint
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-21T21:56:00.695520Z",
"start_time": "2019-10-21T21:56:00.687698Z"
}
},
"source": [
"# Using Jupyter Notebooks as a Rest API Endpoint"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook we make a simple API that converts degrees to radians. We should end up with something like this:\n",
"\n",
"```shell\n",
"$ curl \"http://serverIp:8888/convert?angle=180\"\n",
"# {\"convertedAngle\": 3.141592653589793}\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, install `jupyter_kernel_gateway` package (uncomment line below to use):"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-21T21:48:10.422811Z",
"start_time": "2019-10-21T21:48:10.417083Z"
}
},
"outputs": [],
"source": [
"# !pip3 install jupyter_kernel_gateway"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-21T21:36:56.125476Z",
"start_time": "2019-10-21T21:36:56.109993Z"
}
},
"outputs": [],
"source": [
"import math\n",
"import json\n",
"\n",
"# REQUEST is the http request sent to the endpoints,\n",
"# it's initialized to prevent error in running the notebook\n",
"REQUEST = json.dumps({\n",
" 'path' : {},\n",
" 'args' : {}\n",
"})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Defining the endpoint (this cell response to `GET` requests on `/convert`):"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-21T21:42:59.480365Z",
"start_time": "2019-10-21T21:42:59.469965Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"convertedAngle\": null}\n"
]
}
],
"source": [
"# GET /convert\n",
"req = json.loads(REQUEST)\n",
"args = req['args']\n",
"\n",
"if 'angle' not in args:\n",
" print(json.dumps({'convertedAngle': None}))\n",
"else:\n",
" # Note the [0] when retrieving the argument.\n",
" # This is because you could potentially pass multiple angles.\n",
" angle = int(args['angle'][0])\n",
" converted = math.radians(angle)\n",
" print(json.dumps({'convertedAngle': converted}))"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-21T21:41:09.396589Z",
"start_time": "2019-10-21T21:41:09.278104Z"
}
},
"source": [
"#### In terminal run:\n",
"\n",
"```shell\n",
"jupyter kernelgateway --KernelGatewayApp.api='kernel_gateway.notebook_http' --KernelGatewayApp.seed_uri='./JNB_endpoint.ipynb'\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**sources**: [Eliot Andres blog](https://ndres.me/post/jupyter-notebook-rest-api/) and [ouseful blog](https://blog.ouseful.info/2017/09/06/building-a-json-api-using-jupyer-notebooks-in-under-5-minutes/)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment