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: 2025-01-18
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

  • 2025-01-18: Fixed PHP errors. (SaidAchmiz)
  • 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.

Array
(
    [post_max_size] => 64M
    [$_POST keys] => 
    [$_REQUEST keys] => n
    [$_SERVER] => Array
        (
            [CONTEXT_DOCUMENT_ROOT] => /home/pmwiki/public_html
            [CONTEXT_PREFIX] => 
            [DOCUMENT_ROOT] => /home/pmwiki/public_html
            [GATEWAY_INTERFACE] => CGI/1.1
            [HTTPS] => on
            [HTTP_ACCEPT] => */*
            [HTTP_ACCEPT_ENCODING] => gzip, br, zstd, deflate
            [HTTP_HOST] => www.pmwiki.org
            [HTTP_USER_AGENT] => Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
            [HTTP_X_HTTPS] => 1
            [PATH] => /bin:/usr/bin
            [PHP_INI_SCAN_DIR] => /opt/cpanel/ea-php70/root/etc:/opt/cpanel/ea-php70/root/etc/php.d:.
            [QUERY_STRING] => n=Cookbook%2fSageCell
            [REDIRECT_HTTPS] => on
            [REDIRECT_QUERY_STRING] => n=Cookbook%2fSageCell
            [REDIRECT_SCRIPT_URI] => https://www.pmwiki.org/wiki/Cookbook/SageCell
            [REDIRECT_SCRIPT_URL] => /wiki/Cookbook/SageCell
            [REDIRECT_SSL_TLS_SNI] => www.pmwiki.org
            [REDIRECT_STATUS] => 200
            [REDIRECT_UNIQUE_ID] => afLbnYwD26HPqGu8KMWwqwAAANQ
            [REDIRECT_URL] => /wiki/Cookbook/SageCell
            [REMOTE_ADDR] => 216.73.216.31
            [REMOTE_PORT] => 23873
            [REQUEST_METHOD] => GET
            [REQUEST_SCHEME] => https
            [REQUEST_URI] => /wiki/Cookbook/SageCell
            [SCRIPT_FILENAME] => /home/pmwiki/public_html/index.php
            [SCRIPT_NAME] => /index.php
            [SCRIPT_URI] => https://www.pmwiki.org/wiki/Cookbook/SageCell
            [SCRIPT_URL] => /wiki/Cookbook/SageCell
            [SERVER_ADDR] => 23.254.203.248
            [SERVER_ADMIN] => webmaster@pmwiki.org
            [SERVER_NAME] => www.pmwiki.org
            [SERVER_PORT] => 443
            [SERVER_PROTOCOL] => HTTP/1.1
            [SERVER_SIGNATURE] => 
            [SERVER_SOFTWARE] => Apache
            [SSL_TLS_SNI] => www.pmwiki.org
            [TZ] => America/Los_Angeles
            [UNIQUE_ID] => afLbnYwD26HPqGu8KMWwqwAAANQ
            [PHP_SELF] => /index.php
            [REQUEST_TIME_FLOAT] => 1777523613.2916
            [REQUEST_TIME] => 1777523613
            [argv] => Array
                (
                    [0] => n=Cookbook%2fSageCell
                )

            [argc] => 1
        )

)