Web
Article

Python on the Web: Why Frameworks Like Django Are Hot

By Shaumik Daityari

Created in the early 1990s by Guido van Rossum, Python has grown in popularity over the years. In 2016, Python is the 4th most popular language after Java, C and C++. Python is a general purpose programming language, and it can be used in a variety of fields. To quote Kenneth Love from Treehouse

When I need to build a web app, I reach for Python. When I need to automate some small task on my system, I reach for Python. When I want to find the most common colors in an image, I reach for Python.

Python is a popular choice for writing scripts for testing and monitoring. Python has also been used for game development, and its ability to be integrated with other languages makes it very valuable in the process. Such is the popularity of Python that it’s also been used by George Lucas’ Industrial Light and Magic (responsible for special effects in the original Star Wars trilogy) to manage its complex production process.

Unlike PHP, it wasn’t built for the web in mind, and there are no core web functionalities that are integrated into Python. Hence, we must use a web framework to develop web applications in Python. Web developers have started using it for the web since the rise of popular frameworks like Django.

What makes Python the go-to language for an increasing number of developers when it comes to web development these days? We’ll try to find an answer to this question in this post.

Why use Python?

Python for beginners

The primary reason for the popularity of Python is the elegance of the code — the brevity and readability in particular. For instance, let us look at how Python and Java stack up against each other in terms of reversing a number:

Java vs Python

Programs to reverse a number in Java (left) and Python 2.7.x (right)

Python provides a short learning curve, making it ideal for beginners to learn. In addition to that, if your project is Python-based and new developers aren’t familiar with it, the transition is easier.

As Quora co-founder Adam D’Angelo says on the choice of Python for Quora’s development —

So far, we’ve been pretty happy with the choice … all of the early employees who’d been working with other languages in the past were happy to transition to Python, especially those coming from PHP.

In fact, the webcomic xkcd came up with a cartoon on how easy it is to get things done in Python!

Python webcomic by xkcd. Source: xkcd.com

Further, Python has easy to use debugging tools. Although there are several debuggers and IDE tools, the default one is pdb, an interactive debugging tool which allows a developer to stop the execution of a program midway and assess the environment to better understand run time errors.

Python on a remote server

The management of packages (or modules as they are called) in Python is very easy too. Use a package installer like pip or easy_install and it can be used to install and remove packages.

Python is very portable too! The ease of transferring your development environment to a remote machine is commendable. You just need to export the packages, and install it in a virtual environment on the remote machine with just two commands.

One more reason why Python is a good choice for web applications is the ability of running scripts which are not embedded to the web server (unlike running a PHP script.) Scripts are run as separate processes.

Web Development using Python Web Frameworks

Python’s web capabilities are not built into its core features, and therefore, we need the help of modules to enable web development in Python. If you want to develop very basic applications — such as a page for checking whether a server is running — you may use the cgi module.

With an increase in the requirements, one needs to move to a Python framework like Django or Flask to be able to develop complex web based applications.

Most of these frameworks need to be coupled with a web server like Apache for production purposes, and this is where a minor bottleneck is present. As compared to PHP, ease of deployment is a bit difficult on a remote web server if you are doing it for the first time in your life (although we have covered that as well). However, the time that you invest would definitely reap rewards later.

Popular services like Instagram, Reddit, Quora and Disqus were all developed using Python.

Django — the most popular Python Web Framework

A question naturally arises: Why use Python for web development? My answer is firstly to stop thinking of it as just being about using just “Python” for web development. It’s frameworks like Django and Flask that make web development in Python so feature rich and powerful. For instance, the following are a few things that Django does really well as compared to any other web framework:

  • Easy Learning Curve. Just like Python, Django is easy to learn. You just need to go through this step-by-step tutorial on Django’s website, which does a great job of explaining the basic features of Django clearly. On the other hand, learning something like Ruby on Rails might take a longer time.
  • Creating SQL tables automatically. Django does this for you once you’ve specified the structure. (Note: SQLAlchemy does the same, and there’s a heated debate about this on Hacker News, if you’re interested.)
  • Generate Forms. Once you’ve created a Form class in Django and linked it to a model, the form generator in Django takes care of rendering forms, data validation and saving the data.
  • Admin Interface. Just like the SQL tables, once you’ve decided the structure, Django creates an admin interface that allows you to manage the database. (It’s just like PhpMyAdmin built into Django.)
  • Django Shell. The Python shell, within the Django project’s environment, is what the Django shell provides. This feature is useful in debugging, which is relatively more difficult in PHP.

