Skip to content

userzimmermann/python-coffeetools

Repository files navigation

CoffeeScript from Python

https://bitbucket.org/userzimmermann/python-coffeetools

  1. Setup

Supported Python versions: 2.7 and 3.3+

You need an installed CoffeeScript compiler. By default coffeetools look for a coffee executable in your PATH, or coffee.cmd on Windows. You can find installation instructions and further information at http://coffeescript.org/. The easiest way is to use the node package manager:

npm install coffee-script

To install coffeetools, just use pip to get the latest release from PyPI:

pip install coffeetools

For basic usage there are no dependencies on other Python packages.

Jinja and IPython integration are [extra] features. They require the following Python packages:

All extra Python requirements can also be installed with:

pip install coffeetools[all]
  1. Using CoffeeScript from Python

from coffeetools import coffee

Evaluate CoffeeScript code and return the script's output:

coffee('Some CoffeeScript')

Compile CoffeeScript to JavaScript:

coffee.compile('Some CoffeeScript')

Compile CoffeeScript to JavaScript without a top-level function():

coffee.compile('Some CoffeeScript', bare=True)

If you want to specify a custom name of your CoffeeScript compiler executable or its absolute or relative path, just create a custom interface instance:

from coffeetools import Coffee

coffee = Coffee('executable')
#OR
coffee = Coffee('/absolute/path/to/executable')
#OR
coffee = Coffee('relative/path/to/executable')
  1. Using CoffeeScript from Jinja2

More information coming soon...

  1. Evaluate CoffeeScript in an IPython Notebook

%load_ext coffeescript

It is also safe to %reload_ext coffeescript

This registers the %%coffeescript cell magic function, which works like Ipython's %%javascript magic, except that it takes CoffeeScript as input. It compiles the given code to JavaScript and uses IPython.display.display() with IPython.display.Javascript to run the code directly in the browser window:

%%coffeescript
alert $('#notebook_name').text()