killer web development

Getting Pythonic (Useful Notes)

Here is a very basic python notes from extracted from the very basic introduction in Killer Web Development Part 6.

In 6.1
Firstly, Mr Laspe statement is not true. How do you run web2py.exe from the tukker app folder when that file doesnt exist there? Secondly, if you run web2py.exe, you start a server and i never get a web2py console before. Thirdly, doesn’t “./web2py.py -S tukker” create a new tukker application? It just doesn’t make sense!

Anyway, what i am going to do is to type “python” in the console to get “>>>” prompt.

>>>print “something” or print ‘something’ both acceptable

Print several line breaks
>>>print “””something
… lalala
… lalala”””

combine string (leave a space in between)
>>>print “I love” +” “+”Mama.”
or
>>>print “i love “+”mama”

variables are like normal but have to begin with alphabetic letter or underscore. other characters have to be alphanumeric.

operators
+ – * /
** exponential
% modulo
boolean returns true or false. can be used to compare sting as well.

Lists
enclosed in square brackets []
to append a new value
>>>list.append(5)
to change a value in the list
>>>list[2]=6
index starts from “0”
last index starts from [-1], then [-2]…

Slicing
to get certain parts of the list, use colon “:”
>>>list1=[1,2,3,4,5]
>>> list1[0:]
[1, 2, 3, 4, 5]
>>> list1[1:]
[2, 3, 4, 5]  #start at index 1
>>> list1[1: -2] # start at index 1 and slice off at index -2
[2, 3]
>>> list1[:-2]
[1, 2, 3]
>>> list1[:-1]
[1, 2, 3, 4]
>>> list1[:]
[1, 2, 3, 4, 5]
also works with strings and tuples.

Dictionaries
to map a key to a value
enclosed in curly braces {}
often used by controller function which will return a Dict and available to the view.
>>> dic = {‘title’ : ‘ZAPP’, ‘headline’ : ‘awesome app’}
>>> dic [title]
‘ZAPP’
>>>print dic[‘title’]
title
>>>print dic[‘title’]
ZAPP

6.2

use 4 spaces as indentation of code blocks

if expression1:
codeblock
elif expression2:
codeblock
else:
codeblock

while expression:
codeblock

for element in sequence:
codeblock

Functions
def functionname([argument]):
codeblock

Killer Web Development Walkthrough Part 5

Good day!
Yesterday wasn’t that fruitful. Didn’t understand anything about how Python work and couldn’t get the devices to communicate through PySerial. So how do you represent CTRL+C in Python? I found no answer from the internet.

Anyway, i have to go through web application anyway. So i’m doing the Killer Web Development tutorial again, on Section 5 today – HTML and CSS.

I will focus more on the troubleshooting of the problems faced in the tutorial and solutions, if any. Will add side notes to complement the tutorial too.

in Section 5.3
– Download skeleton.css from here because it is not by default included in the static/css folder. Then put that skeleton.css into the your application static/css folder. Then you can start using the “container” style for your webpage.

– I don’t know why but the “register” button will not appear on my browser.

– create custom.css under static > create file with filename, and paste the codes then save it. The style will be applied to h1 in the index.html

– download superfish.css from here and save in the static/css folder. To add sf-menu to main menu, edit <ul> to <ul> (the tutorial gave <div> which makes me wonder, where to put it!?)

Here is a useful site having the index of all Window Command prompts

cp in linux is “COPY” in window command prompt

Good note:

What does the {{=URL(c=”default”,f=”user”, args=”register”)}} statement do?

– the double curly braces open Python code
– the equal sign (=) makes sure that the result of the Python statement gets filled in the page – don’t forget this!
– the URL helper creates an URL based on the parameters provided. This way the same code can be used on different domains, without breaking the link. In this case:
c=”default” – the controller (c) is default
f=”contact” – the function (f) in this controller is user
args=”register” – one argument (‘args’) is register, which is computed by the function user

This is the final web app i get… looks very different! :/ oh well.

ZAPP

ZAPP

Killer Web Development Part 4 – Windows version

Haha i’m such an idiot! Who says i must use Raspberry Pi to develop my web application???

Now, moving on to use Window XP to continue my Killer Web Development!
Ref: http://killer-web-development.com/section/4/3

I downloaded the window version of web2py and run web2py.exe. Then i start the server. Awesome, everything works fine.
The codes below are a close reference to the link above. Just that all my codes are in windows format and for my future reference. Remember to download gedit first!

