Building your First Flask application in 10 minutes or less

In this recipe, you'll build your first Flask application in 10 minutes or less. Flask is a Python microframework that you can use to create simple web-based applications very quickly. Visit http://flask.pocoo.org/ for all the details.

In this recipe, we're going to keep it simple; however, included with the book is a fully functional Flask application chock-full of the latest JavaScript libraries and CSS themes, ready for customization.

Getting Set Up…

In order to write Flask applications, you first need to install it. If you have the Anaconda distribution of Python, you'll already have it. Just in case you don't, open a terminal and run the following:

pip install flask

To be able to pull data directly from MongoDB later on, install the flask-mongoengine library with this command:

pip install flask-mongoengine

Now let's build that first application.

How to do it…

  1. First, create a new directory for your application.
  2. Next, open a terminal session, and change into the directory you just created.
  3. After that, create a folder named templates.
  4. Next, in the root directory of your application, create an empty index.py file.
  5. Now, change into the templates folder, and create a file named index.html.
  6. You should have a directory structure that looks like this:
    first-flask-app
        - index.py
        - templates
            - index.html
    
  7. That's the minimum for a Flask application. Now, open index.py, and add the following code:
    from flask import Flask, render_template
    app = Flask(__name__)
    @app.route('/')
    def index():
        message = 'Welcome to Your First Flask Application!'
        return render_template('index.html',
                               message=message)
    if __name__ == "__main__":
        app.debug = True  # Comment this out when going into production
        app.run()
    
  8. Next, open the index.html file in the templates folder and add this code:
    <h1>{{ message }}</h1>
  9. To run this application, go to your terminal, and run this command in the root directory:
    python index.py
    

This will start Flask's internal web server and run it on port 5000. Open up a web browser and navigate to http://127.0.0.1:5000.

How it works…

The first thing that we do is to import Flask and the render_template methods from the Flask library. This allows us to create a Flask application as well as render templates. Templates are what will have data inserted into them and be shown to the users when they visit the application:

from flask import Flask, render_template

Next we create a new Flask application object:

app = Flask(__name__)

In order for Flask to serve a web page, it needs to know what to do when a user visits a URL. These two lines define a route to the home page as well as an index function that would run when that URL is visited. In this case, we define a message variable, assign it a value, then tell Flask to render the index.html template, and assign the value of message to the message variable in the template:

@app.route('/')
def index():
    message = 'Welcome to Your First Flask Application!'
    return render_template('index.html',
                           message=message)

In order to run the file at the command line as we did, we need to tell it what to run. In this instance, we are telling Python to run the Flask application that we previously defined, and turn on debugging before doing so:

if __name__ == "__main__":
    app.debug = True  # Comment this out when going into production
    app.run()

When you run the file using the Python index.py command, you will see something similar to the following screenshot :

How it works…

Congratulations! You have created your first Flask application.

See Also..

For much more on using Flask, I highly suggest the following video and book from Packt Publishing:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset