Activate your free membership today | Log-in

Thursday, August 13th, 2009

Skulpt: Crazy project that lets your browser parse and run Python

Category: Python

Skulpt is an entirely in-browser implementation of Python.

Really. Crazy and cool.

You can build an example term like this just via:

JAVASCRIPT:
  1.  
  2. window.addEvent('domready', function() {
  3.     var term = initTerminal(80, 20);
  4.  
  5.     // set up demo typers
  6.     $('codeexample1').addEvent('click', (function(e)
  7.             {
  8.                 e.stop();
  9.                 term.doClear();
  10.                 term.writeStr("print \"Hello, World!\"     # natch", false, true);
  11.                 term.doEnter();
  12.             }));
  13.     $('codeexample2').addEvent('click', (function(e)
  14.             {
  15.                 e.stop();
  16.                 term.doClear();
  17.                 term.writeStr("for i in range(5):", false, true); term.doEnter();
  18.                 term.writeStr("    print i", false, true); term.doEnter();
  19.                 term.doEnter();
  20.             }));
  21.     $('codeexample3').addEvent('click', (function(e)
  22.             {
  23.                 e.stop();
  24.                 term.doClear();
  25.                 term.writeStr("[x*x for x in range(20) if x % 2 == 0]", false, true);
  26.                 term.doEnter();
  27.             }));
  28.  
  29.     term.writeStr("Skulpt demo REPL - " + new Date().toLocaleString() + "\n");
  30.     term.writeStr(term.PS1, true);
  31. });
  32.  

Posted by Dion Almaer at 6:59 am
23 Comments

++++-
4.6 rating from 32 votes

Tuesday, May 26th, 2009

CleverCSS: Rich Python-like DSL for CSS

Category: CSS, Python

We have mentioned Sass and other CSS abstraction libraries before, but somehow CleverCSS slipped by.

The nesting DSL looks similar to other solutions:

CSS:
  1.  
  2. ul#comments, ol#comments:
  3.   margin: 0
  4.   padding: 0
  5.  
  6.   li:
  7.     padding: 0.4em
  8.     margin: 0.8em 0 0.8em
  9.     h3:
  10.       font-size: 1.2em
  11.     p:
  12.       padding: 0.3em
  13.     p.meta:
  14.       text-align: right
  15.       color: #ddd
  16.  

But, you can also use attributes:

CSS:
  1.  
  2. #main p:
  3.   font->
  4.     family: Verdana, sans-serif
  5.     size: 1.1em
  6.     style: italic
  7.  

Define constants:

CSS:
  1.  
  2. background_color = #ccc
  3.  
  4. #main:
  5.   background-color: $background_color
  6.  

Implicit concatenation:

CSS:
  1.  
  2. padding: $foo + 2 + 3 $foo - 2
  3.  
  4. // returns: padding: 15 8; if $foo is 10
  5.  

Calculations:

CSS:
  1.  
  2. // calculations with numbers / values
  3. 42px + 2                    -> 44px
  4. 10px * 2                    -> 20px
  5. 1cm + 1mm                   -> 11mm
  6. (1 + 2) * 3                 -> 9
  7.  
  8. // string concatenation
  9. foo + bar                   -> foobar
  10. "blub blah" + "baz"         -> 'blub blahbaz'
  11.  
  12. // You can also calculate with numbers:
  13. #fff - #ccc                 -> #333333
  14. cornflowerblue - coral      -> #00169d
  15.  
  16. // You can also add or subtract a number from it and it will do so for all three channels (red, green, blue):
  17. crimson - 20                -> #c80028
  18.  

Nice :)

Posted by Dion Almaer at 7:19 am
24 Comments

++---
2.9 rating from 38 votes

Monday, December 8th, 2008

pyQuery: Same API, but with a snake.

Category: Python

jQuery came first, but then we have seen others such as pQuery.

Now we have pyQuery which brings the same great taste API to Python. The author Olivier Lauzanne explains:

yquery allows you to make jquery queries on xml documents. The API is as much as possible the similar to jquery. pyquery uses lxml for fast xml and html manipulation.

This is not (or at least not yet) a library to produce or interact with javascript code. I just liked the jquery API and I missed it in python so I told myself "Hey let's make jquery in python". This is the result.

