SageCell

Summary: This allows you to embed an interactive SageCell into your webpage. Sage is an open source alternative to MathLab, Mathematica, and Maple.
Version: 2017-12-31-2
Prerequisites: None
Status: Stable
Maintainer: Ben Woodruff
Categories: Math
Users: (view? / edit)
Discussion: SageCell-Talk
Download: sagecell.phpΔ

Questions answered by this recipe

Can I embed a SageMath cell into my wiki so that users can perform mathematical computations on the fly?

Installation

Download sagecell.phpΔ and place it in your cookbook/ directory. Then add to config.php such a line:

 include_once("$FarmD/cookbook/sagecell.php");

Feel free to add any CSS you want to change the style.

Usage

Just type (:sage:) to start a computation block, and then type (:sageend:) to end one. Here's an example:

(:sage:)
var('x,y')
f(x)=x^2
plot(f,(x,-3,5))
(:sageend:)

The (:sage:) directive takes several arguments:

ArgumentValueDefault value
evalButtonTextLabel of the eval button (can be any string; must be enclosed in quotes if it has spaces or other special characters)Evaluate
autoevalWhether the cell is automatically evaluated on page load, without having to click the eval button; can be true or falsefalse
editorWhat editor to use; can be any of codemirror, codemirror-readonly, textarea, or textarea-readonlycodemirror
languagesComma-separated list of languages that the cell can evaluate; see docs for full list (can also be allLanguages)sage
replaceOutputWhether subsequent evaluations replace current ones; can be true or falsetrue
hideComma-separated list of UI elements to hide; see docs for full listnone
linkedID of a linked cell group (see “Linked cells”, below)none
Example: (:sage evalButtonText="PROCEED!!" languages=r,python replaceOutput=false :)

(See https://github.com/sagemath/sagecell/blob/master/doc/embedding.rst for more information on these options.)

Linked cells

You can mark a group of Sage cells on the same page as linked cells by adding the linked=N argument to each of them (where N is any number, like 1, 2, 17, etc.). You can have any number of linked cell groups; each cell in a linked group must have the same linked= argument (so you might have several cells with linked=1, and several others with linked=2—this would make two linked groups).

See the documentation for more info on linked Sage cells.

Configuration

The default options may be set in config.php (before including sagecell.php), e.g.:

$SageCellDefaultArgs = array(
	"evalButtonText"	=>	"Evaluate",
	"autoeval"		=>	"false",
	"editor"		=>	"codemirror",
	"languages"		=>	"sage",
	"replaceOutput"		=>	"true",
	"hide"			=>	"fullScreen"
);

Notes

The Sage Cell recipe works with the Gist Embed and Pastebin Embed recipes. You can pass the text of a gist or paste as the contents of a Sage cell, using the raw and no-pre arguments:

(:sage:)
(:pastebin-embed n3q5gtX7 raw no-pre:)
(:sageend:)

(And similarly with (:gist-embed:).)

Change log / Release notes

  • 2017-12-31-2: Added support for arguments and linked cells. (SaidAchmiz)
  • 2017-12-31: Packaged as a script (sagecell.php); added <pre> wrap (SaidAchmiz)

See also

http://wstein.org/edu/2012/1062/sagecell/

Contributors

Ben Woodruff

Comments

See discussion at SageCell-Talk

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.