Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,143,233 members, 7,780,446 topics. Date: Thursday, 28 March 2024 at 02:29 PM

Practical Introduction To Python Web Programming - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Practical Introduction To Python Web Programming (6333 Views)

Web Programming Tutorial In Python. / Sumit Raj Discusses Python Web Development, Chatbot, Natural Language Processing / Learn Python- Introduction To Programming (2) (3) (4)

(1) (2) (3) (Reply) (Go Down)

Practical Introduction To Python Web Programming by Nobody: 10:57pm On Dec 19, 2015
I wanted to start this simple tutorial early next year since i'll be at home this xmas holiday crunching chunks of chicken laps, however, having being addicted to programming and knowledge sharing, i was really tempted to begin this, so...............
Have fun learning and...............

All i want you to do is to pay attention, and.......pray that i have time to complete this tutorial
Happy coding.


Beginning

Python is an easy to learn, powerful programming language. It has efficient high level data structures and a simple but effective approach to object-oriented programming. Python's intuitive and dynamic nature makes it suitable for quickly prototyping systems, as an automating tool, and used highly in the scientific and web programming world.

In this beginner tutorial i will be explaining and demonstrating how to program in python as well as how to build a web application in python that performs basic CRUD operations. The truth is, if you know how to eat a chicken lap this xmas, then you're as good as a potential pythonistia.....

Pre-requisites:

difficulty: beginner (no python experience)

system: windows OS xp and above

tools:

python 2.7 from www.python.org (optional)

Notepad++ from www.notepad-plus-plus.org/download
my version v6.6.9 (i love this tool so much. It gives you syntax highlighting and code completion if configured, and it supports practically all major languages in existence. Thats all i can say).

web2py web framework: this is one of the most popular python web frameworks in existence designed specifically to teach web programming because of its simplicity, yet it is powerful and suitable for building large scale web applications. There are two major reasons why i choose web2py for our practical tutorial; one, web2py follows a pure MVC pattern. MVC means (Model View Controller). Other python web frameworks like Django follows a pseudo MVC pattern, i.e, MVT-U (model views templates......and url cos you have to edit the url patterns anytime you insert a new controller function). So web2py follows the explicit pattern in an MVC architecture, similar to ASP.NET MVC. Also, web2py is a full stack standalone web framework with no dependencies, infact, you can run it from a USB stick or on your mobile smart phone. Finally, it free and opensourced. Download it from:
www.web2py.com/init/default/download

under "For Normal Users", click "For Windows" to download.
Once downloaded as a zip file, you should use the winzip or winrar to unzip it. If you want to go for free software, You can google for universal extractor, an opensource winzip utility tool. Once, armed with the unzip utility tool, you are to unzip it in your C: \ folder. If you are confused, just unzip it in any folder, then copy the unzipped 'web2py' to your C: \ folder.
Re: Practical Introduction To Python Web Programming by Nobody: 11:14pm On Dec 19, 2015
Next, we are going to follow 3 - steps to know whether web2py is really working in our system

step 1 - web2py python interpreter:
you see, web2py comes with its own python interpreter called 'py'. This was why i mentioned earlier that downloading python 27 is optional because web2py comes with its own python 2.7! Now let us edit our system environment variables to make use of web2py's python interpreter for our programming practicals.

(Note: if you already have python installed on your system environments, you do not need to do this)
..............
..............
Re: Practical Introduction To Python Web Programming by Nobody: 11:44pm On Dec 19, 2015
(* Make sure your unzipped web2py folder is in C: \ i.e C: \web2py).

Now click on Start, in the empty text box where the cursor is blinking, type 'system environments' without the quotes, you'll see option for 'edit the system environment variables'. Click on it. The system properties box will pop up, click on 'environment variables'. When the pop up opens, check on your system variables, you will see 'path' or 'PATH', double click on it. Then add this at the end of the variable value:

C: \web2py\;C: \web2py\scripts

with no spaces, click ok, ok, ok to close all boxes.

Now, open a command prompt, change your directory to C: \ by typing this:

cd C: \

once its in C i.e ' C: \> ' without quotes then type ' py '. It should be like this:

C: \>py

if it displays some something like this:

python 2.7.9 blah blah
>>>

then it means the python shell is running, and guess what??!!

You have passed the first test!!

Now.....take your time get a glass of water and gently take a sip. You deserve it. Brb.
Re: Practical Introduction To Python Web Programming by Nobody: 12:49am On Dec 20, 2015
step 2 - playing with python

next, we will see the interactive and static side of python:

now switch over and type:

C: \>py

now in the python shell (>>>wink, type:

>>>print "hello i am learning python" , then hit the Enter key

what did you see?? Does it show any error??

Now type:

>>> import math
>>> math.pi
........
>>> _ + math.sqrt(9)

what happened??

Don't worry about what you typed now, i will come to that later. we just want to know whether our python is working.

Close the command line window.

Create a folder in your C: \ drive
called PythonLessons

Now, open the new installed notepad++ (install it if you haven't) and type this:

import datetime

now = datetime.datetime.now()

print "today's date is: %s" %now

save it as my_lesson.py in C: \PythonLessons

now, open your command prompt and change it to your PythonLessons folder, i.e type:

cd C: \ PythonLessons

it will appear like this below, and type in:

C: \PythonLessons>py my_lesson.py

what did it show? If it works, then you've passed the second test.
Re: Practical Introduction To Python Web Programming by Nobody: 1:45am On Dec 20, 2015
Now to the final step before i rest for the day......

step 3 - testing the web2py framework
All these while, we have been working with command line and the python shell, now lets work with the web parameters to know whether web2py can really work on our system. Remember, our major goal is not to build a console app, but a solid web app. So lets start......

Now, for a web framework to serve its purpose, it needs a web server. So web2py includes with it a web server called Rocket, a fast-wsgi server. So we need to fire up this server, but how??
This is where web2py comes in. It helps in pipelining the HttpResponses and HttpRequests between the server, application and database on one hand, and the clients(people accessing the page) on the other hand. This is the basic work of a web framework such as web2py.

Now lets create an application called myapp in web2py.

From the command prompt, change ur directory to C: \web2py and type this:

web2py -S myapp


web2py will ask you whether to create the new app? Type y meaning yes........and don't close the command prompt yet

now click on the C: \web2py and open the web2py folder. Click on the applications folder. You should see your newly created app in a folder. Click on myapp folder and open controllers. Under the 'controllers' folder, you will see 'default.py', right click on it and click on 'Edit with Notepad++'
once notepad++ is opened with default.py, you will see

def index() :

under this index(), clean everything and make it look like this:

def index() :
return "<h2> It worked!!</h2>"


make sure you hit the spacebar ONLY 4 times before writing the return ".....

Make sure you click on the Save button to save your work.

Go to the command prompt still opened and type this:

web2py -a '123' -p 8081


hit the enter key.

Now open your browser, preferably mozilla firefox and go to

localhost:8081/myapp/default/index

You should see the result on the web page.

If it runs successful, then.......


Congrats! You are ready to learn python web programming!!!

Good Morning!
Its 01:46am
Re: Practical Introduction To Python Web Programming by Nobody: 8:15am On Dec 20, 2015
Before we proceed, questions are accepted regarding the few steps we've covered so far......
Re: Practical Introduction To Python Web Programming by danidee10(m): 1:21pm On Dec 20, 2015
Good work bro.....I use django but it's nice to see someone making this kind of tutorial....don't get discouraged someone will appreciate this definitely
Re: Practical Introduction To Python Web Programming by Nobody: 2:22pm On Dec 20, 2015
Ive been busy..... Here is the sample of the webpage

Re: Practical Introduction To Python Web Programming by Nobody: 2:28pm On Dec 20, 2015
And this is a web app i am still building (from scratch) with web2py.....

Re: Practical Introduction To Python Web Programming by Nobody: 2:31pm On Dec 20, 2015
So any inexperienced python programmer can avail himself of this opportunity to learn web2py.......
Re: Practical Introduction To Python Web Programming by JigsawKillah(m): 2:44pm On Dec 20, 2015
basbone:
Before we proceed, questions are accepted regarding the few steps we've covered so far......

What are "basic CRUD operations"?
Re: Practical Introduction To Python Web Programming by JigsawKillah(m): 2:46pm On Dec 20, 2015
basbone:
Before we proceed, questions are accepted regarding the few steps we've covered so far......

I have Python 3.3 installed already
Won't that do the job? Only 2.7 can?
Re: Practical Introduction To Python Web Programming by JigsawKillah(m): 2:47pm On Dec 20, 2015
Thanks Mr Basbone for this opportunity
Re: Practical Introduction To Python Web Programming by Nobody: 7:02am On Dec 21, 2015
JigsawKillah:


What are "basic CRUD operations"?

CRUD means Create, Read, Update and Delete; derived from the fundamental query statements in sql - CREATE, INSERT, SELECT, UPDATE and DELETE

4 Likes

Re: Practical Introduction To Python Web Programming by Nobody: 7:06am On Dec 21, 2015
JigsawKillah:


I have Python 3.3 installed already
Won't that do the job? Only 2.7 can?

don't worry if you have python 3, just download the we2py framework for window binaries (follow the instructions above for download). It is included with it python 27, which will use for the tutorials.
Re: Practical Introduction To Python Web Programming by dayoungmoney1: 8:12pm On Dec 21, 2015
Good work bro. Keep it up!
Re: Practical Introduction To Python Web Programming by JigsawKillah(m): 9:34pm On Dec 21, 2015
basbone:


don't worry if you have python 3, just download the we2py framework for window binaries (follow the instructions above for download). It is included with it python 27, which will use for the tutorials.

Good evening Boss
Re: Practical Introduction To Python Web Programming by Nobody: 9:49am On Dec 22, 2015
Xmas things.......

Alright, lets continue.......


Brief introduction to python
python is an interpreted language, i.e, variables can be declared dynamically. In statically typed languages like c++, java, c#, etc, variables are first declared before assignment, e.g in c#:

int my_int = 4;
string my_string = "hello";
bool my_bool = true;

but in python, it is dynamically declared, e.g:

my_int = 4
my_string = "hello"
my_float = 60.54
my_bool = True

so when the python interpreter sees the quotations, it assumes ure declaring a string, when it sees numbers with a decimal point, it assumes ure declaring a floating or real number, when it sees True or False, it assumes youre declaring a boolean value.

Excercise:
What is 1) a variable? 2) a string? In python;

