Entries Tagged 'python' ↓

how to setup pinax with nginx

Tonight I spent sometime getting PinaxDjango swiss army knife – to work on nginx via fastcgi on my Ubuntu 9.10 box. Here is a step by step guide.

First of course we need to install nginx, pretty easy with Ubuntu:

sudo apt-get install nginx

Create a new file called <appname> in /etc/nginx/sites-available/ like the following:

upstream djangoserv {
    server 127.0.0.1:8801;
}

server {
    listen   80;
    server_name  alkemic;

    access_log  /var/log/nginx/appname.access.log;

    location ^~ /site_media/  {
        alias /home/nick/dev/projects/pinax07/appname/site_media/;
        autoindex on;
    }
    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)   {
        #access_log   off;
        expires      30d;
    }

    location / {
        # host and port to fastcgi server
        fastcgi_pass 127.0.0.1:8801;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param SERVER_NAME $server_name;
        fastcgi_param SERVER_PORT $server_port;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_pass_header Authorization;
        fastcgi_intercept_errors off;
    }
}

Link it to sites-enabled so that we know it’s going to be started up (meanwhile you want to remove the default one):

sudo ln -s /etc/nginx/sites-available/appname /etc/nginx/sites-enabled/

Startup nginx with:

sudo /etc/init.d/nginx start

Activate your pinax installation (change the path to where you have installed your Pinax virtual environment):

source /home/nick/dev/projects/pinax07/bin/activate

Install flup (required to have manage.py run as fastcgi) in your virtual env:

pip install flup

Symlink all the static assets into the site_media folder:

css -> /home/nick/dev/projects/pinax07/appname/media/css/
img -> /home/nick/dev/projects/pinax07/appname/media/img
js -> /home/nick/dev/projects/pinax07/appname/media/js
pinax -> ../../lib/python2.6/site-packages/pinax/media/default/pinax/
snd -> /home/nick/dev/projects/pinax07/appname/media/snd
swf -> /home/nick/dev/projects/pinax07/appname/media/swf

Launch the python fastcgi process:

python ./manage.py runfcgi method=threaded host=127.0.0.1 port=8801

Or:

python ./manage.py runfcgi method=prefork host=127.0.0.1 port=8801

Note that you might need to move your development environment to use a proper db like MySQL because otherwise the app will have problems finding your sqlite db file.

You should have now your app running on the blazing fast nginx.

how to integrate compass into your default pinax project

Lately I’ve been really impressed and experimented joyfully with
Compass and Sass. They make css/layout design fun again for me. Even if the whole tool chain is in Ruby you don’t have to touch any Ruby to use them – don’t misunderstand me: I like Ruby and I’ve been a Rails early adopter; my brain is very well tuned in with Python, that’s all -.

Compass makes the creation of a semantic layout using any of the
popular css frameworks (blueprint, 960, etc.) a breeze.

There’s ton of documentation around, for example this is what got me into it a while ago.

So today after a furious coding spree I achieved what I wanted. I integrated Compass into a default Pinax project.

To prove that I could quickly change the layout in an elegant way I tried to alter the default theme to have the menus vertically aligned instead than horizontally.

I know people love pretty pictures and screenshots so here is a taste of the end result:

default theme of pinax with vertical menus

And another one:

vertical menu in pinax theme

And if you find any of this interesting be my guest and peruse the newly created github project:

  • The master just plugs in Compass without changing anything in the default theme. You can alter the layout by working in the media/sass folder and running compass -u in the media folder.
  • The vertical branch includes the changes needed to come up with the vertical menu you see above. By looking into the media/sass/base.sass file you should see how to use Compass directives to include a 960 grid into Pinax.

Please note, this is nothing fancy nor hard, but I expect this integration to be a life/time saver in all my next projects.

a webapp in 1 week (part 2): the source of data

In the first installment of this series I’ve spoken about a set of technologies that can speed up the time to market quite a bit. Read about it here if you missed it.

This time I want to talk about where to get the content for your niche web app – if you need a source of content, that is.

Leverage the Semantic Web

I apologize if you all know it and I am late to the news. But really, the semantic web is alive and here and I think one of its most promising incarnations is Freebase.

What is Freebase?

Take the whole of Wikipedia, in it’s unstructured – well I should say semi-structured – mass of data, structure it properly creating schemas on schemas of semantic relations amongst articles, and expose a clean API on top of it. Keep the openness part, allow anyone to create new domains (called bases) and to contribute structured content by hand or programmatically. That is freebase as I understand it.  Beautiful.

How did I use it