It can be used for many purposes, one idea that I might try in the future is to use it for templating with pure http templates that you modify using pyquery.

You can use the PyQuery class to load an xml document from a string, a lxml document, from a file or from an url:

PYTHON:
  1.  
  2. >>> from pyquery import PyQuery
  3. >>> from lxml import etree
  4. >>> d = PyQuery("<html></html>")
  5. >>> d = PyQuery(etree.fromstring("<html></html>"))
  6. >>> d = PyQuery(url='http://google.com/')
  7. >>> d = PyQuery(filename=path_to_html_file)
  8.  

We also saw new posts on pyjamas, the JS emitter (a la GWT for Java) that we covered awhile back.

Add to that the fantastic week that the pythonistas have had as they laud over us with Python 3000 having a final release (which will kick in updates to libraries that are needed to make this backwards not so compatible upgrade viable). It gives a kick in the backside to Perl, Ruby, Java, and even us JS folk :)

Posted by Dion Almaer at 7:45 am
3 Comments

+++--
3 rating from 29 votes

Tuesday, October 7th, 2008

Pyjamas: GWT for Python

Category: GWT, Python

Last week, we posted a story about deploying GWT to PHP back-ends. We've got another GWT-ish post this week: Pyjamas, a sort of GWT for Python.

The SourceForge project page has a brief overview and pointers to a tutorial, a widget showcase, and more.

Posted by Ben Galbraith at 8:00 am
2 Comments

+++--
3.9 rating from 20 votes

Friday, August 8th, 2008

JSBridge: Powering Mozilla with Python

Category: Python

JSBridge is an incredibly alpha, but interesting new project, lead by Mikeal Rogers, that bridges Python and JavaScript with respect to Mozilla. It uses mozrunner, the Python library that can power Mozilla applications (e.g. Firefox).

Once you fire up jsbridge MozRepl will kick into gear, and you will be able to interact across the bridge. "This includes JavaScript < -> Python object translations and a callback mechanism for Python responses to custom events fired in the javascript environment."

Of course, this isn't related to IronMonkey the project that allows you to write Python (and Ruby and ...) on top of Tamarin.

Posted by Dion Almaer at 7:22 am
3 Comments

+++--
3.5 rating from 11 votes

Monday, August 4th, 2008

Django and ExtJS Grids with Filters

Category: Ext, Python

Are you an ExtJS and Django user? If so, you will want to check out this article by Matt of Tangible Worldwide on Using ExtJS's Grid Filtering with Django.

He goes into detail on how to tweak the grid filtering system that is aimed at PHP, and getting it to work in a way that allows you to write this Django code:

PYTHON:
  1.  
  2. # take a ContentType model name (say, from the URL)
  3. # and create a QuerySet
  4. ctype = ContentType.objects.get(model=ctype_model)
  5. model_class = ctype.model_class()
  6. obj_qs = model_class.objects.filter()
  7.  
  8. # 'q' is the set of POSTed filtering parameters
  9. filter_params = request.POST.get('q', '[]')
  10.  
  11. # decode the filtering parameters w/ simplejson
  12. filter_params = simplejson.loads(filter_params)
  13.  
  14. # apply the filtering params
  15. filtered_qs = get_queryset_from_ext_filters(obj_qs, filter_params)
  16.  

ExtJS 2.1 saw inclusion of the popular (at least in my world) user extension for AJAX filtering of data by grid columns. As useful as this is (provided you can abide by the GPL or paid licensing options), the filter parameters serialized as a PHP array, which is not particularly useful for other languages. With a quick modification to the grid filtering serialization method and a helper function for Django, we can rewire the system to send a JSON string that Django can use to directly apply filtering to a QuerySet. I've found that most clients love having this sort of command and flexibility over their data -- especially the kind that don't necessarily realize that this visualization and control exists. I like that. Additionally, the amount of work and code required is minimal, and the code both reusable and straight-forward. I like that, too.

There are 4 steps to this process:

  1. Modify the serialization method in GridFilters.js
  2. Set up the helper function that will process the QuerySet with the grid filter parameters
  3. Slightly customize your models (see below)
  4. Add (simple) helper code to your Ajax view

Posted by Dion Almaer at 10:38 am
Comment here

