Saturday, January 17, 2015

How to set TEMPLATE_PATH for Python Bottle framework in Windows Environment

By default template path is set to ./ or ./views/ in bottle.py, however this will not work and the following error message appear always in Windows environment.
Error: 500 Internal Server Error
Sorry, the requested URL 'http://localhost:8080/' caused an error:
Template 'hello_world' not found.


To overwrite a template path “template_lookup” should be used under return bottle.template function as shown below:
template_lookup=['C:/Python34/Lib/site-packages/views']
*Make sure that forward slash used instead of backslash.
The following sample code illustrates this:
***********************************************
Python Code
***********************************************
import bottle


@bottle.route('/')
def home_page():
   mythings = ['apple','orange','banana','peach']
   return bottle.template('hello_world', username="Uma", things=mythings
                          , template_lookup=['C:/Python34/Lib/site-packages/views'])


bottle.debug(True)
bottle.run(host='localhost', port=8080)

**********************************************
Template "hello_world.tpl"
**********************************************
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<p>
Welcome {{username}}
<p>
<ul>
%for thing in things:
<li>{{thing}}</li>
%end
</ul>
</body>
</html>



Thursday, January 15, 2015

How to install Python modules using setuptools (easy_install/pip) in Window

Setuptools is not included in the standard library. It introduced a command-line utility called easy_install. It also introduced the setuptools Python package that can be imported in your setup.py script, and the pkg_resources Python package that can be imported in your code to locate data files installed with a distribution.
You can download the setuptools in the following link. https://pypi.python.org/pypi/setuptools
Once you unzip and using command prompt you can install using command prompt:
Python setup.py install
One you install you can see the relevant files such easy_install.py and pip.exe under script folder.
The following example show how to install or upgrade packages using pip command:
pip install <packagename>
The following example shows how to install Bottle module.

The following command are normal installation using easy_install.py in other environments
$ easy_install.py Package
$ easy_install.py http://sample.host/Package-X.Y.tar.gz
$ easy_install.py http://svn.sample.host/Package/trunk

Cheers!

Sunday, January 11, 2015

How to retrieve data from MongoDB and display via web applications using Python Bottle Framework

First start the Mongo server using mongod command. In this example the following document data base has been used.

Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.

  • Routing: Requests to function-call mapping with support for clean and dynamic URLs.
  • Templates: Fast and pythonic built-in template engine and support for makojinja2and cheetah templates.
  • Utilities: Convenient access to form data, file uploads, cookies, headers and other HTTP-related metadata.
  • Server: Built-in HTTP development server and support for pastefapws3bjoern,gaecherrypy or any other WSGI capable HTTP server.
Run this Code and run the web browser http://localhost:8080/
************************************************************
Code:
************************************************************
import bottle
import pymongo


@bottle.route('/')
def home_page():


   #connect to mongodb
   connection = pymongo.MongoClient('localhost', 27017)
   #connect to mydb database
   db = connection.mydb
   #handle to friends Collection
   data = db.friends
   #getting all the document from friends
   friendsList = data.find()
   #construct string variable with friends names
   myfriendnames = ""
   for item in friendsList:
       myfriendnames = myfriendnames + ", " + item["name"]


   return myfriendnames


bottle.debug(True)

bottle.run(host='localhost', port=8080)

Cheers!
Uma

Saturday, January 10, 2015

How to retrieve data from MongoDB using Python

First PyMongo module should install according to the Python version that you are going use. You can find the PyMongo in the following link:
Then, create a database and collection on MongoDB with database name: mydb and collection Name: friends as shown below.
The following code will retrieve the data as shown below.
Code:
*******************************************************************
import pymongo
from pymongo import MongoClient


#connect to Database
connection = MongoClient('localhost', 27017)
db = connection.mydb


#handle to friends Collection
data = db.friends


friendsList = data.find()


for item in friendsList:

   print("Name: " + item["name"] + "Age: " + str(item["age"]))
******************************************************************

Cheers!
Uma

Friday, January 9, 2015

How to install Python module or library in Windows, Mac and Linux

Usually module source distribution are in *.tar.gz or zip format. In windows you can’t directly unpack the tar.gz format. However you can using 7-Zip free tool you can unzip this format. The following image show how to unzip tar.gz format in Windows. We have to unzip twice.

You should always run the setup command from the distribution root directory, i.e. the top-level subdirectory that the module source distribution unpacks into. For example, if you’ve just downloaded a module source distribution bottle-0.12.8.tar.gz onto a Unix system, the normal thing to do is:
gunzip -c bottle-0.12.8.tar.gz | tar xf -    # unpacks into directory foo-1.0
cd bottle-0.12.8
python setup.py install
On Windows, you’d probably download bottle-0.12.8.zip. If you downloaded the archive file to C:\, then it would unpack into C:\ bottle-0.12.8; you can use either a archive manipulator with a graphical user interface (such as WinZip/7-Zip) or a command-line tool (such as unzip or pkunzip) to unpack the archive. Then, open a command prompt window and run:
cd c:\ bottle-0.12.8
python setup.py install


The following images shows how to install module after unpack.

Cheers!
Uma