Writing first test:
C:\Document and Settings\Administrator>cd Desktop\my name\web2py_win\web2py
> .\web2py.exe -S zapp //there is a space before “.”
> CTRL+Z Enter //to exit from python shell

open up gedit from program files and type in the following, then save into the zapp folder under the file name “.hgignore”

syntax: glob
cache/**
databases/**
errors/**
sessions/**

*.pyc

Then back in the command prompt,
>hg init
>hg add .
>hg commit -m”initial commit”

Install Selenium

First, create the necessary directory:

>cd applications\zapp
>md fts
>cd fts
>md lib
>cd lib

Go to Python package selenium page https://pypi.python.org/pypi/selenium/2.15.0 to download the tar.gz file
extract using winrar or something else and move the selenium-2.15.0 folder to zapp\fts\lib and rename it to “selenium”

Then create another 2 .py files using gedit.
1. The blank __init__.py in zapp/fts directory
2. functional_tests.py in zapp directory (copy and paste the code from the tutorial http://killer-web-development.com/section/4/3)

Time to Test Selenium
>cd .. (to go up one level)

okay, just open gedit and copy the codes and save into /zapp/fts directory.
Then go up one level again (to zapp dir)

>cd ..
>python functional_tests.py

If you see this: ” ‘python’ is not recognized as an internal or external command, operable program or batch file.”, refer here

>python functional_tests.py

If you get “ImportError: No module named selenium”, edit the functional_tests.py file and change

sys.path.append(‘./fts/lib’) to
sys.path.append(‘./fts/lib/selenium/selenium/py’)
(check your selenium folder, see screenshot)

python: can’t open file ‘../../web2py.py”: [Errno 2] No such file or directory

change ../../web2py.py into ../../web2py.exe
get “SyntaxError: Non-ASCII character ‘\x90’ in file ../../web2py.exe on line 1, but no encoding declared;”

– can’t find solutions, let’s change strategy!

Download the source version from web2py website. extract and move to the same directory. copy the zapp folder to web2py_src/web2py/applications.

Then run from there. Get the error: no handler – download pywin316 will solve the problem.

selenium still causing some problems which i still cant figure them out. argh.

hg commit and blah blah.
From here onwards, i decided to leave out the selenium test.

And learn HTML. Going to recall my task so that i won’t drift too far off. Laugh.

Killer Web Development Walkthrough Part 4 continue

This continues from Killer Web Development Walkthrough Part 4

Skipped the Selenium part, moving on to “Fix our first test”. I couldn’t perform hg add fts/*. it gave me such error:

abort: /bin not under root

Leave it for the time being.

And i just found out one serious problem. In my zapp folder, there is only the .hg and fts folder. the necessary files are not there! so i perform
./web2py.py -S zapp again and it returns me the following:

OSError: [Errno 2] No such file or directory: ‘applications/zapp/language’

but the languages.py is there! in the /web2py/gluon

since i can’t create any new applications for now, (perhaps due to the changing of ownership yesterday argh), i shall copy the pitch application and rename it as zapptest and edit from there.

Start from installing selenium again. blah blah blah and FAILED.

In the end, i decided to change the ownership back to root. lesson learnt, not everyone replied to your forum questions can solve your problem!

sudo chown root:pi /home/www-data/web2py

okay, ownership changed and now i can create a new app!

sudo ./web2py.py -S zap

and there i go, repeating the same procedure from the start, just that now everything has to be in sudo mode. weird.

i copied the hgrc file from pitch too.

well after going through the whole process again, hoping for this miracle moment, it still gives the same error…

ERROR:Rocket.Errors.Port8001:Socket “0.0.0.0:8001 in use by other process and it won’t share.
WARNING:Rocket.Errors.Port8001:Listener started when not ready.

All right, moving on to “Fix ur first test”
$ gedit views/default/index.html

can’t perform the test.

With that i concluded section 4, with failures. sigh. I wonder if i should continue from here? 3 more chapters to go. next chapter will be mostly about testing the app!

Mozilla Firefox on Linux Debian?

Previously, i was following through a tutorial at Killer Web Development and i encountered a problem while trying to use Selenium… because I only have Iceweasel and not Firefox. So, here’re my experiences in finding the answer: Will update when i try something new! :s

6/3/2013
1. Well, thank goodness i found an answer for my Firefox and Iceweasel problem here.

First, you need to remove the existing Iceweasel package

apt-get remove iceweasel

To use Linux Mint’s Debian package repo, add the following line to your /etc/apt/sources.list file:

cd /etc/apt
nano sources.list

add the following line:

deb http://packages.linuxmint.com debian import

Then, just run:

apt-get update
apt-get install firefox-l10n-en-us

And FAILED.
E: unable to locate package firefox-l10n-en-us

Err http://packages.linuxmint.com debian/import armhf Packages
404 Not Found

😦

2. Same goes for this:
apt-get remove iceweasel
echo -e "\ndeb http://downloads.sourceforge.net/project/ubuntuzilla/mozilla/apt all main" | tee -a /etc/apt/sources.list > /dev/null
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com C1289A29
apt-get update
apt-get install firefox-mozilla-build

3. I tried to download the latest Firefox onto my Raspberry Pi, but the

download just won’t start on Midori. Hmm, has been unable to download

since days ago i guess.
Nevertheless, i downloaded firefox for linux from here

http://www.mozilla.org/en-US/firefox/all/
and transfer the files to R-Pi using Core FTP LE.

Then, i followed the instructions in another reply under the same forum thread, and tried to copy the downloaded file to /opt

$ cp firefox /opt

cp: omitting directory
The command cp is by default copies only files and if we try to copy a

directory it will throw the above error.

To copy a directory using “cp” all we have to do is add the option “-r”

which means recursively copy all the files from the source directory to the

destination directory. i.e.

cp -r dir1 dir2

Anyway, i still have the bz2 file so,

sudo cp firefox-19.0.tar.bz2 /opt

(without sudo, permission denied)

And yes, copied successfully.

Extract it using:

$ cd /opt
$ sudo tar -jxvf firefox-19.0.tar.bz2

Change the permissions of the file:

$ chown -R root:users /opt/firefox # OPERATION NOT PERMITTED

$ chmod 750 /opt/firefox

Note that the user should be a group member of “users”

$ usermod -a -G users username

Create a symbolic link

$ ln -s /opt/firefox/firefox /usr/bin/firefox

Delete firefox-19.0.tar.bz2 from /opt
cd /opt
sudo rm firefox-19.0.tar.bz2

Conclusion: stuck at unable to change owner. but i doubt it is already under the root ownership. However, i still cannot run the functional_tests.py. some errors to do with selenium still. Anyway, i am going to skip the step for the mean time.

Some useful reference:

To copy files, you use the cp command. The following will copy file to file2. Note that if file2 doesn’t exist, it’ll be created, but if it exists, it’ll be overwritten:
$ cp file file2

There aren’t any undo commands in the Linux CLI, so accidentally overwriting an important file would probably make you pull your head off. The risk of doing so is smaller if you use the -i option (“interactive”) with cp. The following does the same as the above, but if file2 exists, you’ll be prompted before overwriting:

$ cp -i file file2
cp: overwrite `file2'? n
$

So it’s a good idea to use the -i option whenever you’re dealing with important files you don’t want to lose!

If you want to copy file into directory dir1:
$ cp file dir1

The following would do the same as the above, copy file into dir1, but under a different name:
$ cp file dir1/file2

You can also copy multiple files into one directory with a single command:
$ cp file1 file2 file3 dir1

Note that if the last argument isn’t a directory name, you’ll get an error message complaining about it.

The mv command can be used for moving or renaming files. To rename a file, you can use it like this:
$ mv file file2

If file2 doesn’t exist, it’ll be created, but if it exists, it’ll be overwritten. If you want to be prompted before overwriting files, you can use the -i option the same way as with cp:

$ mv -i file file2
mv: overwrite `file2'? y
$

To move the file into another directory:
$ mv file dir1

If you want to rename the file to file2 and move it into another directory, you probably already figured out the command:
$ mv file dir1/file2

The rm command is used for removing files and directories. To remove a file:
$ rm file

If you use the -i option, you’ll be prompted before removing the file:
$ rm -i file

You can also delete more files at once:
rm file1 file2

Be careful with the rm command! As I already told you, Linux doesn’t have any undo commands, and it doesn’t put files into Trash where you can save them later. Once you’ve deleted a file, it’s bye-bye!

Killer Web Development Walkthrough 3

 An App for a Pitch

 

Let’s move on to the next section of Killer Web Development. We are going to create an application from the command line, and not through the web2py interface.

 

1. Creating a Pitch App

 

Creating the pitch app confused me a bit because I didn’t install web2py following his tutorial, remember? So I have no idea on where is my web2py.py file located.

 

$ cd /your/web2py/folder  # on Windows use backslashes

$ ./web2py.py -S pitch   # web2py.exe on Windows and web2py.app on OS X

I spent some time searching for “how to find a file on Linux” and finally I found some clues of using “find”.

 

$find / -name 'web2py.py'

It searched for a while and found its directory – /home/www-data/web2py/web2py.py

 

So I typed in

cd /home/www-data/web2py

./web2py.py -S pitch   (no idea what is -S, couldn’t find it on web)

Now it is time for Mercurial.

 

“In the terminal go to the directory of the pitch app; create a repository; then create a .hgignore-file; add files to the repository and make your initial commit:

$ cd applications/pitch

$ gedit .hgignore

Line 1 is misleading because it should be the complete directory.

For my case:

 

$ cd /home/www-data/web2py/applications/pitch

 

Then at $ /home/www-data/web2py/applications/pitch $ type in
gedit .hgignore

A gedit file will be created. I typed in as instructed but realized that it could not be saved. Yes, I am not the super user!

So, I should type in sudo gedit .hgignore at the terminal. Now, another gedit file is created but without the index 1 2 3 etc at the front when the codes are typed in. The file is saved. Interestingly, this reminds me of Chemistry! Hg is mercury periodic symbol.

 

Again, at the next few command in the terminal, “sudo” has to be included at the front;

 

sudo hg init

sudo hg add .

sudo hg commit -m”Initial commit”

–                    no username supplied. Well seems like the .hgrc file in the previous section comes to use here? I saved it in home/www-data/web2py/applications/pitch (since it says save in home directory).

–                    still the same though. May be I should save it with the web2py pitch file.

–                    Could not save the file as I do not have the permission to save the file in that directory.

–                    Try to save in /home/ – no permission too

 

Found this under the comment: Hg Init

More things to read up here!

 

So in the end, I use the terminal to open up a new file.

 

cd /home

sudo gedit .hgrc

 

I typed in the username and such and save it.

 

Now, try out the hg commit. It is still the same. Well then, till tomorrow!

Killer Web Development Walkthrough 2

Still on the Killer Web Development tutorial. Step 2 walkthrough:

I installed :

  • the text editor gedit
  • the web browser Firefox with the Firebug-extension
  • the version control system Mercurial

I actually installed them on my Raspberry Pi as well as Window XP.

Start off with Window XP:

Gedit : download from here  (thanks to firebug!)

Python : I chose python 2.7.3 for my Window XP

Didnt manage to set the path properly I guess, because it didnt show me the path when I typed “python” in command prompt.

Firefox

Firebug

Firebug is one awesome Firefox extension!! it is like a recce bug sent off to a destination before I’m going there. Well, not exactly but I can check out the destination before I click on a button! Fights off malicious links! It is able to analyze websites too. Have not try it out though but surely helpful for my site analytic.

Press F12 will toggle to firebug window and pressing Ctrl + shift + C will be in the bug mode!

Mercurial: not really sure what it is used for though.

Next, on my Raspberry Pi Linux system.

Gedit: sudo apt-get install gedit

Python: already installed

Firefox: no such thing in Debian system!

Instead, it is called Iceweasel. Haha, funny huh?

sudo apt-get install iceweasel

Firebug: sudo apt get install iceweasel-firebug

(Not too sure if just the last one will work.)

Mercurial: sudo apt-get install mercurial

I just followed blindly to set up the mercurial text file (“first time setup”) without knowing what is the purpose though.

I skipped the Cygwin section.

Killer Web Development Walkthrough 1

1. Introduction

 I have somehow came across an awesome website called “Killer Web Development” (http://killer-web-development.com/). It is an online tutorial written by Marco Laspe, all about how to create real world web applications using web2py, an open source web development tool, and other stuffs related such as Python.

 He said, “The only prerequisites for this book are your enthusiasm and that you have basic knowledge how to use your computer.” Yes, I do! And I hope the enthusiasm will grow! 🙂 The problems that I am having is the unfamiliarity with Linux OS and Python language. Even though I did a project on Android and Python last year, I could not recall how the interface looks like. Opps, this sounds ridiculous.

1.1 Introduction to web2py

Web2py is a rather new tool developed by Massimo Di Pierro in 2008. I tried it a few days ago when I was following another tutorial on creating a web application to control the lighting at home using R-Pi (http://www.instructables.com/id/Raspberry-Pi-GPIO-home-automation/). I will recall that on my blog when I have time. It was quite fun following though the steps in instructables.com and I learnt a lot!

Perhaps, lets start on the Killer Web Development! (mostly based on the tutorial and my own side notes)

1.2 Start your Engine

The first step was easy – get the web2py on your computer! (http://killer-web-development.com/section/1/3)

I did not follow the first few steps as I have gotten my web2py through this method:

  1. On R-Pi system, start the terminal and log in.

  2. sudo su

  3. wget http://web2py.googlecode.com/hg/scripts/setup-web2py-ubuntu.sh
    chmod +x setup-web2py-ubuntu.sh

    ./setup-web2py-ubuntu.sh

***

sudo su – is to set the user mode to superuser whom is capable to do anything as the root user.

wget – which I observed, is to download something from the internet directly

chmod +x – is to grant execute access

./ – is to run a certain shell script?

During the installation, it is important to include the email and password, so that I can log into the server.

Then I could access web2py server at https://10.0.0.192 (my domain IP) , which will show a web2py default home index page. HTTPS is particularly important because the server is secured. I think there shouldn’t be a problem if download from the website manually (http://web2py.com/examples/default/download), unzip it and run it. The only thing to mind is the IP address (which I spent quite sometime to figure out for my R-Pi last week) and the port number. I am wondering why is it 8000 instead of 8080.

8000

UDP

iRDMI (Intel Remote Desktop Management Interface)[86]—sometimes erroneously used instead of port 8080

Official

8000

TCP

iRDMI (Intel Remote Desktop Management Interface)[86]—sometimes erroneously used instead of port 8080

Official

8000

TCP

Commonly used for internet radio streams such as those using SHOUTcast

Unofficial

8000

TCP

FreemakeVideoCapture service a part of Freemake Video Downloader [87]

Unofficial

8080

TCP

HTTP alternate (http_alt)—commonly used for Web proxy and caching server, or for running a Web server as a non-root user

Official

8080

TCP

Apache Tomcat

Unofficial

8080

UDP

FilePhile Master/Relay

Unofficial

 (source : wikipedia http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers)

Posted it as a question to Mr Laspe and wonder if he will reply :O .

Image

Next is to log into the Administrative Interface using my password. Here you could see some of the applications that I have created for the past few days. I just followed through the steps on the rest of the section.

Image

The interesting thing I learnt is the format of displaying the text.

return dict(message=T(‘Hello World. I\’m John. How are you?’))”

There is a complete Application Programming Interface (API) of web2py which can be found here: http://web2py.com/book/default/chapter/04#API

T” is the internationalization (i18n), which confused me for a while but after some readings, I somehow understand what it is for. It basically structures application in a way that makes it possible for them to localized. So cool!

So, “T” means “language translator”. Which if it contains message bundle for certain language, it is able to be translated. Awesome huh?

dict” means dictionary! Read more here (http://docs.python.org/2/tutorial/datastructures.html#dictionaries) but I do not quite understand.

Then it is almost the same as normal html editor, which I am not too familiar with. Also, “Notice the Backslash (\) behind the I – this is important because Python uses ticks to encapsulate Strings.” Then Ctrl+S to save.

(Hmm, I wonder what have I learnt in my study years! *rant rant*)

1.3 Deploying Your First App

Moving to next section – Deploying the web application. Yes, the word “deploy” confused me but all the answers are on Google.com

To install, test and implement a computer system or application” is the best answer I got from http://www.webopedia.com/TERM/D/deploy.html.

There are a few ways to deploy our web application which includes fluxflex.com ( I have not try it), Google’s App Engine, Pythonanywhere and etc (http://web2py.com/books/default/chapter/29/13).

It is my first time knowing Google’s App Engine, sounds cool. I should explore it when I am more advanced in this field.

So the tutorial guides us through how to deploy using http://pythonanywhere.com. I signed up for free account and got locked out once because I forgot the password (I think I mistyped it). I deleted the new application, log out, log in, and recreate a new application (choose web2py format and upload the w2p file). Then at http://yvonnezoe.pythonanywhere.com my website is online! Well it looks the same as https://10.0.0.192.com/first just that this is live and everyone could see it rather than just on my server. Awesome, I found another hosting!

1.4 Conclusion

Oh man! I found another interesting site called Diaspora! Check it out here: https://joindiaspora.com/. Perhaps with my little determination and some encouragement from all around the world (from you, reading this), I can be an older version of Mark Zuckerberg. Laugh. Okay, KIV but no Diaspora during working hour! (in fact, no blogging during working hour, but i’m treating this as a log book and not crapping!)

Okay, till next post! 🙂

– Zoe –