Monday, July 12, 2010

My 2 cents on web development using python

If you don't already know, I just love programming in python. I was told back in 1996-97 to use python but those were my early programming days on windows with VB so the idea of python went over my head. It took me around 5-7 years of development to realize the benefits of python and open source. So when I converted myself toward Linux and open source, python became my favorite programming language and though there have been a few others that I tried (Ruby, Perl) and have worked more often with (PHP), python still remains my first choice.

Every now and then I get the need to do web development in python; be it using django on some project or using GAE (Google AppEngine) to launch a site I always felt the need for something better in the web development area. The biggest problem for me seemed the issue of learning a whole new language for templating and a complete framework for creating a website. The framework bit wasn't that bad but why would I want to learn yet another programming language (which I might not like eventually) when I want to program in python?

About 6 months back, I decided to create a product (more on that some other day) and obviously python was the choice for programming. The issue was that this product required using templates; not for generating HTML, but instead for generating html, javascript and php code. This time I carefully examined all the available options like database layers and ORMs like SQLAlchemy, SQLObject etc; templating languages like jinja, genshi, mako and their older variants etc. There wasn't a need for a web framework so I was saved the trouble of deciding on one of djano, pylons, zope, turbogears and some micro-frameworks. The database layer doesn't give much choice so the obvious choice for me was SQLAlchemy. Its reflection capabilities really were critical for the project.

The templating language category had many many options and after careful consideration I decided on mako. Sure the biggest factor for me was that I could use python syntax and functions inside mako instead of learning a whole new templating language syntax and its associated functions/filters etc, etc. Both of these choices have worked well for me in the project.

Now a few days ago I again needed to make a choice as I decided to create a Intranet browser based application that involved mostly reporting and graphs. So the time to choose was upon me, having previously worked with django and GAE I didn't really want those kind of features. Zope seemed too huge and too complex. Some micro-frameworks like flask, bottle and others were also good candidates but then I decided on pylons. It by default supports SQLAlchemy and Mako. Though we can use any other option for ORM and templating language but the default choice says quite a bit about how the pylons developers want pylons to work. Being in favor or Mako and SQLAlchemy myself, it seemed more natural.

Now a bit about pylons, unless you have developed for the web a lot and have developed big projects with many many files with lots of lots of code; the benefits of pylons might not become clear to you. In fact, it might seem quite complicated (specially if you are coming from a php background). That is quite understandable. Pylons uses MVC (Model View Controller) approach towards web development. Don't panic if by hearing MVC, old Visual C developers come to mind :) PHP developers might need some getting used to changing 3-5 files for just getting changes done to a single page but once you get the hang of it (which can only be done with some practice, not just reading), you'll feel that developing this way is much better. A pylons project is structured in a way that it encourages code reuse with very little effort. Most importantly, the benefits start increasing as the size of project starts growing.

The extra effort placed upfront in a pylons project is well worth it in the long run. Just remember, you don't have to use everything pylons has to offer in your very first project. If you like to generate your html yourself, you can skip using the form helpers etc. Don't feel like going through the ORM, use the database directly and use plain old SQL if you like. The choice really is yours. Once you get used to some of the features then trying others would be a good idea.


No comments: