Thursday, April 16, 2009

Pygmentool

I have written my first Python web application, yay! The purpose of the application is to help people post highlighted code on their blogs using Pygments. I have written an article on this before, but it implied using Pygments from the command line, which is unpleasant for most people. Also, not all people have Python available (like, say, Windows users?) and don't feel like installing it just for this. That's why I have made this nice little application which can do basically two things:
  1. Give you a Pygments-compatible stylesheet, with the style of your choice;
  2. Format your code so you can simply copy-paste it on your blog.
For more, check out the how to page.

Developing the application was a real treat. Werkzeug is awesome through its simplicity (as you may or may not know, I'm not a fan of big frameworks that "do everything for you") and its efficiency. The docs are a bit hard to grasp at first, but after a short while you will know where to find what. What bugged me the most was the tutorial which I found a bit backwards and I thought it emphasized way too much on the database part of the project (which has no relation whatsoever to Werkzeug). Also, it jumps from one thing to another, which makes it pretty hard to follow and, honestly, I wasn't able to read it through to the end (although I'm sure I will read it at some point, it looks like a great resource that teaches good coding habits, code organizing and SQLAlchemy usage!). I just picked up bits from other pages in the documentation. But, unlike the tutorial, Werkzeug is awesome. I like the fact that it works directly on the WSGI layer, so you can really understand how everything works. Also, it doesn't have hundreds of modules that do all sorts of things and in which you will get so lost. Basically, it gives you full control over everything.

Jinja2 is also pretty cool. It's actually the first template engine I use, but it's so straightforward, feature packed and terribly easy to use that it must be one of the best!

Pygments, well, what can I say. This project would have been pretty much impossible without it.

I have decided to make the source code of the application available. I've done this for a couple of reasons. For one, it may be a good starting point for those trying to learn Python + Werkzeug. The second reason is for people with more experience to be able to criticize my coding and maybe give some advice ;). [pygmentool.tar.gz]

The fun thing is, all three big modules I used in this (Werkzeug, Jinja2 and Pygments) are developed by the Pocoo team. They're pretty pythastic :D.

I still have a lot to learn. I have no idea how SQLAlchemy (which seems to be what people use these days in web apps and not only) works. Also, I have no idea how to use sessions in a Python web application (and they're critical in bigger projects), but I have a hunch I would have to use flup, so I'll have to study that, too. But this was a nice appetizer :).

6 comments:

  1. I love pygments too and about blog stuff I discovered lastly an another great tool to create easily consitent posts: hyde that is based on jekyl the tool used on git to process pages.

    Take a look it might interest you! Personnally i like a lot the idea and think about migrating my wordpress blog.

    (It would have been welcome to have a link here I know but I can't paste it and am too lazy to write it entirely)

    ReplyDelete
  2. Hey buddy they link to the program is broken...

    You know where else I can get it?

    Thanks.

    ReplyDelete
  3. It should be back up now. Sorry about that.

    ReplyDelete
  4. Hi,
    The tool is pretty cool, but it would be excellent if it also rendered the syntax-highlighted text below the big text field, so you could see how it looked before cutting it out and pasting it somewhere.

    regards,
    christopher

    ReplyDelete
  5. Hi, I am testing the tool, but somehow, the text will all appear in one line. I have to add
    manually. Anyone has the same problem?

    Regards,
    kc

    ReplyDelete
  6. You must have a "white-space: pre" style applied to your code container.

    ReplyDelete