Categories
Python

Using web.py with Google App Engine

I love coding in Python and have been exploring the excellent web.py framework lately. From what I’ve seen, web.py is a very simple yet powerful python based web framework. It’d take me a lot of time to learn django (if I ever manage to make up my mind into it) and it already took around a couple of hours to understand the Google App Engine’s default webapp framework.

web.py includes a built in web server to develop an application locally. But I am a Google fan and was thinking of deploying web.py on Google App Engine. I believed GAE and web.py together can help me put up a web app in minutes. And so did they… 😀

The first question came to my mind was “How am I going to install web.py on my GAE account ?”. Well, it was pretty easy in the end. Just extract the web.py package and put the “web” directory inside your application directory. That is the “web” directory would be in the same directory where the app.yaml file resides. Now, in app.yaml file, map all URLs to a single python file. In my case, it was “main.py”.

Now develop the application in the normal web.py way. “It’s easy as pie!” — I thought. But it was not that easy in fact. I had to make a couple of changes.

The app I wrote didn’t do what I expected it to. Rather it raised an “import error” 🙁 Later, I found out that I have to use “web.application.cgirun()” method instead of the normal “run()” method.

Then I reloaded the app. But this time I got an “internal server error”. After visiting the web.py cookbook, I found out that, to use web.py templates on GAE, I have to compile all templates using the “web/template.py –compile templates” command. I did so. All my templates were compiled into python source code. Cool !

Now my app was running smooth and working fine ! So, at last I made it… Yahoo ! I am loving python, web.py and Google App Engine together 😉

PS: I am using web.py 0.3 where I have the cgirun() method. In older versions of web.py, you have to be tricky. In that case, please consult this thread:

http://bit.ly/EoVyM

Here’s the source code (Without the template):

Here’s the “templates/masnun.htm”:

Categories
Python

ZIP, Python and TK

The title says it all. Here’s a very simple program written in Python and TK which can extract a ZIP file to the selected location.

Python 3 – Source Codes:

Explanations It creates a TK interface with a button and a text entry widgets. Clicking on the button invokes the work() function. The work() function displays a filedialog and prompts you to choose a ZIP file. Then it creates a new ZipFile object using the file location. The extractall() method of the ZipFile object extracts all contents of the archive into the directory specified using the inputBox text entry widget. We call inputBox.get() to get it’s value. If the value of target location is null, the files are extracted into the current working directory. In case of an invalid target location, it prints out error messages to the console.

It’s a very basic program with a little functionality. It mainly demonstrates the use of TK and Python to write GUI programs that really does something.

Categories
Python

Building GUI with Python and Tk

I have been looking for a decent GUI toolkit to develop some desktop programs. I worked with Java. Didn’t like the idea of typing that much to develop a app. Used php with WinBinder and PHP-GTK. Liked the first one, but the binary I have won’t load the winbinder extension. To use it, I will have to go back to php4. Again I don’t like the idea of using php4 for GUI while using php5 for web development. So again I had to make a swtich. This time I went for wxPython for Python 2.6.0. Alas, though wxPython used to work fine on my XP, it no longer runs on Windows Vista.

Finally, I deceided to work with TK which comes pre-installed with Python. The IDLE, Module Docs and other GUI tools which comes with a standard Python installations are most often developed using TK on Python.

I liked TK quite a bit because the process of developing TK GUI apps are damn easy and great fun. While learning TK, I converted my old console twitter publisher to a TK powered GUI app. Here’s the sourcecode:

Python 3 Source Code:

It’s very comfortable to work on TK. Write less, do more. A good set of widgets and a powerful language like Python as the backend… It’s simply cool 🙂