Book Review - Instant Flask Web Development - Packt Publish 2013
  • book review
  • packt publishing
  • Flask
  • Python
  • web development
  • This is my review of Ron DuPlain's Instant Flask Web Development published by Packt Publishing.

    The book can be found here for purchase.

    I've been a fan of Python frameworks for a few years and have played around with or otherwise developed sites with Django, web2py, and Flask. When doing web development, simplicity is one of the most important concepts to me. Let me explain further, since many think that geeks naturally gravitate to reading a lot of arcane code and obscuring everything with overcomplicated code and concepts.

    I am not that guy. I like the KISS (keep it simple, stupid) methodology and I go out of my way - sometimes spending more time than I'd like - finding ways to work efficiently and quickly and leave things better than I found them.

    Thus, I gravitated to Flask quite naturally after taking down my Django-mezzanine site and wanting something simpler that I could grow from seed.

    Jinja2 and me - well - we get along like strange cats, but once I understood how to incorporate blocks with CSS, I let go of some of the anger. I initially learned Jinja2 templating with Django and then sort of let go for a while. Now I'm stronger than ever with it.

    Flask uses Jinja2 natively, but other templating engines could be used.

    For this very site, I'm using Flask, Flask-flatpages and the defaults.

    On to the book:

    The first thing that struck me as interesting and nice to see was in the "About the Author" section: "Ron DuPlain is a core contributor to the Flask project, and has built dozens of production applications with Flask. He has been thinking in Python since 2005, applying it to a variety of applications from embedded systems to web development"

    It is very important to me that Ron is a core contributor. The flask docs are nice but it's also nice to have someone on the "inside", so to speak, to expound on concepts and give longer examples.

    The preface starts out very well and explains the origins of flask - I actually did not know that the project started as an April fools joke - and moves on to describe the involvement of Werkzeug and Jinja. I particularly like the part where the author explains what really sets Flask apart - which to me was how he explains that there is no "Flask way", rather, that Flask is very "pythonic" and that a user should understand how to use python to achieve results in Flask rather than understanding an ORM (django) or other concept that takes the user away from the framework and into other potentially distracting ways and systems.

    A standard "what this book covers" section follows that lays out the basic things a reader of technical "how to" books might expect. I like how the topics are grouped as:

    1. simple

    2. intermediate

    3. advanced

    This makes it much easier to see where you are in the book and skip around as needed. The format of the book is to start at "Hello World!" and move all the way to deployment. This is useful in today's "deploy or die" web development world.

    The What you need for this book section is fairly boilerplate: python should be installed, and if you're not an expert python coder, having the willingness to learn python as well as learn and understand basic HTTP, HTML, CSS and Javascript is enough to start. It is said clearly that this could be a learning curve for python neophytes and that it is good to have resources handy. Luckily, anyone who wants to learn can go to and find everything that is needed to learn and progress. As you'll read later on in this review, I sort of disagree that a neophyte would be at all comfortable with this book, but it is possible for a very motivated and intelligent and patient person - just as learning any large task would require those attributes for success.

    Virtualenv and pypi are discussed briefly, and then the author conveys that using Python3 while learning would be a better idea than using python2. I disagree fundamentally with this because I prefer python2 and still code with it, but new coders may wish to use python3 for their projects and I say more power to them if they choose that path.

    The who is this book for section is again - fairly boilerplate - but it is nice to see that the book covers beginner to advanced and that it is written for all of these audiences. I suppose sections like these are designed for the casual bookstore browser who is deciding whether to buy the book - it seems to me that it is hard to find a brick and mortar bookstore these days, and so this standard might be unnecessary. That's not a knock on this book - but instead a knock on all technical books that use this framework of introductions. Also, this is specifically designed as an ebook, so I'm wondering where and how a potential reader might be browsing this section if he had not already purchased the book. Perhaps this part of the introduction is redundant based on the media format of the book.

    Let's jump right into chapter 1: Instant Flask Web Development. Briefly described are ways to obtain a copy of Python and then how to install virtualenv. This is key to development with Python web technologies and I can't emphasize enough how important it is to use virtualenvs rather than install packages system-wide.

    I could go into the various reasons for this, but consider a workshop as a metaphor - you wouldn't mix paint and clean car parts in the living room, now would you?

    Brief yet consise instructions are given to help the user in both Linux (yay!) and Windows install virtualenv. (reviewers note: I would recommend using Linux if you are already familiar with it and have at least a virtual machine installed to use if not a full system. Windows users - I would still recommend installing Virtualbox and making a virtual machine to work with Python and web frameworks. You'll thank me later - I promise!)

    Brief instructions are given to run virtualenvs including activate/deactivate and so on.(editors note: It would be a very good idea for a reader who is not familiar with virtualenvs to spend some good quality time learning how to use them. Not doing this could set the reader up for severe technical obstacles - tread lightly and research fully!)

    A checklist for development environment follows - this is fairly important - don't skip it or you may find yourself lacking what you need to do the coding and testing.

    IDEs are discussed and as well - command line is discussed. I recommend using a free IDE of your choice along with at least 2 console windows - 1 for an interpretor and 1 to run the test webserver. Some IDEs have terminals and other features built in - stick with what is simple and works and does not get in your way.

    Some common errors are discussed, and then we dive into a sample program which is the same sample program available in the Flask documentation and tutorial. This is fine - we have to start somewhere. Then, the author shows how to run the script and where to find the test server and on which port. This will be either localhost:5000 or depending on your system and how you have networking set up there.

    A line-by-line breakdown is then described for the sample application. I find this to be a nice addition so early on as it should help the reader to understand the basics before getting in too deep.

    Project layout is discussed, and I see rearing its not so attractive head. I'd prefer to stay away from Django conventions, but and are all here. We have the freedom to be different, yet, the author is sticking to what I am assuming is comfortable for him (please excuse me if I am wrong). While this structure is fine, and probably also good for future Django programmers, I would rather see a derivation from the obvious conventions here. I can understand why the author went this direction and don't fault him for it.

    Development/debug is discussed - if you are deploying to the world - make sure you understand this well. A site in the wild in debug mode is sad to see and is ripe for skiddies to pillage and ruin. It is so much easier to do all of your testing locally and then turn off debug mode to deploy than to edit on a live site, but people do it wrong seemingly all of the time. Tread lightly and make sure to understand debug vs. deployment fully! :)

    Routing is then discussed, and by page 15 we are in fairly deep.If you don't understand Python by now, this will look like a foreign language. If you don't understand HTTP at all, this will be a rude awakening. The reader is instructed to paste a bunch of code into at this point.

    I'm not a fan of cutting and pasting code from books - even ebooks where it is easier to do so - mostly due to the fact that accidents happen, books have errors, and cutting and pasting assumes a high level of understanding of the code as well as a high level of trust with the author.

    I'd rather see this done brick-by-brick, but I also understand that this is a "fast-track" style of ebook that is under 70 pages in total.

    On pages 15 - 17, all about routing and HTTP is explained. All in 2 pages. Make sure you understand this well and do make sure to research topics and refer back and research some more unless this is an area that you are already well-versed in. I remember understanding the basics of Django but quickly becoming lost in regexp and routing - patience will pay big dividends here - so make sure not to rush through the book even though it is not very lengthy.

    Decorators and alternatives are talked about. Without decorators, the code will be harder to maintain, so I recommend sticking with those as a convention.

    Pages 20-22 go more into detail on HTTP requests and errors. Go slowly here and reread as necessary - this is a very quick going over of this material and it needs to be fully understood.

    Static files are broached on page 23. This is a nice feature, and there's a package called flask-flatpages in pypi if you want to deploy a site without a database. It may seem silly to use static pages, but that's for people who haven't torched a database and spent hours digging out data and rebuilding. Static is dead easy and has great benefit for pages that don't change or don't change often.

    Finally by page 25 we talk about databases. SQLAlchemy is discussed, and I like this a lot. This is a feature that any pythonista should try in any framework if it is available.

    If I haven't stressed the importance of virtualenv yet, the author continues to. Make sure to understand 'requirements.txt' and how that is very important to making a deployment go quicker and easier. Requirements.txt comes up again later - this is a PIP convention and is easy to overlook but even easier to learn.

    Pages 25 through 30 go through setting up queries and such with SQLAlchemy and I think that this is a significant percentage of pages to use for such a short book. If you are going to use a database with tables, consider rereading this section as much as necessary and not using standard SQL.

    Page 31 discusses considerations for database engines. I like SQLite because it is small and portable and I can browse a database with a simple, readily available piece of software. I don't need a huge package of binaries and a data dump to fix a problem or find a piece of data with SQLite.

    For larger sites with a lot of data and user logins, however, PostgreSQL and MySQL are probably a better bet, and I agree with the author here.

    To simply learn the framework and deploy a simple site, however, I'd suggest not tying yourself down with yet another methodology to learn and stick with a small, portable database like SQLite.

    Forms, uploads, etc., are discussed through page 35. If don't understand the python os() library, now is a good time to learn. If you don't understand much SQL, yet, now is a good time to recap and even reread previous sections before going forward.

    Page 36 introduces Jinja2 and templating and this will be a soulbreaker for you if you don't fully understand templating. I have spent literally hours debugging and learning Jinja2, and no matter how many times I see the developers of this or that framework talk about how easy it is to use, I think back on the time I've spent getting more complex ideas to flesh themselves out there. don't skip this section - you will regret it!

    Jquery and Javascript start on page 37. I suppose this is a necessary evil for a book that has so few pages, but I have sofar managed to get away with very little javascript and feel that I'm the better for it.

    Page 38 has a very brief explanation of jinja2 and this is revisited again on page 43 with intermingled javascript.

    I understand that for the format of this book - it needs to happen this way - but I also think it is possible to make a basic site without javascript that is completely adequate. Take Yours Truly's site as an example. As I noted earlier, Jinja2 can get fairly complicated, and I'm not sure that combining another language inside the templates while juggling Python coding for forms and apps would be a good primer for a beginner. Having seen these pages, I'd say that this book is definitely catering to the intermediate to advanced reader/programmer.

    The app does look nice when running, but I will take understanding over aesethetics when learning something new and fairly complex. Your mileage may vary, so please take that with a proverbial grain of salt when going through the book.

    Ajax is briefly discussed - and as the author says - it could be a book of its own. Many developers swear by Ajax, so it is worth looking further into.

    Deploying to the world begins discussion at the bottom of page 62. Tools and readiness are discussed, but I'll say what the author won't: use Linux. If you don't know Linux, learn it. Few, if any hosts are going to deploy Python-Flask sites on windows servers, and you should not do this either. Learning environment and paths and other necessary concepts on windows, if you're not already familiar, are going to start yet another learning curve for the coder, and create unnecessary frustration. Just use Linux.

    In fact, the author provides no instructions whatever for deploying to IIS, and instead points to the NWSGI project. I'll say it again - nobody deploys to Windows. He doesn't doesn't deploy to windows, I don't deploy to windows, and neither should you.

    Pages 63 and 64 discuss nginx server. I love nginx and use it instead of apache for many reasons: it is portable, easy to install, easy to understand, and not bridled with mod-this and mod-that and a half-ton of configuration files. If you've ever setup a php dev environment with xampp, you have a pretty good idea of what I am speaking. Nginx is beautiful and dead easy to learn which might be why only 2 pages are dedicated to it.

    Gunicorn is also discussed, but so briefly that it is almost an afterthought. Consider using this for your deployed environment: Debian (ubuntu is heavy with extras that aren't necessary and is based on Debian), Gunicorn, Nginx, and Supervisor. Supervisor is nice because it allows you to script the automation of processes. A simple script will make sure that gunicorn runs and fires up your Flask site on reboot, for instance.

    In summary

    I suppose that in light of this being a relatively short book, I can understand why some topics are touched on less than others. As this is an ebook, it is easy to cut and paste code as well as copy and paste keywords and urls to a browser to do research and so the actual length of the book can be abbreviated. I do have my reservations about copying/pasting code rather than writing it from concepts, fundamentals, and practice, but it seems that since more programmers coming into the market seem to ply their trade by asking for help with code in social media pages that perhaps this is a precursor to a more crowdsourced form of code development. Whether it is inevitable or just a fad, I'm still not thrilled by the prospect of coders becoming lazier and depending too much on others for their work product.

    Perhaps the newer generation of social-media coders who have been raised on search engine research will have a relatively easy time finding the extra knowledge to fill in the gaps, and perhaps not. Learning a new anything takes time and dedication and a bit of moxy sometimes to get us through the rough patches.

    I think that Ron Duplain does an admirable job of presenting a complete project that could be modified and presented to the world. My only caveat is that this book is fast-paced, and therefore I wouldn't recommend it to beginners of the Python language or those who are not experienced - at least in a minimal way - in using a server console. Some will disagree with this stance, but I have seen very experienced programmers quickly broken by the rudiments of attempting to install python on Windows, failing to get paths correct, having difficulty learning how to run scripts, and so on.
    GNU/Linux is installed with python2 and sometimes python3 (depending on the distribution) and learning the command line is the same there as it is anywhere - there's going to be a learning curve, so pick your poison.

    Even considering all of these caveats that I have, I wholeheartedly recommend the book as there simply isn't too much out there yet - barring the reading of the official documentation - that can get a person moving forward and successful with Flask.

    Useful and easy to implement open-source projects are few and far between and in my opinion, Flask is still a young framework with plenty of room to grow. That the author took the time and energy to make this book is a boon to any web developer interested in using a Python framework without learning a more complicated ORM (e.g. Django) This book is worth every penny and probably more, but it is an ebook and therefore less expensive as ebooks should be.

    Full code is given by the author that the reader can use to deploy a project and then hack it up to her pleasure. This is nice and goes a bit further than the docs are able to and so I would recommend the book if only to take it up a notch for the potential Flask user in a way that she might not experience from reading the docs and perusing the scant amount of tutorials available in the wild.

    I intend to further study this book to enhance my own use of SQLAlchemy as well as URL routing so as to nip in the bud some errors I encounter within the framework that can't be completely straightened out by nginx. I hope that you enjoy the book.

    Friday, October 25, 2013
    Adam Rohacs

    Return to main