++++-
4.2 rating from 66 votes

Thursday, July 31st, 2008

JSON Pickle: Serialize your complex Python objects to JSON

Category: JSON, Python

John Paulett wanted to be able to define complex Python model objects, then seamlessly pass them into CouchDB and to client-side Javascript.

To make this happen for objects that are beyond primitive sets he created JSON Pickle which has been used on the Universal Feed Parser, and lets you do the following:

PYTHON:
  1.  
  2. >>> import jsonpickle
  3. >>> from jsonpickle.tests.classes import Thing
  4.  
  5. # Create an object.
  6. >>> obj = Thing('A String')
  7. >>> print obj.name
  8. A String
  9.  
  10. # Use jsonpickle to transform the object into a JSON string.
  11. >>> pickled = jsonpickle.dumps(obj)
  12. >>> print pickled
  13. {"child": null, "classname__": "Thing", "name": "A String", "classmodule__": "jsonpickle.tests.classes"}
  14.  
  15. # Use jsonpickle to recreate a Python object from a JSON string
  16. >>> unpickled = jsonpickle.loads(pickled)
  17. >>> print unpickled.name
  18. A String
  19.  

Posted by Dion Almaer at 7:50 am
1 Comment

+++--
3.5 rating from 22 votes

Monday, July 28th, 2008

Dojango: Two great tastes in one can!

Category: Dojo, Framework, Python

Dojango, a template Django application that includes full Dojo support baked in, has been released by Tobias Klipstein, Nikolai Onken, and Wolfram Kriesing.

  • It provides capabilities to easily switch between several Dojo versions and sources (e.g. aol, google, local)
  • Delivers helping utilities, that makes the development of rich internet applications in combination with dojo more comfortable.
  • It makes the building of your own packed Dojo release easier.
  • It includes a reusable django app that provides Dojo
  • It includes helper functions, i.e. JSON conversion

Posted by Dion Almaer at 10:22 am
1 Comment

+++--
3.5 rating from 43 votes

Monday, July 7th, 2008

LLVM and running C as well as Python in the browser

Category: JavaScript, Python

Dan Morrill doesn't like JavaScript 2. His reasoning is a little like the folks who don't want Java.Next to try to copy features from every other language, but to just be the best static language, and let other languages like Scala, Groovy, JRuby (and the hundreds of others like Fan) go in different directions on the same Java platform.

You could argue the same for the browser platform. Why push JavaScript 2 further than cleaning it up, and instead allow other languages to augment it.

This is where technology such as IronMonkey come in, as well as the work that Scott Peterson is doing, written up here:

Scott Petersen from Adobe gave a talk at Mozilla on a toolchain he’s been creating—soon to be open-sourced—that allows C code to be targeted to the Tamarin virtual machine. Aside from being a really interesting piece of technology, I thought its implications for the web were pretty impressive.

If I followed his presentation right, Petersen’s toolchain works something like this:

  1. A special version of the GNU C Compiler—possibly llvm-gcc—compiles C code into instructions for the Low Level Virtual Machine.
  2. The LLVM instructions are converted into opcodes for a custom Virtual Machine that runs in ActionScript, a variant of ECMAScript and sibling of JavaScript.
  3. The ActionScript is automatically compiled into Tamarin bytecode by Adobe Flash, which may be further compiled into native machine language by Tamarin’s Just-in-Time (JIT) compiler.

The toolchain includes lots of other details, such as a custom POSIX system call API and a C multimedia library that provides access to Flash. And there’s some things that Petersen had to add to Tamarin, such as a native byte array that maps directly to RAM, thereby allowing the VM’s “emulation” of memory to have only a minor overhead over the real thing.

The end result is the ability to run a wide variety of existing C code in Flash at acceptable speeds. Petersen demonstrated a version of Quake running in a Flash app, as well as a C-based Nintendo emulator running Zelda; both were eminently playable, and included sound effects and music.

Posted by Dion Almaer at 8:50 am
7 Comments

++++-
4.3 rating from 17 votes

Friday, January 12th, 2007

Creating a Flex application using the TurboGears framework

Category: Adobe, Python, Screencast

Screencasts are a nice way to learn certain things. Adobe has done a fun one, and got their own James Ward, and the knowledgeable Bruce Eckel together to write some code.

