Using Cairo to generate SVG in Django

Cairo is a 2D vector graphics api used by Firefox, Gtk and other desktop projects.

I’m going to show here that it can also be used to generate web content, using Django.

I’m going to port two examples from the Michael Urmans Cairo Tutorial for PyGTK Programmers.

To understand the cairo and it’s drawing model I’d recommend his his Cairo Tutorial for Python Programmers.

Note: In this example I’ll be generating SVGs…  as I.E. (as of 2010) does not support them, you might want to generate PNG or PDF – if you need to do this with cairo, look for one of the many cairo tutorials on the web.

The example django project can be downloaded at the end of the article.

Read more

Scripts to help workflow now on google code.

I’ve uploaded my scripts to help workflow to google code, naming the project batch flow.

Heres a summary of some of the things you can do.

Clipboard integration

Go to a directory in the clipboard.

If you have the address bar enabled in explorer:

Copy the location, go to the prompt and enter ‘pcd’ to go to that folder

Go to the location of a setting in netbeans or eclipse:

Copy the location , go to the prompt and enter ‘pcd’ to go to the folder (or folder containing the file).

Get the current directory without dragging:

Enter ‘ccd’

Directory bookmarks

dirsave and dirload let you save named bookmarks.

Using hotkeys

If you use the alternate shell 4nt (or the free TCC/LE) you can use the supplied configuration and aliases to access the bookmark functionality from the keyboard:

In the 4nt or tcc/le prompt enter “option”, under the “TCStart/TCExit” path, change the location to the location where batch-flow is installed + “\conf”, for instance on my computer batch-flow is installed to c:\usr\batch-flow, so I set it to

c:\usr\batch-flow\conf

Now in new TCC/LE prompts F5-F10 are reserved for directories: Ctrl+Fkey to save, and Alt+Fkey to load.  Alt-F12 lists these shortcuts.

Note:  Alt-F12 only lists shortcuts on FKeys, to list these and other shortcuts enter dirload /l

batch-flow comes with other handy hotkeys, use ‘alias’ in TCC/LE to see what they are.

Path manipulation

addpath

It’s annoying after installing a program to have to add it to the path, so there is an ‘addpath’ command to do this.

regpath

This is a more general utility for viewing the registry path, you can list it, validate it, check for the location of files within it.

Also useful is ‘regpath /L’ which sets the local prompts path to the one in the registry.

Further help

Most of the commands have help builtin, which you can access by using the /? option.

Access files in a Linux virtual machine from Windows

I recently found a neat way of accessing files in a linux VMWare image.   This is really useful, as theres never really a good time to break your VMWare image, this is also handy if you don’t want to run the whole machine, but just access the files inside.

There is one caveat:

  • It only works if the filesystem is ext2 (ext3 works, and ext4 probably works).

Install VMWare DiskMount Utility

Accept the EULA, download and install the VMWare DiskMount utility.

For convenience add the utilities folder to the path:

C:\Program Files\VMware\VMware DiskMount Utility

Do this through the Windows Gui, or even use my addpath utility.

At this point you can mount Windows VMWare images.

The usage is:  vmware-mount drive-letter vmdk-image.

Heres how I mount my Ubuntu image to the j: drive

[C:\vmware\Ubuntu]vmware-mount j: ubuntu.vmdk
[C:\vmware\Ubuntu]

No output here indicates success.

At this point everything seems fine, but a crucial piece of the puzzle is still missing; try and view the files and you still can’t:

Failing to see files in an ext2 VMWare image

The next step is to make Windows understand the ext2 filesystem, using a special driver.

Install ext2ifs

Grab ext2ifs from www.fs-driver.org and install.

If the following steps don’t work then you may need to reboot.

Thats it!

You should be now able to access files inside your VMWare image (assuming it’s ext2 and not reiserfs), remount the image and have a go:

In my case I did:

[C:\vmware\Ubuntu]vmware-mount j: ubuntu.vmdk
[C:\vmware\Ubuntu] dir j:

Heres the output – hooray, I can copy my work out of the image !

Viewing files inside a VMWare image with ext2fs

This is very useful, especially if you do dist-upgrade in ubuntu and can’t access the network.

Bonus tip:

Newer versions of VMWare player let you install VMWare tools from inside the GUI, this is another way to fix the kind of catestrophic problems you can cause yourself by accidentally upgrading the kernel in an image.

Some useful scripts for windows.

Stoyan of PHPIED.com has found the joy using javascript for scripting in the OS

I’ve put up a few scripts I find useful in windows (download at the end of the article):

  • addpath.js – Add a path to the registry path.
  • dt.cmd – Change to desktop folder.
  • e.cmd – Open explorer in current or specified folder.
  • regpath.js  – Output the path stored in the registry.
  • updateenvironment.js – Updates running apps with any changed settings in the registry.

And a couple that use python and pywin32:

  • ccwd.py – Copy the current working directory to the clipboard
  • cpath.py – Copy the current path to the clipboard

All of have acompanying batch files to run them, I generally have everything in a folder c:\usr\cmd, but they should work from anywhere in the path.

Download  cmd.zip

Using Java2Python to port a JavaCairo tutorial

I recently came across Java2Python.  As I’m interested in Cairo I thought it would be interesting to try porting one of the example tutorials from ZetCode.

I’ll run through the steps involved in porting then try and reach some conclusions at the end :) .

1. Get setup

This is easiest in Linux, I’m running Ubuntu (in vmware), and installed

antlr 2.x
python2.5.x
sun java6
pygtk
java-gnome

You can install them like this:

# sudo apt-get install antlr python2.5 sun-java6-bin libjava-gnome-java

Then install Python2Java with easy_install

# sudo easy_install-2.5 java2python

To test if it’s working run j2py -i.  It should complain there is no file:

# j2py -i
Usage: j2py [options]

j2py: error: -i option requires an argument

If you get any other errors your missing some packages.

2. Get the Java Code from the Simple Example.

Save the ’simple.java’ example as ‘GSimple.java’

If java-gnome is running ok, compiling and running it you should see a window:

# javac GSimple.java
# java GSimple

gsimple

Now we’ll run through the code, it’s important to understand what it does before we port it…

Read more

The Bloggage and slacking

Just been checking out what Mr Palmer has been up to… quite a lot it seems…   if I could get further than 2 chapters in 6 months with Getting Things Done maybe I could stop procrastination and start GTD too :)

And I’ve been checking out some online comics…

My 6 monthlyish check on SayUncle comics blog always makes me want to do some drawing.

Also Quite enjoying the Beartato in space storyline in nedroid.

Toast

Lyrics

Router upgrade nixed xbmc

It turns out my lack of networked media consumption utopia via xbmc was all down to upgrading my router… the xbox was fixed on 10.0.0.x while everything else moved over to 192.168.1.x  …so I learned that SMB doesn’t like that at all.

Oh well, one task down… many more todo… maybe I can actually setup my kurobox in the next few days and then some really nice things might be possible.

Flex for developers – Bootstrapping

A lot of information on the web is aimed at people who use flashdevelop and other guis to build flex apps, this article is aimed at developers.  I’ll give you the information to get started with flex development quickly, using the free sdk and building with ant from the commandline.

A passing knowledge of ant wouldn’t hurt either.

This tutorial uses the file blankapp.zip to help bootstrap you into the world of flex / actionscript development.

Prerequisites

Flex SDK

You will need the flex sdk available from the flex developer center

Apache ant

You will need apache ant, make sure that the bin folder is in your path.

Copy flexTasks.jar from the flex ant lib into your own ant lib folder.

Setup

Extract the blankapp.zip to a folder

Open build.xml and change the line <property name=”FLEX_HOME” value=”/usr/flex/sdk3″ /> to point to the flex sdk.

Build

You can build it by typing

ant build

You should see some output like this

Buildfile: build.xml

clean:
   [delete] Deleting directory C:\Documents and Settings\Stuart\Desktop\projects\flex\blankapp\build

init:
    [mkdir] Created dir: C:\Documents and Settings\Stuart\Desktop\projects\flex\blankapp\build

build:
    [mxmlc] Loading configuration file C:\usr\flex\sdk3\frameworks\flex-config.xml
    [mxmlc] Initial setup: 172ms
    [mxmlc] Loaded 8 SWCs: 391ms
    [mxmlc] C:\Documents and Settings\Stuart\Desktop\projects\flex\blankapp\src\blankapp_333027.cache (The system cannot
 find the file specified)
    [mxmlc] Files: 328 Time: 1562ms
    [mxmlc] Linking... 16ms
    [mxmlc] Optimizing... 125ms
    [mxmlc] SWF Encoding... 47ms
    [mxmlc] C:\Documents and Settings\Stuart\Desktop\projects\flex\blankapp\build\blankapp.swf (159158 bytes)
    [mxmlc] Persisting 33 compilation units... 15ms
    [mxmlc] Total time: 2328ms
    [mxmlc] Peak memory usage: 59 MB (Heap: 33, Non-Heap: 26)

BUILD SUCCESSFUL
Total time: 2 seconds

A new file, blankapp.swf should now be in the build folder

Type

ant deploy

To copy it to the deploy directory (later you can customise this later).

If you open the blankapp.swf in the browser you should see something like this

It’s fairly bare, but demonstrates some basic techniques and a couple of widgets.

Mxml and Actionscript, how they link together

There are two important parts, the blankapp.mxml and org/blankapp/BlankApp.as.

The mxml file defines the layout while the actionscript contains code to be executed.   In this case there is one mxml file, BlankApp.mxml which links to a single actionscript class, BlankApp (in the file src/org/blankapp/BlankApp.as).

The directory structure org/blankapp, defines the package, in much the same way as java packages.

The graphic below shows how the class is linked to the mxml and where the instance of the class is:

The app is very basic, but should provide a jumping off point.

Adding libraries

If you need to add any libraries, just place the swc files in the lib folder and they will be included, the build.xml can be changed if this isn’t satisfactory.

Afterword

I only wanted to get started with papervision, but found it a bit of hassle to get started, hopefully this post will help people get started quickly.

Acknowledgements

The ant build file is by no means my own, owes inspiration to previous ant files I’ve known, including the pyAMF one and information available on the web.

This blog entry: http://talsma.tv/post.cfm/ant-mxmlc-and-swc-files for the info on how to include swc files.

Aggregated angst

A little python, a lot of angst…  for just 3 hours of work this was definitely worth it :)

Click the image to view it in wordle where you can mess with it.

I love the way that Im is the biggest word, you can really see how egotistical they are on grouphug.

(Cheers to Andy for turning me onto wordle and helping with ideas).