2

I am new to web development. After much consideration I picked Python over Ruby simply because code readability is important to me. I have looked at a few Python cgi code and I wondered if it is possible to polish that code in such a manner that I can focus on program functionality without having to have all the HTML formatting code embedded (cluttering) in the Python source, i.e. separate the actual Python code from the HTML code. Perhaps this style of programming then leaves the realm of CGI into something else? (i.e. CGI = script that generates a web page).

1
  • Thanks all! After much reading, I've decided to convert my old-school CGI way of doing things to a mature full-stack framework: web2py. Coming from Apple's XCode MVC way of thinking, web2py will prove to be very easy to deploy! Commented Apr 3, 2011 at 2:07

3 Answers 3

4

Pick a template engine. And it's still CGI, it just happens to be CGI that uses a template engine; CGI is the interface, not the programming methodology.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for opening a whole new world of options! For simple HTML page generation, which package would you suggest? Thanks for clarifying the meaning of CGI.
I'm a fan of Genshi, but there are those that suggest Mako or Jinja2.
1

is possible to polish that code in such a manner that I can focus on program functionality without having to have all the HTML formatting code embedded (cluttering) in the Python source

Use a templating engine

Perhaps this style of programming then leaves the realm of CGI into something else?

CGI isn't a style of programming. It is a means for a web server to execute a program to determine what data to provide to the user. (It is a slow and inefficient method, but it is also a very simple one).

If you want to get away from CGI and stay with Python, then you should probably look towards wsgi

3 Comments

Will look at templating engines. Can you suggest one? I am also curious as to why CGI is slow and inefficient. What would be faster? Does HTML generation have to be faster? Thanks for the wsgi suggestion, I will take a look.
I can't suggest one, my Python experience is rather limited. The performance issues with CGI are due to it having to spawn the entire Python (or whatever you use) process for each request, this imposes a startup cost.
That could be eliminated by adding mod_python to Apache, but you are right, CGI in its simplest form is primitive and expensive.
0

New web application code in Python isn't written using CGI, it's based on WSGI. Separating your HTML generation from the rest of your program logic is indeed a good thing, and should be done using a templating engine. Python also has several frameworks for writing web applications; if you're just starting out, I recommend Flask.

2 Comments

Any specific reason why Flask is preferred over the much hyped Django?
Django is probably a good choice if you want to build the CMS-style things it's good at. Flask is a more minimal and flexible library that takes very little work to start using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.