The coding session is the pair of them building a sample app from scratch using TurboGears on the backend, and Flex on the front end.

It is fun to see Bruce using plain old vi for his python hacking, and James gets to go into uber-IDE land with the Flex Builder (built on Eclipse).


Posted by Dion Almaer at 7:31 am
1 Comment

+++--
3.9 rating from 26 votes

Monday, November 6th, 2006

Pyjamas: Pythons answer to GWT

Category: GWT, Python

If you are a python coder who doesn't like JavaScript and wishes she could stay in python land more, maybe pyjamas is the framework for you.

It is an early stage port of sorts of GWT.

Given that a lot of python goes on at Google, they may be interested in this themselves, and it would be really nice if the underlying JavaScript was shared where it made sense:

  • Shared ugly browser handling fluff
  • Java oddity handling
  • Python oddity handling

Then other languages and platforms could come to the party and play too.

Pyjamas

Posted by Dion Almaer at 6:54 pm
10 Comments

+++--
3.9 rating from 29 votes

Monday, May 1st, 2006

MochiKit Releases Version 1.3

Category: JavaScript, Library, Python

mochi kit logo
MochiKit, "a lightweight JavaScript Library", has released version 1.3.1. The highlight of this release is MochiKit.Signal - a simple universal event handling system. Other features include additional examples, improved documentation, and changes and tweaks to Async, Base, Logging, and Dom and packages.

For the full list of changes, check the version history, or just download it and try it. Also, if you are coming to the Ajax Experience, don't miss Bob's Intro to MochiKit if you are interesed in this library.

Posted by Rob Sanheim at 10:38 pm
9 Comments

++++-
4.2 rating from 31 votes

Monday, February 20th, 2006

The Future of JavaScript: an Update from Brendan Eich

Category: Firefox, JavaScript, Programming, Python

Brendan Eich has posted a status update on some of the work going into the upcoming JavaScript 2, aka ECMAScript Edition 4 (ES4). One feature that should look familiar to Python hackers are generators and iterators, as seen in the following example taken from a console session:

JAVASCRIPT:
  1. js> function count(n) {
  2.     for (var i = 0; i <n; i++)
  3.         yield i;
  4. }
  5. js> g = count(10)
  6. [object Generator]
  7. js> g.next()
  8. 0
  9. js> g.next()
  10. 1
  11. js> two_to_nine = [i for i in g]
  12. 2,3,4,5,6,7,8,9
  13. js> squares_to_20 = [i * i for i in count(20)]
  14. 0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361

Why go with the Python style and syntax?

Given the years of development in Python and similarities to ECMAScript in application domains and programmer communities, we would rather follow than lead. By standing on Python’s shoulders we reuse developer knowledge as well as design and implementation experience. The trick then becomes not borrowing too much from Python, just enough to gain the essential benefits: structured value-generating continuations and a general iteration protocol.

Brendan also mentioned that support for XUL in Python is almost done, and should land back into the main Firefox codebase from the DOM_AGNOSTIC2_BRANCH soon.

The official spec that will lead the way is ECMA TG1, which is being worked on by Brendan along with colleagues from Mozilla, Adobe, and Microsoft. Lets hope Brendan's vision for JavaScript's future will become reality:

My gut says that we will successfully evolve JS into a much stronger language, with a better and more standard library ecosystem, for the next ten years of its life. I will wager that other browsers will follow the ES4 standard, possibly even in 2007.

Posted by Rob Sanheim at 7:00 am
33 Comments

++++-
4 rating from 112 votes

Tuesday, January 31st, 2006

Django adopts Dojo as Ajax framework

Category: Dojo, Programming, Python

django logo
Django, the Python "Web Framework for Perfectionists", is bundling Dojo for Ajax with its 0.92 release, due in out in a few weeks. The initial integration will use Dojo in the admin interface of Django, but the toolkit will be available for any part of a Django app.

It was recently announced that the Java framework WebWork 2.2 will be released with Dojo under the hood, so this looks like another vote of confidence from the open source community in Dojo's quality.

Thanks to Eugene, who also blogged about the announcement.

Posted by Rob Sanheim at 11:39 pm
13 Comments

++++-
4.1 rating from 60 votes