Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How nicely does Python 'flow' with HTML as compared to PHP?

Tags:

python

html

php

I'm thinking of switching from using PHP to Python for web applications, but I was wondering if Python is as adept as weaving in and out of HTML as PHP is. Essentially, I find it very easy/intuitive to use <? and ?> to put PHP where I want, and am then free to arrange/organize my HTML however I want. Is it just as easy to do this with Python? Fundamentally, the question is: is working with HTML with Python similar to working with HTML with PHP in terms of ease-of-use?

Edit: I guess to help clear up some of the confusion in the comments below, I get the intuition that PHP would be better than Python at organizing the front-end, presentation part of a website, while Python would excel at the back-end part (the actual programming...). The question is - am I wrong and is Python just as good as PHP for front-end?

Edit of my Edit: Ah, I'm starting to understand the error of my ways; it seems I have unknowingly picked up some bad habits. I always thought it was okay (read: standard) to, for example, have PHP in pseudo-code do the following:

If user has filled out form:
    print this html
else:
    print this html

When in fact I should use an HTML template, with the PHP in a sep. file. And in that scenario, PHP and Python are on an even fighting field and it's probably up to my own programming language tastes.

like image 275
Tony Stark Avatar asked Aug 21 '09 13:08

Tony Stark


2 Answers

You can't easily compare PHP and Python.

PHP is a web processing framework that is designed specifically as an Apache plug-in. It includes HTTP protocol handling as well as a programming language.

Python is "just" a programming language. There are many Python web frameworks to plug Python into Apache. There's mod_wsgi, CGI's, as well as web application frameworks of varying degrees of sophistication.

The "use to put PHP where I want" is not really an appropriate way to judge Python as language for building web applications.

A framework (like Pylons, Django, TurboGears, etc.) separates the presentation (HTML templates) from programming from database access. PHP mixes all three aspects of a web application into a single thing -- the PHP language.

If you want to switch from PHP to Python you must do the following.

  1. Start with no preconception, no bias, nothing.

  2. Start fresh with a tutorial on the framework you've chosen. Do the entire tutorial without comparing anything you're doing to PHP.

  3. Start fresh on solving your chosen problem with the framework you've chosen. Build the entire thing without comparing anything you're doing to PHP.

Once you've built something using a Python-based web framework -- without comparing anything to PHP -- you can step back and compare and contrast the two things.

Folks who ask questions like Python - substr, java and python equivalent of php's foreach($array as $key => $value), what is python equivalent to PHP $_SERVER? are sometimes trying to map their PHP knowledge to Python. Don't Do This.

The only way to start using a Python web framework is to start completely fresh.


Edit

All Python web frameworks have some "presentation logic" capabilities in their template engines. This is a "slippery slope" where you can easily turn a simple template into a mess. Clearly, a simple {% if %} and {% for %} construct are helpful to simplify conditional and repetitive elements of an HTML template.

Beyond that, it starts to get murky how much power should be put into the tag language.

At one extreme we have PHP/JSP and related technologies where the template engine can (and often does) do everything. This turns into a mess. Is the middle are Jinja and Mako where the template engine can do a great deal. At the other end is Django where the template engine does as little as possible to avoid mingling presentation and processing logic.

like image 178
S.Lott Avatar answered Nov 15 '22 17:11

S.Lott


Well. First of all, you must know that mixing code with presentation is considered bad practice. Although you can use template engines that let you mix any python code inside the html, like Mako, usually python web libraries and frameworks tend to favor writing logic on a python script file and a separate html template to render the results.

That said, python is much easier than PHP.

But you can only be productive in python If you're willing to learn its way of programming.

If you want PHP, nothing is more PHP than PHP itself, and python takes different approaches, so maybe you're going to be frustrated because things are not like you're used to.

However, if you are searching for a new, better way of doing things, python is for you.

After reading the basic python tutorial, try some python web framework like pylons and see for yourself.

like image 25
nosklo Avatar answered Nov 15 '22 18:11

nosklo