Editor’s note: We found an interesting series of articlesLearn 30 New Technologies in 30 Days, ready for translation, one update a day, year-end gift package. Here’s day three.


I was recently approached by PacktPub to review their new book about Flask. Flask is a popular Python framework. The book is Flask Real-Time Web Development by Ron Duplain. I decided to post the third part of my “Learn 30 New Skills in 30 Days” challenge on Flask. In this article, I’ll first cover some of the basics of the Flask framework and then provide a short book review. Similarly, I will port the sample application to OpenShift. I’m not a complete novice to Flask, and I’ve also used it to create sample applications. It was a good refresher for me.

For those of you unfamiliar with this series of articles, I’m trying the “Learn 30 New Technologies in 30 Days” challenge. Each day I learn a new technology and then write an article describing what I learned and how I used the new tool. If there’s not enough technology in a day, I’ll talk about a subtopic about that technology every day.

What is a Flask?

Some developers may not have heard of Flask, but let me give you a brief introduction. Flask is a small Python-based web development framework. Although Flask is a microframework, we don’t need to put all of our code into a single file, as other microframeworks suggest. After all, what microframes really mean is simplicity and smallness. We can start with a single-file architecture, using only the features we need. Once you’re comfortable with Flask, you can extract the code into modules and learn about the advanced features that Flask provides. Flask does not give advice on file schemas, but I will discuss the recommended file schemas in the book later in this article.

Let’s look at a sample code:

from flask import Flask
app = Flask(__name__)

@app.route('/', defaults={'name':"Guest"})
@app.route('/<string:name>' , methods=['GET'])
def say_hello(name):
    return "Hello " + name

if __name__ == "__main__":
    app.run(debug=True)

This example is simple, but eloquent. Sit back and enjoy it.

Let’s explain the above code:

  1. The first line imports the Flask class to create an instance of the Flask application.

  2. In the next line we create an instance of the Flask class. This is an example WSGI application. WSGI stands for “Web Service Gateway Interface” and is a Python standard for building Web projects. This line tells Flask where to find the static resources and templates needed by the application. In our example, we pass the name to tell the Flask to locate the resource within the current module.

  3. Then we define some routes for /. The first route is for the root path /, and the second corresponds to paths like /shekhar, / ABC, etc. For the/route, we set the initial name to Guest. If the user visits http://localhost:5000/, he sees Hello Guest. If user access to http://localhost:5000/shekhar, then he will see the Hello shekhar.

  4. Finally, we use the Python app.py command to start the development server to run the application. Let’s copy the above code into the app.py file. We also enable debugging with Debug=True, so that the browser can provide an interactive debugger when something unexpected happens. Another benefit of enabling debugging is that the service will automatically reload after the file has been changed. We can leave the debugger running in the background and continue to work on our application. This provides an efficient development environment. You can ask Java developers how to implement automatic reload 🙂

A few things to know about Flask:

  1. Flask was founded in 2010 by Armin Ronacher.

  2. Flask was inspired by Sinatra. (Sinatra is a Ruby framework for creating web applications that tries to avoid making a fuss.)

  3. Flask relies on two libraries, Werkzeug (Python’s WSGI tool library) and Jinja2 (a template engine).

  4. Flask follows the principle of “convention over configuration” and reasonable defaults.

Why should I care about Flask?

I decided to study Flask because:

  1. Easy to learn: I have 8 years of Java development experience. So after learning the basics of Python, I wanted to get into web development. Flask is very easy to use and, as my sample code above shows, is a dead-simple framework. My brain naturally adapted to it, and with Flask, I was able to develop it much faster.

  2. Very active and vibrant community: I talked to a few of my Python friends, and everyone suggested that I start with Flask. I Googled it too, and I found that Flask is everywhere said to be the best Python framework for modern web development.

  3. Create REST APIs quickly: I want to learn a framework that makes it easy to create REST services. The example shown above could easily be changed to an application that returns a JSON document by using Flask’s JSONify function. Future Web applications will be architected on the REST back end and on the front end based on a modern JawaScript MV* framework.

  4. 280 pages of excellent documentation with lots of examples.

  5. Flask follows “convention over configuration.” There are also reasonable defaults, such as static resources placed in static folders. Of course, most defaults are overridden.

About Flask Real-Time Web Development

Now let’s look at the details of the book.

  1. This book belongs to the recipe category, and each section creates a sample application. The book is only 78 pages long.

  2. For $11.

  3. The author submitted patches for Flask.

What did I like about the book?

  1. Starting with VirtualEnv is the right path for Python development. Avoid contaminating the main Python environment. Easy to use different versions of Python for different projects.

  2. Use best practices, such as a good directory structure. Although Flask is a microframework, we should still have a good hierarchy for our application. This is very helpful when developing enterprise applications.

  3. Introduces some Flask features and extensions I haven’t heard of. For example, the Flask-Script extension was introduced. Flask-Script supports writing external scripts for Flask applications.

  4. WTMForms is explained in detail. WTForms helps you write form-based applications.

Which could be better?

  1. For beginners, some topics are not clear enough and the relevant knowledge is not covered enough. I just don’t see why I would want to use subdomains in a Flask application.

  2. Lack of real database content such as PostgreSQL and MySQL. Although the author mentions using different database URIs to connect PostgreSQL and MySQL, there is no real example. I prefer to use the example of a real database rather than having the database in memory. Of course, it’s easier for beginners to keep the database in memory.

  3. No mention of REST services: REST is essential for today’s applications. One of the advantages of using Flask is that it is easy to write REST services.

  4. There is no mention of how to use a NoSQL database like MongoDB with Flask.

  5. The Flask framework’s unit testing support is fairly complete, but the authors don’t cover it.

  6. Cloud deployment is not mentioned. In this article, I’ll show you how to deploy the sample program in the book to OpenShift.

Should I buy the book?

Flask beginners who want to write traditional RDBMS-based Web applications will find this book useful.

Start Flask

To start using Flask we need to install Python and VirtualEnv. The Python version used in this article is 2.7.

Install the flask:

$mkdir schedulingapp $CD schedulingapp/ $virtualenv venv --python= Python2.7 $. Venv /bin/activate $PIP install flask

In the above command, we first create a directory for the sample application and then activate VirtualEnv. VirtualEnv helps avoid polluting the Python main environment. This means that different projects can use different versions of Python. Finally, we installed Flask. Pip Install Flask will install the latest stable version of the Flask framework.

The deployment sample is applied to OpenShift

The sample application in the book is a simple reservation application. Once you have registered your account, you can log into the app to create, modify, and view reservations. To run the application locally, use the following command:

$ git clone https://github.com/shekhargulati/instant-flask-web-development-book-app.git scheduleapp $ cd scheduleapp $ VirtualEnv venv --python=python2.7 $.venv /bin/activate $PIP install -r requires.txt $python manage.py create_tables $ python manage.py runserver

The application can then be accessed at http://127.0.0.1:5000. Users can register an account and start using the application.

Very good. But I’m going to deploy my app to the cloud. We will deploy the application on OpenShift. OpenShift is an open source, open, scalable PaaS (Platform as a Service).

Rely on

Before we can build the application, we need to make some Settings:

  1. Sign up for an OpenShift account. Registration is completely free, and Red Hat gives each user three free Gear sets that you can use to run your app. At the time of this writing, each user gets a total of 1.5 GB of RAM and 3 GB of hard disk space for free.

  2. Install the RHC client tool. RHC is a Ruby gem, so you need Ruby 1.8.7 or higher on your machine. Simply type sudo gem install RHC to install RHC. If you’ve already installed it, make sure it’s the latest version. Run sudo gem update RHC to upgrade. Detailed information on configuring RHC command-line tool, please refer to: https://openshift.redhat.com/community/developers/rhc-client-tools-install

  3. Use RHC’s setup command to configure your OpenShift account. This command will help you create a namespace and upload your SSH public key to the OpenShift server.

The deployment of application

To deploy the application to OpenShift, enter the following command:

RHC create - app schedapp python 2.7 postgresql 9.2 - from-code=https://github.com/shekhargulati/schedapp-openshift.git

This command will create the application, set up the public DNS, create a private Git repository, and deploy the application using the code in your GitHub repository. The application can be accessed through http://schedapp-shekhargulati.rhcloud.com/.

That’s all for today. Feedback is welcome.


Day 3: Flask–Instant Python Web Development with Python and OpenShift