Installing and Running Django Tutorial on Anaconda Python

So I want to do some more fluffy web based stuff with python, and, ultimately, mongodb. I’m doing things properly and starting with the Django tutorial. I’ve already installed Anaconda to provide me with a  full python 2.7 and the key data analysis packages pre-installed. The Anaconda windows installer works perfectly.

An aside, but the ipython notebook is brilliant. Runs a local webserver on your machine with the python command line running. You can record everything you do, download copies and send to others, inline graph generation, etc. It’s lovely.

Fire up a DOS command line.

So I started by checking to see if Django was already installed:

python -c "import django; print(django.get_version())"

It wasn’t. easy_install and pip often don’t work directly from the command line, with either python 2.7 installed manually  or with anaconda, so I went for the safe option that seems to work everytime and:

where python
C:\Users\me\AppData\Local\Continuum\Anaconda\python.exe
cd C:\Users\me\AppData\Local\Continuum\Anaconda\Scripts
pip install Django

That installed Django for me fine. I checked again to see what version was installed:

python -c "import django; print(django.get_version())"
1.6.2

So now I need the Django Tutorial for this version. Note that if your version of Django/python is different to mine, you will need to select the right Documentation version. You can do that with the control atthe bottom RHS of the tutorial page.

Next, it wants me to create the project. I have to cd to my source folder and run a command. That command blows up:

C:\me\projects\reporting\incubator>django-admin.py startproject mysite
Traceback (most recent call last):
File "C:\Users\me\AppData\Local\Continuum\Anaconda\Scripts\django-admin.py", line 2, in <module>
from django.core import management
ImportError: No module named django.core

Sad Face. But let’s work around those pesky path issues like we did with pip:

C:\me\projects\reporting\incubator>python C:\Users\me\AppData\Local\Continuum\Anaconda\Scripts\django-admin.py startproject mysite

Success. Happy Face. Mental note made. Continuing with the tutorial.

I didn’t have the sqlite3 shell installed, so downloaded it from sqlite.org.

 

Serving HTTPS out of Bottle using CherryPy

I wanted to bash a couple of ideas together and see how quickly I could get something serving data from mongodb over https. I’d just started messing with python and the pymongo driver and I was (and still am) loving the quick hit python coding gives. Adding bottle as a framework made it all webby and useful feeling into the bargain :-). What is not not to like?

What I really wanted was a sweet single point solution that bottle already gave me, but with https. Past self takes over at this point….

After a bunch of digging around and reading lots of blurb, the upshot is a lightweight https server called CherryPy using bottle querying a mongodb using a regex and outputting whatever is in the first result to the screen, with a list of some of the other results, allowing the user to refine their search. The app is mickey mouse, and you do have a post where I’d prefer a redirect and using a cookie (so that browser refreshes work nicely), but the amount of data you can potentially return using the re would blow out the cookie size pretty quickly. There’s a probably a better way(tm) 😉

To do a test run you will need:

  • mongod running
  • If you want it not to throw a wobbler (I haven’t put much exception handling in it), you’ll need at least one row in testbottle.trades collection with at least one attribute of ‘Trade_Id’.
    So create a test db and record or two to play with. Start mongo shell and add some:

    use testbottle
    
    //bung one in with an easy one match re to remember
    db.trades.insert(
    	{
    		"Trade_Id" : "000457390G-1",
    		"Version_Number" : 1,
    		"Notes" : "",
    		"Settlement_Consideration" : 1721.72
    	}
    )
    
    //let us bung a small shedload in starting with x
    for (var i = 0; i < 1000; i+=1) { 
    	db.trades.insert(
    		{
    		"Trade_Id" : "x" + i, 
    		Version_Number : i, 
    		Settlement_Consideration : Math.random() * i * 100
    		}
    	)
    };
    			
    		
  • python installed
  • pip install pymongo – OR if on windows – http://pypi.python.org/pypi/pymongo use the windows msi
  • pip install bottle
  • pip install cherrypy
  • pip install pyOpenSSL – OR if on windows – http://pypi.python.org/pypi/pyOpenSSL use the windows msi
  • Create a top level folder for this project. I used pyMongoBottlePoc
  • create your own ssl cert –
    openssl req -new -x509 -keyout testserver.pem -out testserver.pem -days 365 -nodes

    Put this in your project top level folder.

  • The find_trade.py code is here. Put this in your project top level directory
  • The hello page and show found pages are here. Put these in “views” subdirectory
  • Run it by typing at the command line
    python find_trade.py
  • Use it by pointing your web browser at
    https://localhost:8082/

find_trade.py is a webserver on your localhost port 8082 which initially shows a welcome page hello_get_value.tpl asking you to input a trade id. When you click submit, find_trades.py does a regex search in the mongo database returns a cursor (trades) and the first result (trade). Bottle renders them using show_trade.tpl.

I leaned really heavily on this post from Diego XA and on the 10Gen Dev course youtube videos week 1 which have an excellent intro to python course in them. Thanks guys.

Installing Python, pyMongo, and Bottle on Windows

I had no problem installing all of the above on my Mac. On Windows, however, it was a bit of a pain. Thinking about it, it maybe that the mac install was Python 2.7, which everyone has been there and done that, whereas the Windows install was python 3.3, which may be a little later to the party.

Anyway, what I did:

  1. Install python 3.3 from http://www.python.org/download/releases/ Use the msi installer
  2. Install the pymongo using the msi from http://pypi.python.org/pypi/pymongo/ > Look down the bottom to find the windows installers. I suggest you don’t do an easy_install, it won’t compile properly and will either not work at all, or will have degraded performance.
  3. Install distribute. The instructions are here. I found it easiest to install from cygwin/git-bash using:
     $ curl -O http://python-distribute.org/distribute_setup.py
    $ python distribute_setup.py
  4. Install pip using easy_install supplied by distribute. NB: you must do this from the DOS command line (and, it appears, be in the Scripts folder):
     C:\> cd c:\Python33\Scripts\
    C:\> easy_install pip
  5. I also installed bottle at this point.
     C:\> pip install bottle

Many experienced python developers are recommending using virtualenv. It allows you have a virtual python install where you can pip install anything without blowing up your base python install by accident. You may have to use the performance retarded pymongo as Windows 7 (32-bit) doesn’t have the c libraries needed to build the necessary modules.

Hat tip to khattam for the distribute/easy_install usage.