Running a simple application (Simple)

Let's get started by running a simplest Flask application.

Getting ready

Make sure you have activated your virtualenv tool as described in the previous section.

How to do it...

  1. Put this code into a file named hello.py:
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello():
        return 'Hello, world!'
    
    if __name__ == '__main__':
        app.run()
  2. Run this application from the console:
    $ python hello.py
  3. If this runs correctly, you will see:

    * Running on http://127.0.0.1:5000/

    This is a URL, which you can visit in your browser. When you do, your browser will display a plain page which says "Hello, world!" (without quotes). Note that the address 127.0.0.1 refers to localhost, such that Flask will only respond to ...

Get Instant Flask Web Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.