The process of creating interactive web applications has changed dramatically over the past several years. The options for building a site are seemingly limitless, and choices can quickly become confusing when you start to study pros and cons of choosing a framework or stack. Hmm… Django vs. Ruby on Rails vs. Flask vs. Node.js vs. Spring vs. Yii vs. Batman.js vs. <insert trendy framework>. If you choose Batman strictly because of its name, that seems fair to me. At Josh.ai, our system needs to handle several distinct types of demands, and we find that using Python gives us great flexibility to satisfy sometimes conflicting demands.
1. Be Pythonic
To start, I like Python as a language. In some sense the language is a jack-of-all-trades — you can follow a reasonable object-oriented paradigm, and can even get a taste for some functional programming ideas. Here’s a good source to check out some interesting language features such as generators, map and filter operations, lambdas, and much more. Some features may seem esoteric, but on many occasions I’ll run into a scenario where I find exactly what I need for some interesting task. Further, and this is certainly more of a personal preference, I like the Python indenting style. It tends to result in relatively neat and concise code, which I’ve found useful in projects that get large. “Pythonic” is quite the buzzword, so feel free to open up a terminal, enter a python interpreter, and say “import this.”
2. You can have it all
If you’re looking for a batteries included solution to get a project off the ground, consider the framework Django. The project’s claim to fame is to “take care of much of the hassle of web development.” The idea is that tons of functionality is already built into an out-of-the-box Django app when you get it fired up, such as database solutions and HTML templating abilities (i.e. make pages generate content on the fly). Many proponents of Django also enjoy the built-in admin site, which is in itself a Django app that makes it easy to view, modify, and delete objects that you create in your database (via creating a mapping between your database models and custom classes to specify how you’d like to display objects on the dashboard). Beyond the official Django project itself, another big benefit is the plethora of 3rd party apps that you can plug into your own. Ideally, these external apps should fit right into your app without too big of a headache dealing with configuration. The project dates back 2003, so the community has been growing ever since, which often makes it simple to find an answer you’re looking for with a quick search. That said, Django is a relatively large piece of software compared to some other options, and in my opinion it takes some real time to get comfortable with all of the objects you get and how to use them effectively. If you find that you’re satisfied with some of the out-of-the-box solutions (including the external apps you decide to include), the project may very well be a great starting point.
3. You can have enough
In stark contrast to Django (at least in the world of Python web frameworks) is the popular Flask framework. The first thing you’ll learn about Flask is that it’s a “micro” framework — one of its primary design goals is to have a light-weight core that developers can choose to extend in many ways. Flask is my framework of choice for your typical site where users surf around pages with dynamic content. I found the experience of starting up a Flask app and building it out to be extremely pleasant and straightforward (even compared to Django). The canonical “hello world” Flask app is 7 lines of code from the user, which of course doesn’t do much, but points to the project’s effort at creating simplicity. In terms of extensibility, Flask leaves it up to the developer to choose the database methodology, templating languages, and other core components of a web app. With this, I feel I have a solid grasp of what’s going on from top to bottom in my apps. If you are a beginning programmer and would rather not have to make the decisions about which technologies to use to supplement your site, note that there are some widely used Flask extensions out in the community, which wind up creating relatively standard ways to tackle these choices. For example, I highly recommend this tutorial from Flask guru Miguel Grinberg. You’ll see that a full-fledged application can be put together in Flask with relatively little pain as compared to some competitors. The framework gives you plenty as you build from the ground up, without the baggage of components you don’t even know if you desire.
4. Leverage the world of Python
Back to the language itself, Python is well-known for having a package for just about anything. I believe the core set of Python packages are very well-documented too, which makes a big difference. Some great examples are the widely used Numpy and SciPy packages to help with scientific computing. You can get a lot of functionality to do statistical work and get extra Python packages, including cool Artificial Intelligence and Machine Learning packages, from the community (check out PyBrain for example). As you probably know, Python has a huge community and is one of the most widely used languages, meaning questions and answers are plentiful, often with nice, elegant solutions. I see Python as a generalist language which will be useful to have in your back pocket for a long time to come.
5. Easy deployment
At some point you’ll have an amazing web application that people need to see. Luckily, I’ve had a great experience deploying a Python application to the web. I’m a fan of Heroku for much of this task — Heroku takes care of lots of things for you, including building a virtual environment to install your Python dependencies, easy code changes by linking an app to a GitHub repository, creating a means to access your app on the web before dealing with domains and DNS if you wish, extremely useful error logging, and a nice way to add more computing power if you so decide. Even more, Heroku has a large library of add-ons that you can plug into your site, such as storage (e.g. PostgreSQL and Redis), website monitoring, email utilities, and much more. Of course beyond Heroku, there are many options to get your app up and running live, and you can choose based on the level of control you wish to maintain as well as the price point offered.
6. Great options for database work
Earlier I alluded to the flexibility I’ve experienced programming with the Flask framework. This point is especially true when it comes to database decisions. Python has a great suite of packages to deal with database connectivity, querying, and management. I have been using PostgreSQL for the bulk of my database work, and even here I’ve worked with multiple good options. Psycopg is a well-known PgSQL adapter for Python, and it provides a very clean-cut way to get up and running with some basic queries, which is useful when you’re building up a new concept. If you prefer, another good option is to use SQLAlchemy for Python, which is famous for its object-relational mapper. The idea here is that you can create Python classes to mimic the database table you are querying, and thereafter manipulate the database information via working with Python objects. If you’re looking to avoid writing SQL queries by hand, the package provides a way to keep more of your code Pythonic, and perhaps more natural to the developer. The database is the anchor to the entire application, so it’s certainly worth some effort to decide on a scheme that best suites your site.
7. Rapid development
All of my points above are evidence that you can get a prototype of an application up and running quickly, which is a common point made about Python. Between a framework like Flask, some add-ons to deal with a user authentication system and database connectivity, and a quick launch on Heroku, you can see results fast (especially with the succinct nature of the language itself). In an environment where you want to try new things and see what sticks, that’s a huge benefit.
8. Straightforward synchronization
Python has very well-documented and relatively easy-to-use synchronization primitives. With a bunch of users accessing your site and changing your internal information, you’ll need to think about protecting your information so it’s clear who has access when. Python has clean syntax for locking up your data structures, setting conditions under which more events can proceed, etc. These considerations may not come into play immediately, but having these tools on the back burner will pay off in the long run. No one wants to be deadlocked.
9. Multiple options for asynch performance
I mentioned some good solutions to create an “ordinary website” in the sense most people think of — go to different pages on the site, fill out some forms, click some shiny buttons, etc. Here at Josh.ai we’re developing a personal assistant to manage your smart home, which means real-time communication is a huge consideration for our development. With these demands, the average website isn’t a great fit because the focus needs to be on real-time speed rather than giving users nice pages to look at. Fortunately, Python has options. Asynchronous communication is the idea that the user can ask Josh to “turn on the lights in the kitchen,” for example, at any time, and that Josh might give the user useful information at any time as well. That means the traditional request-response cycle doesn’t quite cut it. A popular python project to deal with type of scenario is Tornado. You can see right on the home page that the developers classify the framework as an “asynchronous networking library.” Perfect! It has WebSockets available, which are a great means to send data back and forth from client to server and vice versa whenever you please. Another potential solution worth researching is the gevent websocket package, which uses lightweight “green” threads to attempt to handle the communication quickly.
10. Perfect for beginners
Lastly, many sources cite Python as ideal for beginning programmers. As I described, Python pulls in elements from many different languages, which gives new programmers many flavors with which to experiment. Users can find great tutorials and learn how to put together a project while learning important programming paradigms. It’s great to pull up a terminal and start testing some python code, and relatively straightforward to get an environment set up to get off to a good start. Once you’re comfortable, all you have to do is import PyBrain and you’re an expert in neural networks.
This article was written by Nate at Josh.ai where he focuses on the cloud and web. Previously, Nate was a global markets analyst at RBC Capital Markets in New York. Nate has a CS degree from Cornell and grew up in New York. Nate recently moved to Colorado and outside of work he likes to play golf and tennis, hike, work out, and he’s looking forward to skiing.