Sunday, December 29, 2013

Deploy Django app in heroku

Here is how a Django app deploy to heroku.Django app should be success in your local host.Then read carefully,it is something confusing.But leave it,i will help you..


Your application should be like this..

project
-------project
--------------__init.py
--------------setings.py
--------------urls.py
--------------wsgi.py
-------manage.py


This setup will get using commands

django-admin.py startproject project

then,

cd project
django-admin.py startapp paint  #here i am adding a paint app

Care that we are in the main project folder,

Then Whole project should be like this..


project
-------project
--------------__init.py
--------------setings.py
--------------urls.py
--------------wsgi.py
-------paint
--------------__init__.py
--------------models.py
--------------tests.py
--------------views.py
-------manage.py
-------requirements.txt
-------Procfile
-------venv


Don't be upset,we should cover how these gets...

In settings.py we have to add some codes..

import urlparse
import dj_database_url
DATABASES['default'] =  dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)


Add this line also

here = lambda * x: os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
PROJECT_ROOT = here("..")
root = lambda * x: os.path.join(os.path.abspath(PROJECT_ROOT), *x)
TEMPLATE_DIRS = (
    root('templates'),
)

Delete already existing template directory in your settings.py.But dont edit already existing database

your wsgi.py shoulbe

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())

Then run command

Heroku login     #to enter username and password
heroku create websitename   #should like www.websitename.herokuapp.com
heroku addons:add heroku-postgresql:dev
heroku config:set HEROKU=1
heroku pg:promote HEROKU_POSTGRESQL_PINK   #PINK is here only your's can be find using
virtualenv venv --distribute   #virtual environment for flask
source venv/bin/activate
pip freeze > requirements.txt

requirements.txt should be like this

Django==1.6
argparse==1.2.1
distribute==0.6.34
dj-database-url==0.2.2
dj-static==0.0.5
gunicorn==18.0
psycopg2==2.5.1
static==0.4
wsgiref==0.1.2

then,

pip install -r requirements.txt
git init

Create a Procfile using vi Procfile ('P' is capital) and add following line in it

web: gunicorn project.wsgi

Also add a .gitignore file using vi .gitignore and add following lines in it

venv
*.pyc
*.py~

Then,

git add .
git commit -m "hosting"
git push heroku master 

After launching it,

heroku pg:promote HEROKU_POSTGRESQL_PINK   #PINK is here only your's can be find using

heroku config | grep HEROKU_POSTGRESQL

heroku run python manage.py syncdb

Then,'

heroku ps:scale web=1
heroku restart --app websitename

heroku open

Then you can see it is working......!!!!!!

heroku logs #for further info

If u have any doubts just clone my simple paint app using,

heroku git:clone -a latheefdjangopaint

and verify the codes and structure of Django app

Deploy Flask app in Heroku

In a flask app,database let us consider postgresql

Now we have to deploy Flask app in heroku which is success in local host.First,

In our app,database connection is  like this

con = psycopg2.connect(database='firstdb') 
cur = con.cursor()

Instead of this,use....

urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.environ["DATABASE_URL"])
con=psycopg2.connect(database=url.path[1:],user=url.username,password=url.password,host=url.hostname,port=url.port)

also import some modules

import os
import psycopg2
import urlparse

wherever database to connect,we should connect like above in each function..

Next in routes.py or the routing python program.our app includes

app.run(debug=True)

Instead of this...

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port)


Then also add

app.config.from_object(__name__)

in the below of app = Flask(__name__)..so it should be like this..

app = Flask(__name__)
app.config.from_object(__name__)


Then,

Create account on heroku.com
open terminal and enter

Heroku login     #to enter username and password
cd appfloder      #appfolder includes route.py,templates etc...
heroku create websitename   #should like www.websitename.herokuapp.com
heroku addons:add heroku-postgresql:dev   #creating database
heroku config:set HEROKU=1
virtualenv venv --distribute   #virtual environment for flask
source venv/bin/activate
pip freeze > requirements.txt

requirements.txt should be like this

Flask==0.10.1
Jinja2==2.7.1
MarkupSafe==0.18
Werkzeug==0.9.4
argparse==1.2.1
distribute==0.6.34
gunicorn==18.0
itsdangerous==0.23
psycopg2==2.5.1
wsgiref==0.1.2

then,

pip install -r requirements.txt
git init

Create a Procfile using vi Procfile ('P' is capital) and add following line in it

web: python routeprogam.py #here route program is yours

Also add a .gitignore file using vi .gitignore and add following 3 lines in it.This is for avoiding the virtual env files to deploy.

venv
*.pyc
*.py~

Then,


git add .
git commit -m "hosting"
git push heroku master 

After launching it,

heroku pg:promote HEROKU_POSTGRESQL_PINK   #PINK is here only your's can be find using

heroku config | grep HEROKU_POSTGRESQL

Then,'

heroku ps:scale web=1
heroku restart --app websitename

heroku open

Then you can see it is working......!!!!!!

heroku logs #for further info

If u have any doubts just clone my simple microblog app using,

heroku git:clone -a latheefmicroblog

and verify the codes and structure of Flask app

Saturday, December 28, 2013

Unit testing in Python

Unit testing may be define as testing each units of a program.Unit testing in python has a separate testing python program for original program.

Let us explain with an example:
i want to create a program which accepts a number and check whether odd or even.

In python unit testing testing,we must assume the original program to predict requirements,inputs,outputs,errors etc..

Let us assume original program is oddeven.py and test program is testoddeven.py.

First we must know the requirements,inputs,outputs,errors..
Input may be int,string,float etc..
So errors may arise when input is string,float,zero.

So lets start our testoddeven.py

import oddeven #our original program
import unittest #python inbuilt module

class KnownValues(unittest.TestCase):
knownValues=((1,'odd'),(2,'even'),(11,'odd'))

Here some input and expected output is defined in a class

import oddeven #our original program
import unittest #python inbuilt module

class KnownValues(unittest.TestCase):
knownValues=((1,'odd'),(2,'even'),(11,'odd'))
def testoddeven(self):
for i,j in self.knownValues:
result=oddeven.testoe(i)
self.assertEqual(j,result)



Here checking whether the output equals to output of our oddeven.py Now we have to design errors or assume errors.Here possible errors are
zero,float value,string.Now i am defining expected errors.

import oddeven
import unittest

class KnownValues(unittest.TestCase):
knownValues=((1,'odd'),(2,'even'),(11,'odd'))
 
           def testoddeven(self):
for i,j in self.knownValues:
result=oddeven.testoe(i)
self.assertEqual(j,result)
class OddEvenBadInput(unittest.TestCase):
def testZero(self):
self.assertRaises(oddeven.ZeroError, oddeven.testoe,0)
def testNonInteger(self):
self.assertRaises(oddeven.NonIntegerError, oddeven.testoe,6.5)
def testString(self):
self.assertRaises(oddeven.StringError, oddeven.testoe,"abc")

if __name__ == "__main__":
unittest.main() 


Then start coding oddeven.py Then define the errors in original program sa below.

class ZeroError(Exception):pass
class NonIntegerError(Exception):pass
class StringError(Exception):pass


Then define each line and test each time using python testoddeven.py Rewrite the program until 'ok' gets...




class ZeroError(Exception):pass
class NonIntegerError(Exception):pass
class StringError(Exception):pass

def testoe(n):
if type(n)==int:
if n==0:raise ZeroError,"zero..."
elif n%2==0:
return "even"
else:
return "odd"
elif type(n)==str:
raise StringError,"string...!!!!"
else: raise NonIntegerError,"Non int...."

unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures are tested to determine if they are fit for use. Unit tests are created by programmers or occasionally by white box testers during the development process.One can view a unit as the smallest testable part of an application.Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended.

To view the codes.Click here

Wednesday, December 25, 2013

Python flask microblog application using postgresql

   
As in the previous example,there used sqlite3.Here database is postgresql

    Blog app act as a small scale blogspot account which provides options such as posting commenting etc.Blog app uses postgresql database to store the post and comments. psycopg2 module provided by python is used for it.Posting can be done only by the admin. Admin has to log in to post to avoid fake users.

documentation on flask


These kind of things takes a lot of work to code in normal networking in python.  Flask makes everything easier.Also flask helps the debugging much easier. The blog app is my first work in flask and it didn't give me much headache.
Let me show you some basic items in flask.

@app.route('url')
def func_name():
.....

Here first line tells us 'Where to go'.For example @app.route('/') means home page, @app.route('/post') means post link(ie home/post/).

Under that we define the function to tell the app 'how to behave' when these links are provided. We can render an html page, read posts from forum, save data etc.

In short if have a 'not bad' knowledge in flask then web based apps can be easily made. Flask is a great web framework provided by python where rookies can have a shot too.

Postgresql setup:

    sudo apt-get install libpq-dev python-dev
    sudo apt-get install postgresql
    sudo -u postgres createuser --superuser $USER
    sudo -u postgres psql 
    sudo -u postgres createdb $USER

Now you can create database in terminal using command psql

    create database databasename;
    \c databasename;   #to connect database
    create table posts(id integer,title TEXT,text INT);
    \d   #show tables
    \d tablename #to show corresponding table
    then use select query and more...

Connection:

To connect postgresql,we must install psycopg2

    sudo apt-get install python-psycopg2
 
In a program connection done through this lines.for example:

import psycopg2
con = psycopg2.connect(database='firstdb')
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS paintstore1")
cur.execute("CREATE TABLE paintstore1(id serial,title text,imagedata text)")
con.commit()
con.close()

To view the codes: Click here
To view the site i deployed to heroku,click here

Monday, December 16, 2013

Paint app using Django and sqlite3 as databae

Django is a web framework designed to help us build complex web applications simply and quickly. It’s written in the Python programming language.Django takes it name from the early jazz guitarist Django Reinhardt, a gypsy savant who managed to play dazzling and electrifying runs on his instrument even though two of the fingers on his left hand were paralyzed in an accident when he was young.Thus, it’s a fitting name for the framework: Django can do some very complex things with less code and a simpler execution than you’d expect. It doesn’t take a heavy hand to build with Django. The framework does the repetitive work for you, allowing you to get a working website up quickly and easily.

Django setup:

After install django,make a directory to store our project.

Open the terminal and enter the following commands:

mkdir djando
cd django
django-admin.py startproject mysite

this produces:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

This is the general structure of django.Here we didnt add any apps.

First we have to setup the database.Open settings.py and do as following.Here i am using sqlite3 database.


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'test.db',
    }
}

Then enter the command:

python manage.py syncdb   #Creates database and corresponding tables for django

Now its time to add our paint application.so

python manage.py startapp paint

this produces following directory like

├── manage.py
├── paint
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── mysite
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Here we are...

Here model view controller(MVC) is the routing procedure or model template view.
In models.py we have to setup database model
In views.py we have to setup routing functions or rendering functions
In urls.py corresponding urls are determined

Then we have to add this app in settings.py

INSTALLED_APPS = (
#    'django.contrib.admin',
#    'django.contrib.auth',
#    'django.contrib.contenttypes',
#    'django.contrib.sessions',
#   'django.contrib.messages',
    'django.contrib.staticfiles',
    'paint',
)


In models.py

from django.db import models

class Pic(models.Model):
        name=models.CharField(max_length=100)
        data=models.CharField(max_length=10000000000)

Then we have to create template directory and static directory inside.
So this will like

├── manage.py
├── paint
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
├── templates
└── static

Then htmls routing functions are done.To view the codes click here

To view the site i deployed on heroku.Click here

Paint app using Flask and postgresql

Paint application using postgresql i defined earlier is implementing in Flask.Here database is postgresql.

The paint app that was created using HTML5 canvas and javascript is modified with a save option. When each object is drawn all the details about that object is saved as a JSON object. Then it is saved to the postgreSql database.

For example when a Rectangle object is drawn the details such as width, height starting points etc are saved as a JSON object.Then it is added to a list


After everything is drawn the list will contain all the info of drawn objects as a list in the order of appearences.

Then it is saved to the database.

Example: Saving object info using JSON object.

Here on each drawing function JSONDATA.push is used.for example:

jsondata.push({"Type":"pencil", "X0":sX,"Y0":sY,"X1":mX,"Y1":mY,"width":c.lineWidth,"color":c.strokeStyle})data=JSON.stringify(jsondata)

Then this 'data' is posted to python program and insert to database using sql query.

At the time of reloading a selected picture these details are retrieved from database and redrawn in the canvas.
The following code shows how to redraw a rectangle in the canvas. The stringified data is parsed before it is drawn on canvas.

l=JSON.parse(data);

Then check the type in list 'l' and redraw.For example:

if(l[i]['Type']=='line'){
context.beginPath()
x0=l[i]['X0']
y0=l[i]['Y0']
x1=l[i]['X1']
y1=l[i]['Y1']
context.moveTo(x0,y0)
context.lineTo(x1,y1)
context.lineWidth=l[i]['width'];
context.strokeStyle=l[i]['color'];
context.stroke()
context.closePath()
}
context.beginPath()
x0=l[i]['X0']
y0=l[i]['Y0']
x1=l[i]['X1']
y1=l[i]['Y1']
context.moveTo(x0,y0)
context.lineTo(x1,y1)
context.lineWidth=l[i]['width'];
context.strokeStyle=l[i]['color'];
context.stroke()
context.closePath()
}

jsondata.push({"Type":"pencil", "X0":sX,"Y0":sY,"X1":mX,"Y1":mY,"width":c.lineWidth,"color":c.strokeStyle})data=JSON.stringify(jsondata)

By using flask the whole paint app became efficient ,interactive and useful

To view this code : Click here

To view the paint app which i deployed in heroku click here