Skip to content

Instantly share code, notes, and snippets.

@pbui
Last active November 6, 2015 16:58
Show Gist options
  • Save pbui/6ab03e27e97c30183bc4 to your computer and use it in GitHub Desktop.
Save pbui/6ab03e27e97c30183bc4 to your computer and use it in GitHub Desktop.
Simple example of using Tornado to build a "Hello, World" web application in a Jupyter Notebook.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Imports\n",
"\n",
"To utilize [Tornado], we need to import or load certain packages from the framework.\n",
"\n",
"[Tornado]: http://www.tornadoweb.org/en/stable/"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import tornado.ioloop\n",
"import tornado.web"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Constants\n",
"\n",
"Our web application needs to listen on a **port**, which is a number associated with our program that acts as our \"mailbox\" number. That is if another machine wishes to communicate with our application, then the other machine will send a message to our machine's **address** and with a **port** number associated with our application.\n",
"\n",
"Normally, web servers use **port** `80`, which considered a priviledged **port**. Since we are running as a regular user (rather than an administrator), we should use a higher-number port such as `8080`, `8888`, or `9999`."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"PORT = 9999"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Handlers\n",
"\n",
"To define what happens when we get a request from the user, we define **handlers** that are associated with different URL **endpoints** or **resource paths**.\n",
"\n",
"**Endpoints** are the paths after the **hostname** and **port** in a **URL**. For instance, if the **URL** is `http://www.nd.edu:80/courses/`. then the **hostname** would be `www.nd.edu`, the **port** would be `80`, and the **endpoint** would be `/courses/`."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class HelloHandler(tornado.web.RequestHandler):\n",
" def get(self):\n",
" self.write(\"Hello, World\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Application\n",
"\n",
"Once we have our **handlers** defined, we need to associate them with specific URL **endpoints** and then we can **listen** on our configured **port**.\n",
"\n",
"Each **handler** is a **sub-class** of the `tornado.web.RequestHandler` and defines a `get` **method** that will be called whenever a **HTTP** **GET** request is made on the web application."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"Application = tornado.web.Application([\n",
" (r\"/\", HelloHandler),\n",
"])\n",
"\n",
"Application.listen(PORT)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Testing\n",
"\n",
"Once you have executed all the code above, you can test your web application by going to http://hostname:port (where **hostname** is the name of your machine and **port** is the configured port).\n",
"\n",
"For instance, if you are running the web application on your laptop and the **port** is `9999`, then you can access the application on the same machine via http://localhost:9999.\n",
"\n",
"If you need to make modifications and restart the application, then you will need to **interrupt** and **restart** the notebook kernel."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment