Showing posts with label Python - Django. Show all posts
Showing posts with label Python - Django. Show all posts

Tuesday, November 12, 2013

Django interesting links

Initialization tutorials:
  • TangoWithDjango. Fantastic tutorial that explains how to build a web site with Django 1.5.4 as a backend and Twitter Bootstrap as a frontend.
  • Django tutorial in four steps six steps (wow! Now there is a part introducing tests and another about static files management). The manual of the official django webpage. As the whole framework, the tutorial is excellent.

Miscellaneous:

Wednesday, October 23, 2013

Using the ndv3.js library

nvd3.js is an amazing d3 charts library.

However, it has few documentation in its website. In this post I put a summary of the useful things I've done when working with nvd3.
  • Integrating nvd3 with Django
django-nvd3 is a Django package which integrates nvd3 charts in a Django-Pythonic way. It also lacks a bit of documentation.

In this site there is the source code  of the package, what allows us to see the possible parametrizations of the chart:

def load_chart(chart_type, series, container, x_is_date=False, x_axis_date_format="%d %b %Y", tag_script_js=True, color_category='category20'):

The variable color category is the way to specify the colors range of the chart. However, in my case I had to specify its values in the javascript code instead of doing it directly in Python.

...
(javascript)
var mycolors = ["#80B898","#007030","#E58A01"];
d3.scale.mycolors = function() {
    return d3.scale.ordinal().range(mycolors);
}

(end javascript)

{% load_chart grafico.charttype grafico.chartdata "grafico_container" True "%d %b %Y" color_category='mycolors' %}
...


  • Customizing more nvd3.js charts
If you want further customization on nvd3.js charts then you have to move completely to the js environment. Some useful links I used:

Event trigger when  a serie is selected

Brush extent when working with a line with focus chart

Updating different d3 charts on the same page

Friday, October 12, 2012

Map-Reduce implementation in Python

Recently I discovered a Python implementation of the Map-Reduce framework.
It is mincemeat.py, and can be found here: https://github.com/michaelfairley/mincemeatpy
The advantage is that it is an only-file implementation and it is quite efficient.
There is an example attached with the downloaded file.
To run it, you need just to execute example.py, which will perform the server tasks.
For each client, just run mincemeat.py specifying the server port (127.0.0.1 by default).

Other implementations of the Map-Reduce pattern in python are:

octopy
hadoopy
dino
mrjob

Sunday, September 30, 2012

Django - User authentication with django-registration

http://stackoverflow.com/questions/6127264/newbie-django-error-templatedoesnotexist-at-accounts-login

https://bitbucket.org/devdoodles/registration_templates/src

Monday, January 2, 2012

Python - How can I iterate through two lines?

You want the zip function.

a = [1,2,3]
b = ['a','b','c']
for
(n,l) in zip(a, b):
print "num: ", n ,"; let: ", l



num: 1 ; let: a
num: 2 ; let: b
num: 3 ; let: c

Friday, November 11, 2011

Django - Context Processors

In some occasions we may need information available to each template, like the user properties, the session settings, or an special variable.

What we could do is to add all this information to the response dictionary. However, it would contradict the DRY principle of Django.

A good approach is to use a customized Context Processor, which will add to the dictionary the returned variables of the function.

Specifically, the modifications we must do can be the following:

  • Define the Context Processor function, like for example:
def context_processor_function(request):
return {'additional_values': ['a','b','c']}

  • Load the function inside the TEMPLATE_CONTEXT_PROCESSORS variable of the setting.py:
TEMPLATE_CONTEXT_PROCESSORS = ("django.core.context_processors.auth",
...
"path_to_context_processor_function.context_processor_function
",)
  • Use the variables inside the templates like any other variables of the dictionary
For more complete information, this links are useful:

http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/

https://docs.djangoproject.com/en/1.1/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS

http://stackoverflow.com/questions/335231/in-django-is-it-possible-to-access-the-current-user-session-from-within-a-custo

Saturday, October 1, 2011

Python - C++ like stream input

One of the things in which C++ is better than Python is in the stream input, using the fantastic cin option.

In this link there are similar ways to do the same with Python

http://stackoverflow.com/questions/417703/python-c-like-stream-input

Friday, September 23, 2011

Iterating through two lists in a Django template

http://stackoverflow.com/questions/2415865/iterating-through-two-lists-in-django-templates

Access array elements in a Django template

http://stackoverflow.com/questions/1700661/access-array-elements-in-a-django-template


Remember that the dot notation in a Django template is used for four different notations in Python. In a template, foo.bar can mean any of:

foo[bar] # dictionary lookup
foo.bar # attribute lookup
foo.bar() # method call
foo[bar] # list-index lookup

Thursday, July 14, 2011

Thursday, August 26, 2010

Django - Deploy an application to alwaysdata.com (French web site)

www.alwaysdata.com is a French web site which allows free django hosting (and others) for websites up to 10Mb.
The site includes a fantastic but in French documentation which explains step by step how to publish a django site:
http://wiki.alwaysdata.com/wiki/D%C3%A9ployer_une_application_Django

Remarks:
  • ssh and ftp connections are possible.
  • There is an admin site https://admin.alwaysdata.com/ from where we need to enable the ssh user before using it.
  • The database must be created from the admin site.
  • When we make changes we may need to restart the fcgi process. This must be done from the admin web site, with the processes option, by killing the process. Immediately it is restarted.
  • Notice, as it is explained in the instructions, that we create a public directory. Some content, specially the static files, must be in that directory to be served, although the development server does not use them, it uses the files in the typical directory.
  • Important! Don't forget to redirect all queries to the main domain to the specific folder /appname/public where the project relies. This must be done from the admin panel.
  • Instructions for working with the development server are in http://wiki.alwaysdata.com/wiki/Utiliser_le_serveur_de_d%C3%A9veloppement_int%C3%A9gr%C3%A9_de_Django

Wednesday, August 18, 2010

Django - Deploy an application to djangoeurope.com

I followed these steps to deploy a Django application into djangoeurope.com:
  • Register to djangoeurope.com and enter to the Panel (panel.djangoeurope.com)
  • Click into the installdjango option and create a project (it is called application in the webpage).
  • Run python manage.py startapp appname (the application should be called equal to our project's application).
  • Access with ssh and sftp to our space and put our original application into the corresponding folder. Overwrite all files except settings.py and settings_development.py.
  • Merge the files settings.py from djangoeurope.com and the one of our application.
  • Copy the content of settings.py to settings_development.py.
  • Run python manage.py syncdb.
  • Modify file lighttpd/django.conf to be correct.
  • Syncronize with init/appname restart, init/lightppd reload and init/lightppd restart.
Useful utilities when working are the commands
zip -r file.zip directory
,
which compresses the directory into the file, and
unzip file.zip,
which uncompresses the directory.
Caution: if we are in folder a/ and we execute unzip b/file.zip, the content of file.zip will be extracted in our working directory a/!