Django wrapper

9

Click here to load reader

Transcript of Django wrapper

Page 1: Django wrapper

FusionChart Django Wrapper

Definition:

Python 2.7 provides some interesting concepts like decorators or wrappers. The function

decorators or wrappers change the action of the original function before or after the execution of

the function. In this type of case the original function does not modify itself but the working is

decorated by the wrapper according to its principal. The easiest example is wrapping a function

return by some html/JavaScript tags. If a function returns some name as output, the decorator can

modify it using the <p>, <b>, <i> tags etc.

Example::

Input:

def get_result(intro):

return “Captain America, {0} in civil war” .format (intro)

def pb_ decorate (func):

def func_wrap(intro):

return “ <p><b>{0}</p></b>” .format (func(intro))

return func_wrap

mah_movie= pb_decorate ( get_result)

print mah_movie ( “Chris Evans”)

Page 2: Django wrapper

Output:

<p><b> Captain America, Chris Evans in civil war</b></p>

However, calling the decorating function has a clear shortcut. This kind of function can be

identified prepending ‘@’ in it. In the above example the decorator should be @pb_decorate ().

Django wrapper:

This special type of decorator or wrapper provides the advantage of class based views over the

function based views. The wrapper organizes server side HTTP methods like Get, Post etc using

separate methods instead of branching ( if/else / for/ while). The wrapper also provides object

oriented process like ‘mixins’, ‘prefork working module’ etc. The wrapper uses toolkit and

multiple inheritance (‘mixins’) to increase the extensibility and flexibility of the class based

attributes.

CODE:

from django.http import HttpResponse

from django.views.generic import View

class GreetView(View):

greeting = "Nice Day"

def get(self, request):

return HttpResponse ( self.greeting )

In the above code The URL resolver of the Django wrapper expects the arguments from a

function and not a class therefore the view method serves as the opening point of the class. The

Page 3: Django wrapper

view method creates an interface for dispatch () method to identify the type of request

(Get/Post/not allowed response) and works according to it.

Purpose of the Wrapper:

The python module (wrapper) creates an interface between python and a third party library. The

advantage of this kind of module is the full use of only python codes even while calling the non-

python third party library. The ‘Django wrapper’ can be used as following:

Creating charts (Stacked, Bubble, 2D/3D Pie chart) using HTML/ Javascript codes as a

string to the server

Creating a SQL database using only python code and interfacing the database abstractly

(OO concept of ‘abstraction’) into the server

The ‘FusionChart Django wrapper’ can create charts using the usual HTML/JavaScript codes

but sending those codes as a string to the server.

Explanation of a Sample using the Wrapper:

To create a chart using the wrapper the user should understand the steps in the codes. The chart

can be used in the project installing the ‘fusioncharts.py’ in the project and applying the classes

and methods under the’ FusionCharts ‘namespace. The charts can be rendered in a webpage

using the server request module of the ‘Django’ wrapper. The code of creating a chart is

explained below in details.

Step1: The HTML/JavaScript codes should be used as a string to create the chart. If the user uses

the ‘FusionCharts’ CDN, then he/she should include the path as a source in the script tag.

Page 4: Django wrapper

Code:

<script type="text/javascript"src="http://static.fusioncharts.com/code/latest/fusioncharts.js">

</script>

The ‘src’ attribute identifies the library link for the official CDN.

Step2: The code to create the chart includes the type of chart, data as well as type of parameters

used in this case.

Code:

Inclusion of Filename: app_name/views.py

Inclusion of `fusioncharts.py`

from fusioncharts import FusionCharts

def chart(request):

column2d = FusionCharts("column2d", "ex1" , "1200", "600", "chart-1", "json",

"""{

"chart":

{

"caption":" Targets of Supermarkets ",

"subCaption":"Top 3 stores in February by revenue",

"numberPrefix":"$",

Page 5: Django wrapper

"theme":"ocean"

},

"data":

[

{

"label":"Bakersfield Central",

"value":"880000"

},

{

"label":"Garden Groove harbour",

"value":"730000"

},

{

"label":"Los Angeles Topanga",

"value":"590000"

},

]

Page 6: Django wrapper

}""")

return render(request, 'index.html', {'output' : column2d.render()})

In the code the arguments of the ‘fusionchart’ identifies Chart type, ID, width, height, format and

source respectively. The chart type is 2d column in this case. The ID identifies the chart in the

HTML source. The data source gives the original values for the chart. IN this case three values

are considered.

Step 3: The URL configuration considers the URL pattern of the chart.

Code:

Inclusion of Filename: app_name/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [

url(r'^$', views.chart, name='demo1'),

]

Step 4: The last step creates a specific decoration for the chart.

Code:

<h3><b><i>FusionChart</i></b></h3>

<div id="2DchartContainer">{{ output|safe }}</div>

Page 7: Django wrapper

The Django wrapper decorates the chart sample importing the JavaScript library in the python

interface only using the python code in the above way.