Monday, September 2, 2013

Django, Dajax, and Getting Dajax to work (Lite-Em-Up Component Organizer)

Django

After futzing around with all sorts of database application approaches, I finally settled with Django. Here's what I messed with:
  1. Pure sqlite implementation with python application for interface
  2. MS Access Database
  3. Finally, Django
The pure approach at the top is probably what I'd end up going for if I ever wanted to mass produce this, MS Access was painful and nearly useless (it was a dark time in my life). Django has offered the easiest database creation/ application development framework of all three. So it was very attractive for my "just get it done" state of mind. Not only that but most of the interface for working on the database contents has already been made for you (in the admin site). The only thing left for me was figuring out how I was going to use the nice interface to make a box light up... a button perhaps? A button that could run a python script. The tricky part is how do you wire up the button which is in a webpage that Django serves, to run your dead simple python script? And how do you pass the script the number of the box to light up?

The answer is of course, Dajax. Not only will dajax serve me well for this project, but also for all the other projects I have going at work to create database systems for control component management and planning.

Dajax

Starting from here, I was able to gather that this dajax thing was pretty powerful, and handy. But the example leaves a lot out. I tried going through the setup documentation, but it is also operating from the assumption that you pretty much know what you're doing... Not only that, but there are almost no very basic tutorials for the current (0.9 dajax, 1.5 django) releases. So that's what I am going to write here, somewhat for you, and mostly for me.

How to get a button on a django webpage to execute a python script using dajax:

This tutorial assumes that you already have Django 1.5 setup. I do not claim to be any sort of an expert, my training was in mechatronics and physics, so getting Django to work for me has been an exercise in hacking all the way. That being said, here we go!

Dajax Setup

First install dajax and dajaxice using pip.
 sudo pip install django_dajax  

Then follow these instructions to add dajaxice to your project (required!). Some points for clarification: the urls.py to add your dajaxice url to is for the site (not the app!). You probably could have figured that out, but just to save you some questioning.

Finished? Great! Now follow these instructions to install dajax. For clarification this is what I did for the jquery step ( I was confused because I thought you simply add this line to your head:
 {% static "/static/dajax/jquery.dajax.core.js" %}  
 but you need to embed it sensibly like so:
 <script type="text/javascript" src="{% static "/static/dajax/jquery.dajax.core.js" %}"></scrip>  
t>

Also make sure you load the staticfiles lib into the template (place this at the top of the template):
 {% load staticfiles %}  

The meat

Here's my template page all together:
 {% load dajaxice_templatetags %}  
 {% load staticfiles %}  
 <head>  
 <script type="text/javascript" src="{% static "/static/dajax/jquery.dajax.core.js" %}"></script>  
 <script type="text/javascript" src="{% static "/static/eci/jquery-2.0.3.js" %}"></script>  
 {% dajaxice_js_import %}  
 </head>  
 <button onclick="Dajaxice.eci.light_up_box(Dajax.process);">Show me where</button>  

A few things to note:

  • I downloaded the newest jquery from the jquery website here, and placed it in the app folder under static/eci/ where eci is the name of my django app.
  • The onclick call goes as Dajaxice.appname.function in the ajax.py.
Here's my ajax.py code which resides in my app folder (called eci in my case, for electronics component inventory):

 from dajax.core import Dajax  
 from dajaxice.decorators import dajaxice_register  
 import serial, time  
 @dajaxice_register  
 def light_up_box(req):  
   dajax = Dajax()  
   dajax.alert("Hello World!")  
   serial_port = serial.Serial('/dev/ttyUSB0', 9600, timeout=None)  
   box_number = 4  
   number = str(box_number)  
   if box_number <10:  
     number = '0'+number+'X'  
   else:  
     number = number+'X'  
   serial_port.write(number.strip())  
   serial_port.close()  
   return dajax.json()  

I use the dajax.alert function to confirm independently that the dajax function was called. I'll probably use it later to confirm that the box was found and the part removed (ask for a quantity or something).

That's the long and short of it. I hope it helps you. I was really sad when I couldn't find a basic tutorial that explained the stupid questions I had, hopefully you don't have the same problem!

Wrap up

So as of last night the electronics component organizer has been improved a bit. I have a larger current power supply connected, which will hopefully help with the power issues I've been having. I have also finally secured my pcbs and power supply to the top of the box so it's not so janky. Also, now that I have this dajax thing working I can finally start putting together the rest of the database viewing pages that will allow me to light up boxes at will.

Thoughts for the future:

I think I might have made a mistake trying to get my computer to stream the FFT data to the arduino over serial. I may end up making a new pcb that has an audio jack pass through so that the FFT can be done directly on the arduino. Once the basic database is done I'll probably go ahead and populate the organizer with parts. That will probably take a good part of a Saturday.