Packtpub recently contacted me to review their recently published book on Flask, which is a popular Python framework. The book is titled Instant Flask Web Development and is written by Ron DuPlain. On my third day of 30TechnologiesIn30Days challenge, I have decided to spend time on Flask. In this blog post, I will first talk about Flask framework basics and then will provide a small book review. Also, I will port the sample application to OpenShift. I am not entirely new to Flask and have built some sample applications using it. This will be a good refresher.
For people who are not following this blog series, I have taken a challenge to learn 30 technologies in 30 days. Every day I will learn a new technology and will write a blog post about what I learned and how I will be using the new tool. If a technology can not be covered in a single day, I will write about a subtopic within that technology each day.
What is Flask?
For fellow developers who are not aware of Flask, let me give a small introduction to it. Flask is a micro framework for doing web development in Python. By micro framework, it does not mean that we have to write all our code in a single file as recommended by other micro frameworks. Microframework actually refers to simplicity and the small size of the framework. We can start working with a simple one file structure using features we need. As we become familiar with Flask, we can modularize the code and learn advanced functionalities offered by Flask. Flask does not make any recommendations about any file structure but later in the blog I will discuss file structure recommended in the book.
To make it more clear, lets look at the sample code.
This is a very simple example but expressive. You can just sit and admire it. Let's decipher the code snippet shown above.
In the first line we imported Flask class so that we can create an instance of Flask application.
In the next line we created an instance of Flask class. This is WSGI application instance. WSGI stands for Web Service Gateway Interface and is the Python standard for hosting web projects. The argument tells Flask where to find application static assets and templates. In the example shown above we passed name which informs Flask to use the current module to location resources.
Next we defined a couple of routes for "/" url. The first route is for root url '/' and next corresponds to any url like /shekhar or /abc. For the '/' route, we set the default name as "Guest". If a user makes a request to http://localhost:5000/, then he will see "Hello Guest". If a user makes a request to http://localhost:5000/shekhar, then he will see "Hello shekhar".
Finally we start the development server to run the application using python app.py command. We should copy the code shown above in app.py file. We also enabled debugging by passing Debug=True. Debugging provides interactive debugger in the browser when unexpected exceptions occur. Another benefit of the debugger is that it will automatically reload the changes. We can keep the debugger running in the background and work through our application. This provide highly productive environment. Ask any Java developer about automatic reloading :).
Few things worth knowing about Flask :
Flask was created by Armin Ronacher in 2010.
Flask was inspired by Sinatra (A Ruby framework to create web apps with minimum fuss).
Flask depends on two libraries Werkzeug(WSGI utility library for Python) and Jinja2(a templating engine).
Flask follows conventions over configuration and has sensible defaults.
Why should I care?
I deiced to learn Flask because:
Easy to Learn : I have been a Java developer throughout my 8 years of software development. So after learning Python fundamentals I wanted to get into web development. Flask is a very easy to use and dead simple framework as I showed in my example above. It naturally fits in my mind, and I can develop faster.
Very active and vibrant community : I talked to few of my Python friends and everybody recommended me to start with Flask. I googled on the web and everywhere I found Flask to be the best modern Python web development framework.
Quick REST API creation : I wanted to learn a framework which makes it easy to write REST services. The example shown above can very easy return json documents using Flask jsonify function. The architecture of future web applications is REST backend with the Front end written in modern JavaScript MV* framework.
Excellent documentation spread over 280 pages with lots of examples
Flask follows conventions over configuration. It has good sensible defaults like a static folder for static assets. We can override most of the defaults.
Instant Flask Web Development Book Details
Now that we are on same page lets look at the book details.
Recipes based book where in each section you develop a sample application. The book is only 78 pages.
The book cost $11.
Book is written by Flask committer.
What I liked in book?
Started with virtualenv which is the correct way to work with python. It avoids polluting your main python installation. This allows us to work with different versions of python for different projects.
Used best practices like proper folder structure for the project. Although flask is a micro-framework, but we should define proper layout for application. This helps when developing enterprise applications.
The book talked about Flask functionalities and extensions that I was not aware off. The book talked about Flask-Script extension. Flask-Script provides support for writing external scripts for Flask applications.
The book covered WTForms in detail. WTForms helps us write form based applications.
What should have been better?
Some topics were not very clear and did not provide much context for beginner. I did not understand why one would use subdomains in Flask application.
Real database like Postgresql or Mysql were missing. Although author talked about using different database uri to connect to PostgreSQL and Mysql, but did not gave real example. I prefer when a real database is used instead of in-memory database. One advantage of using in-memory databases is that it can make things easy for beginners.
No talk about REST services : REST services are critical to today's application. One of the advantage of using Flask is that we can very easily write REST services using it.
No talk about how one can use NoSQL database like MongoDB with Flask.
Flask framework has very good unit testing support, but the author did not talk about it.
No talk about Cloud deployment. In this blog, I will show how we can deploy the sample application discussed in the book on OpenShift.
Should I buy this book?
This book is useful for Flask beginners who want to write traditional RDBMS based web applications.
Getting Started with Flask
Before we can get started with Flask we need to install Python and virtualenv on our machine. The Python version I am using in this blog is 2.7. To install flask do the following.
In the commands shown above, we first created a directory for the sample application and then activated virtualenv. The virtualenv helps avoid polluting main python installation on the machine. This allows us to work with different versions of python for different projects. Finally, we installed flask. The pip install flask will install the latest stable version of flask framework.
Deploying Sample Application to OpenShift
The sample application in the book is a simple appointment application. We can register for an account and then login to application to create, edit, and list appointments. To get the application running on the local machine, execute the commands mentioned below.
This will start the application at http://127.0.0.1:5000 and user can register an account and get started with application.
This is fine, but I want to host my application in the cloud. We will be deploying the application on OpenShift. OpenShift is an open source, public, scalable Platform as a Service.
Prerequisite
Before we can start building the application, we'll have to do few setup tasks :
Sign up for an OpenShift Account. It is completely free and Red Hat gives every user three free Gears on which to run your applications. At the time of this writing, the combined resources allocated for each user is 1.5 GB of memory and 3 GB of disk space.
Install the rhc client tool on your machine. The rhc is a ruby gem so you need to have ruby 1.8.7 or above on your machine. To install rhc, just type
If you already have one, make sure it is the latest one. To update your rhc, execute the command shown below.
For additional assistance setting up the rhc command-line tool, see the following page: https://openshift.redhat.com/community/developers/rhc-client-tools-install
Setup your OpenShift account using rhc setup command. This command will help you create a namespace and upload your ssh keys to OpenShift server.
Deploying Application
To deploy the application on OpenShift just type the command shown below.
It will do all the stuff from creating an application, to setting up public DNS, to creating private git repository, and then finally deploying the application using code from my Github repository. The app is running here http://schedapp-shekhargulati.rhcloud.com/
That's it for today. Keep giving feedback.
What's Next
Sign up for OpenShift Online
Get your own private Platform As a Service (PaaS) by evaluating OpenShift Enterprise
Need Help? Ask the OpenShift Community your questions in the forums
Showcase your awesome app in the OpenShift Developer Spotlight. Get in the OpenShift Application Gallery today.