hello worlds

Installing Flask

Install flask with the following command:

  sudo pip install Flask


The pip utility may not be installed on your system by default. If you're on a Mac execute:

  sudo easy_install pip

And if you're on an Ubuntu machine:

  sudo apt-get install python-pip


There are other ways to install Flask, for instance on Ubuntu simply open a terminal and enter:

  sudo apt-get install python-flask

For more information see the Flask documentation.

server.py

Now create the file server.py and add the following:

  #!/usr/bin/python
#
# Flask server, woo!
#

from flask import Flask, request, redirect, url_for, send_from_directory

# Setup Flask app.
app = Flask(__name__)
app.debug = True


# Routes
@app.route('/')
def root():
  return app.send_static_file('index.html')

@app.route('/<path:path>')
def static_proxy(path):
  # send_static_file will guess the correct MIME type
  return app.send_static_file(path)


if __name__ == '__main__':
  app.run()

You can run the server.py file by using the python command, or make the file executable and use the more fun ./ method:

  chmod 755 server.py
./server.py

If all goes well with the server you should see output similar to:

   * Running on http://127.0.0.1:5000/
 * Restarting with reloader

You can how enter http://localhost:5000 in your browser and access your new server.

index.html

If you go to the new server site at this point you should receive a HTTP 404 "File not found." error. This is because in the server we configured the default "/" route to serve index.html which we haven't created yet.

Create a static/index.html file and add the following for testing purposes:

  <!DOCTYPE html>
<html>
  <head>
    <title>Testing</title>
  </head>
  <body>
    <h1>Hello, Worlds...</h1>
  </body>
</html>

Then return to your browser http://localhost:5000 and refresh the page.

You should see a nice welcoming "Hello, Worlds..." message.