List the data types available in python.
Re: Practical Introduction To Python Web Programming by Nobody: 10:24am On Dec 22, 2015
Printing in python:
after we've declared our variables, we would like to know how they look like, so how do we know ??
We will use python's print() function......
*Note: this will be last i will use the python command shell >>>

now change ur directory to C: \ and start the python shell, type;

print "Hello Python!"

now type;

my_string = "i love python"
print my_string

python is a csv language. CSV means comma separated values. So python uses commas to print several values......

Now, one last use of the python shell;

type;

name = "Michael"
age = 18
profile = "Married: "
is_married = False
result = 50.23
print name, age, profile, is_married, result
Re: Practical Introduction To Python Web Programming by Nobody: 11:25am On Dec 22, 2015
Printing strings:
python uses the two types of quotes to print strings e.g:
string1 = 'hello'
string2 = "world"

however, there is a problem, what if we want to print "world" with the quotes, what do we do??


Escape sequences:
python provides us with a set of characters for manipulating strings and these set of characters are called escape sequences.
E.g to print out i don't know in python, we use \' to manipulate the don't, i.e, we will write:

print "i don\'t know"
or
print 'i don\'t know'

both codes will print the same thing.
Re: Practical Introduction To Python Web Programming by Nobody: 11:38am On Dec 22, 2015
Some of the most common escape sequences include:

\' for single quotation

\" for double quotation

\n for new line

\t for tab

\a for alert (gives a beep)

\\ for backslash;

and others......

Variable conversions:
you can convert a string to an integer and vice versa, e.g:

number = 15

alphabet = str(number)

or

alphabet = "10"

number = int(alphabet)

we will see how to use this when reading input from a user.
Re: Practical Introduction To Python Web Programming by Nobody: 12:08pm On Dec 22, 2015
Practical section:

now open a new file in notepad++ and type this;

print "my friend\'s name is Jesus, his mother\'s name is Mary.\nHe will be born on the \" 25th day of december, 2015\". \nI will be expecting the angels to sing the \t \"Halleluyah\" song."

save it in the C: \PythonLessons folder as lesson1.py

now, change ur directory to C: \PythonLessons, and type py lesson1.py to run the program, i.e:

C: \PythonLessons>py lesson1.py
Re: Practical Introduction To Python Web Programming by JigsawKillah(m): 12:31pm On Dec 22, 2015
Following ardently
Carry on Basbone
Re: Practical Introduction To Python Web Programming by Nobody: 4:18pm On Dec 22, 2015
We can also encode and format strings in python. E.g:

name = "Basbone"
age = 12
print "my name is %s and my age is %d" %(name, age)

this is called string encoding. Here, the %s is a string encoder while the %d is an integer or decimal encoder.

Learn more on how to use string encoders and string formatting in python.
Re: Practical Introduction To Python Web Programming by Nobody: 4:46pm On Dec 22, 2015
Introduction to python data chains:

according to www.thefreedictionary.com ;

a data chain is -

"any combination of two or more data elements, data items, data codes, and data abbreviations....."

so we need to briefly look at the data chains provided by python, and they are:
List

Tuples

Dictionaries

Lists: a list is a dynamic array of any objects. It is declared with square brackets [ ] ;
e.g
mylist = [1, 3, 'hello', 'ok']

in computer science, precisely in data structures, data count starts with 0, the knowledge of this concept is important in web programming when it comes to databases, sessions and cookies. So from the above list, the first element is 0, the second element is 1, and so on.


Learn some of list operations such as;
list assignment;

list.append()

list.remove()

len(list)

etc.
Re: Practical Introduction To Python Web Programming by Nobody: 4:55pm On Dec 22, 2015
Tuples: a tuple is similar to a list but it is immutable. In other words, it is an array that cannot change or alter the position of its values, they are just constant and unchangeable. It is declared using parenthesis;
e.g
my_tuple = (1, 3, 10, 'hi', 'python')
Re: Practical Introduction To Python Web Programming by Fulaman198(m): 5:19pm On Dec 22, 2015
Very nice job thus far OP
Re: Practical Introduction To Python Web Programming by JigsawKillah(m): 1:44pm On Dec 23, 2015
Nice work Sir
Re: Practical Introduction To Python Web Programming by Nobody: 2:43pm On Dec 23, 2015
I just downloaded notepad++ will only it be okay?
Re: Practical Introduction To Python Web Programming by Nobody: 4:18pm On Dec 23, 2015
MuhdG:
I just downloaded notepad++ will only it be okay?
Yeah, its ok.. Coupled with Python...
I hope u ve downloaded python already(version 2.7)..

@Basbone Nice Work Sire...
Re: Practical Introduction To Python Web Programming by Nobody: 5:42pm On Dec 23, 2015
Djangocode:

Yeah, its ok.. Coupled with Python...
I hope u ve downloaded python already(version 2.7)..

@Basbone Nice Work Sire...
Ohh d python is separate on its own?
Okay. How do i download it pls

(1) (2) (3) (Reply)

Shoplog V2.3 (Inclusion of license fee) / Nigeria Sucks On Programming. / Hospital Management System Program.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 50
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.