So the secret is out, that’s exactly what I used to present cool thumbnails and basic movie data for my tiny tuttivisti – now at the second week of life (check it out by the way, the new release is out).

Let me go into deeper depth here because I realize the audience requires it.

The Freebase folks have extensive documentation on how to interact with their service. In addition to that they publish two very nice Python libraries, freebase-suggest and freebase-python. The first to add a cool, jquery based autocompletion dropdown to your forms, the second to query Freebase programmatically from a Python program.

freebase-suggest example

Using the freebase-suggest library is very easy. To achieve this:

I just had to add this jquery snippet to the bottom of the page:

<script type="text/javascript">
var options = {
soft: false,
ac_param: {
type: '/film/film',
category: 'instance',
disamb: '1',
limit: '10'
}
};

$('#searchbox')
.freebaseSuggest(options)
.bind('fb-select', function(e, data) {
$('#searchbox').val(data.name);
$('#freebase_id').val(data.id);
$('#freebase_image_id').val(data.image.id);
$('#search_movie_form').submit();
return false;
});
</script>

And mark the text input of the search with id=”searchbox”.

The freebase-python library is also very easy to use and I encourage you to take a look at the documentation, here.

So in conclusion, before your app gains traction and your content is produced by your users, leverage the Semantic Web if you can, use freebase!

Tools for rapid prototyping: a webapp in 1 week

First things first.  My new webapp tuttivisti is live!

This post is about how it came to be. In just one week. One person team. Crazy working hours.

So I was there immediately after receiving the news that YC did not select my team for this round. I thought I would feel depressed. I thought that the put down would ruin my mood.

Well it didn’t! I actually felt a jolt of energy. For some reason I felt a bit as if I had been freed. Can’t really explain why but my motivation and excitement bounced back up.

So what did I do with all this creative energy? Well my partner for diffract.me (the other project I am preparing to launch) had to go for a few days back home and we hadn’t planned our next iteration yet. So I decided to dive into something new, short and sweet.

Yep, I started coding. Again. From scratch.

I sat on the shoulders of giants. For this 1 week challenge I picked some of the most productive tools I know (and these will be topic for a future post):

I’d love to communicate properly the productivity boost that the set of frameworks above can give a developer.

No more spending endless hours trying to create a decent CSS/HTML layout. Bluetrip (and by saying Bluetrip I mean also the great Blueprint and Tripoli from which Bluetrip has been created) makes it trivial to put the stuff exactly where you want it on the screen. Cross-browser. No hassle.

Pinax. Ah, the joy. No more coding for the nth time a registration module. No messing with authentication emails. No rewriting another openid library. No more. It’s all there. Ready to use. Greatly structured. Amazing stuff. This is a game changer in my opinion.

Jquery. Well it needs no introduction. Jquery is a work of genius. For me it changed coding in Javascript from a painful experience into a pleasurable one. I love Javascript again.

Now will tuttivisti fly? I sure hope so, but the truth is that it does not matter. What I am saying to myself is: If I can build something like this in 1 week the question is not “will I ever manage to build a webapp that gains some traction?” but “How many attempts will it take? Is 5 going to be enough? 10?”. Maybe. :D

I have a lot more to say but will cut it short. I am at the end of a very very long and demanding coding spree.

Update: The past weekend a new version of tuttivisti has gone live, my 2nd week of work on the project, which incorporated much of the feedback I received, but a lot of work is still needed. So feedback is welcome, and – yes I know – I need a graphic designer.

my emacs setup for python development

Recently I tried to give some other Python editors/IDEs a chance. I tried pydev for a while and found it good enough, but not perfect.

Truth is I have been using emacs for a long time for development. And I found myself quite comfortable to develop Python code with it. With a proper shell on the side, be it ipython or pycrust I thought my setup was good enough. And in all honesty it was good enough. Oh and I have to say I am one of those maniacs that use the brilliant, almighty, slightly blaspheme viper. The vi mode for Emacs. Ah the power!

Then recently I read this article and I realized that there might be things I was missing out. ECB, rope, ropemacs. What a beautiful discovery. So armed with a bit of patience I gave it my best shot and I managed to get it all working.

The article above is pretty explanatory but in addition to the things noted there I also added ECB and viper-mode. I had to tweak rope so that it was able to auto-complete the Python code of the Google app engine. To do that I had to add the root folder of the Google app engine code to the pythonpath property in the .ropeproject/config.py of my project.

The result is neat, very neat. Don’t believe me? Marvel at this beauty!

emacs screenshot for python development

The thing you can’t see in this screen-shot is that this setup has total auto-completion for the full Python library; re-factoring ability, instant inline documentation and much more. I have to say I am excited :D .