Monday, December 29, 2008

GWT : How to clear HTML Table completely?

This is the way many programmers cleans HTML Table (FlexTable and Grid are subclass of HTML Table). 

private FlexTable table = new FlexTable();

int i = 0;
for (List row : result) {
   int j = 0;
   for (String element : row) {
      table.setHTML(i, j, element);
      j++;
   }
   i++;
}

Programmer could also add widgets to the Flex table.

table.setWidget(i, j, new Label("Name"));

And to clear the table, programmer call clear() method of HTML table.

table.clear() 

Description of the clear() method says "it removes all widgets from this table, but does not remove other HTML or text contents of cells". So it will remove the label added to the Table but not HTML added in the for loop. Many times it leads you to wrong result.

For example: Lets assume table (4 x 4) contains 'A' in each cell 

AAAA
AAAA
AAAA
AAAA

Now programmer called table.clear() and write (3x3) elements with data 'B'. If he tries to print the complete table, he will see following result which is not expected.

BBBA
BBBA
BBBA
AAAA

How can we eliminate this problem? Solution : remove all the row one by one before adding new data to table.

for (; table.getRowCount() > 0; ) {
   table.removeRow(0);
}

Feel free to comment on this blog.

Friday, December 12, 2008

Running Python Script on the Mac Apache Web Server

The Mac OS X operating system has build in Apache Web Server and libraries to run python script. So user does not need to install any extra software to run python script on apache web server.

Open System Preference -> Sharing window and enable 'Web Sharing' option. It starts Apache web server on your machine. Wow so easy !!!


The default location to place CGI programs for the OS X apache installation is in the following directory:

/Library/WebServer/CGI-Executables

Files placed in this directory can be accessed via the following URL:

http://localhost/cgi-bin/script-name

Lets create a simple python script named hello.py under /Library/WebServer/CGI-Executables directory.

#!/usr/bin/python
print "Content-type: text/html\n\n"
print "<html>"
print "<head>"
print "<title>hello</title>"
print "</head>"
print "<body>Test Page</body>"
print "</html>"

Set the executable permission to the python script (chmod 755 hello.py). Now access this script via http://localhost/cgi-bin/hello.py

If webserver throws some error, please look at the log for detailed information. Logs are stored in /var/log/apache2/error_log file. NOTE: location may differ based on the version of OS and version of apache server.


Thursday, December 11, 2008

Python os.spawnl returns error code 127

In this blog I am giving solution to the most common problem when programmers try to execute os.spawnl command. Most of the programmers get 127 error code at the time of executing command os.spawnl. For example

>>> import os
>>> os.spawnl(os.P_WAIT, 'ls', 'ls', '-l')
127

The error code "127" means that the system can't find the exectuable.

Solution: Use absolute pate of the executable.

>>> import os
>>> os.spawnl(os.P_WAIT, '/bin/ls', 'ls', '-l')
-r-xr-x--- 1 abc unknown 2994 Nov 5 21:25 create_
-rw------- 1 xyz unknown 7938048 Oct 8 19:09 nohup.out
0

Hope, this trick helps you.

Friday, December 5, 2008

Google Friend Connect : adding social features to your site

Google Friend Connect means more people engaging more deeply with your website -- and with each other. By adding Google Friend Connect to your site, you can give social network's effect to your website. Your users can easily invite friends from social networks and contact lists to visit and join your site. Just copy and paste a few snippets of code into your site, and Friend Connect does the rest.




Members Gadget : The Members gadget allows visitors to join your site, sign in and out, see other members, see pictures of their friends, explore other members' profiles, invite other people to join your site and use other social features. 

Wall Gadget : Allows viewers to post comments, or links to videos on your site.

Review & Rate Gadget : Allows viewers to rate a page, or a section of a page.



These are few gadgets which give social network effect to your website with minimal effort. So go and enjoy these cool gadgets.