logo4 Evolution is progress—                          
progress is creativity.        

Creole macro to display sage single cell server results

The sage single cell server is a convenient open source solution to add to interactive mathematical functionality to a web page. It is demonstrated here. The documentation can be found here.



The problem when integrating a sage cell into a Creole macro arises with the fact that sage cells requires extra content included into the <head> ... </head> section, JQuery source scripts. As for performance reasons it is not recommended to include those JQuery scripts into all web pages, we need a way to define an Django {% block extrahead %} block to include the source depending on the presence of the sage cell macro.

Two main possibilities exist to add that functionality into the currently running Creole macro Django framework.

  1. The Creole parser not only creates the text to be included into the <body> ... </body> section but also into the <head> ... </head>. Thus a call of the Creote parser would return a tupel (head,body) to be included by two different variables into the template. That functionality has to be programmed into the both the creole macro interpreter (maybe by the macro environment variable can be used for that purpose) and the view. The view has to receive the parsed head and body html content separately and assign it to different template variables.
  2. A filter has to be written that searches the Creole text for {% block extrahead %} relevant macros.

I opted for the latter solution as it leaves my present code virtually unchanged. The Creole parser is called by the template through a filter already and not implemented in the views.


The new filter is named creolehead. It uses creole's macro definition though the start end end tokens have to be set during initialization. Also the dictionary of installed macros is searched for the macro name to avoid mistyped macros causing errors.

[Error: Macro 'code' error: code() got an unexpected keyword argument 'lang']

This filter will be called from the extrahead block of the template. Don't forget to turn autoescape off as the filter returns pure html

[Error: Macro 'code' error: code() got an unexpected keyword argument 'lang']

Next each macro that wants to set a extra head information has to provide a head() function. As in my creole parser every macro resides in its own module the head function is simply an other function added to the file. This example shows the head function that provides the CSS data for the pygments code macro used on this page

[Error: Macro 'code' error: code() got an unexpected keyword argument 'lang']

The Sage cell macro

The macro code is shown below. To allow the several option that the sage cell server offers the java script is included in the body section and commented out. Not all options are implemented.

:If multiple cells reside on the same page a unique id has to be given each of them.
:It probably makes no sense to change the template as this feature is poorly documented and the editor is hidden anyway.
:This is a fancy option to change the evaluation botton.
:This parameter accepts a comma separated quoted list like this lang="sage,gap,gp,html,maxima,octave,python,r,singular". The example below include python, sage, and r. The most frequently used languages throughout this website.

[Error: Macro 'code' error: code() got an unexpected keyword argument 'lang']

Now click the start button to open the cell and run the little demonstration program.

[Error: Wrong macro arguments: "lang=\"sage,python,r" for macro 'sagecell' (maybe wrong macro tag syntax?)]

If multiple languages are given a selector is offered. Slecting 'r' wil cause an immediate error as the labguage is incompatible with 'r'. Also if selected python the cell is correctly displayed but moving the slider causes an error as the power function defined in sage is not valid in python.

The code used for this little demonstration is as follows.

[Error: Macro 'code' error: code() got an unexpected keyword argument 'lang']

More examples and demonstration can be found on these sage websites Graphic Tour and Interact Wiki.

Tags: Software


(c) Mato Nagel, Weißwasser 2004-2023, Disclaimer