If these are not enough, there’s cutting edge development going on in Python. Two exciting services in Python are Tornado (for asynchronous tasks) and Celery (for job scheduling). You may want to look at them if they fulfill your needs.

Django Web Hosting

The biggest hurdle for someone to start working on Django is the availability of web hosts. Obviously, due to the popularity of PHP, the number of hosts are high and hosting is relatively cheap (but not necessarily value for money, as cheap hosting is often shared). However, there are hosts like AWS and Heroku that allow you to host your Django applications for free (within certain limits). For shared hosts at a pocket-friendly price, you may go for a host like WebFaction.

The most popular cloud hosting services today like AWS, Digital Ocean and Microsoft Azure provide root access to the server, which allows you to manually set up your environment. Digital Ocean, in fact, has come up with one-click Django deployment. Django hosting might not be as popular as the PHP counterparts, but reliable options at different pricing levels are available.

Final Thoughts

The purpose of this article is not to tell you that Python is the solution to every problem! It is to highlight the parameters of Python and its web frameworks, which make life easier for web developers.

Have you tried Python and its web frameworks before? Do let us know in the comments below.

  • Todd Zmijewski

    There really isn’t anything great about Python. Most languages out there are easy to learn. Most frameworks do what you described and much more. Nothing special. Its all really just a matter of preference. I would also go as far to say that if you’re going to learn something it might as well be JavaScript not Python. Everything web related is moving in the direction of JavaScript not Python. Coincidentally everyone says javaScript/MEAN is easy to learn as well. Not to mention their aren’t nearly as many frameworks or cms’s built on Python. In comparison to more mainstream solutions you end up re-inventing a lot of wheels for really nothing more than some slight syntax changes.

    • Sponsor Ni Wewe

      Your point?

      • George

        There really isn’t anything great about Python. <- seems to be Todd's point.

    • N.T

      I do Python in my spare time and I really enjoy it.

    • David D

      Python librairies are greats, documented, well tested.
      Python is not the best for all your needs, but it is ok to use for most of your needs, and this point is something great (except if you like learning 1 language per need?)
      It is not a matter of preference, it’s facts.

      Python is a lot easier to learn/use than Javascript.
      The only reason Javascript is so Popular in webdev today is because it’s the only “web full stack” language. Legacy.

      • http://dada.theblogbowl.in/ Shaumik Daityari

        Totally agree with you, David.

        Just the other day, I had to filter and import data from a text file, I used Python over JavaScript or PHP (although I am equally comfortable) because of the availability of the libraries.

  • Helen Davis

    I’m a fan of Python))

  • Fernando Guerrero

    I really enjoy using Python. Such a powerful and elegant language

  • Helen Davis

    Python is like a jack-of-all-trades allowing for functional programming ideas. Other benefits of the language, to my mind, include easy deployment, great options for database work, straightforward synchronization, and abundance of external apps that fit into its Django framework without much headache. I really like the prebuilt functionality of Django. Yes, Python is perfect for beginners, but in my company Iflexion even most experienced developers refer to this language for a great number of projects.

    • http://dada.theblogbowl.in/ Shaumik Daityari

      Perhaps, it’s the flexibility of the language that makes it a favourite of many for everyday tasks.

  • Ralph Mason

    Thanks for the feedback, especially on the typo!

  • Ralph Mason

    Thanks for the feedback, especially on the typo!

  • http://www.timefortheweb.com/ Soumen Das

    Well, Python is really a most popular and effective programming language for web development these days. Thanks for the elaborated article.

  • Zhilevan Ibra

    I’m a developer since 2003 and experienced very programming languages, C,C++,VB.NET, C#, Java, Php, and Python, I work on Python and Php since 2008, I love both,I’m pro Drupal and Symfony drupa and I worked with Django too,Really Python is awesome but I believe Django is not matured enough yet,poor multi language support, poor vocabulary management ,… . I believe Python is most powerful than Php but Php Frameworks are better that Python framework and CMS’s. maybe in future we save matured python awesome Frameworks.

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Instant Website Review

Use Woorank to analyze and optimize your website to improve your website to improve your ranking!

Run a review to see how your site can improve across 70+ metrics!

Get the latest in Front-end, once a week, for free.