Ask Your Question

Show Sage results in a webpage as string (not as Sagecell box)

asked 2016-08-12 09:49:38 -0500

Byungyun Jeon gravatar image

I'm trying to print a result on my website from Sage. I know that Sagecell can display output in website by rectangle box. Could I print as a string in web?

For example, if I write 2016 in the input form on my website, is it possible to print the next prime as "string" in web by using Sage command? (The sage command: P=Primes(); print )

How can I build the code?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-08-14 21:57:45 -0500

updated 2017-03-19 15:02:53 -0500

There is an existing way to use SageMathCell as a web API. It's described briefly here, but since the documentation needs improvement I'll record some information here.

One way to access the API is by submitting a form to (do not include a trailing slash on this URL). The documentation has a JSFiddle that I have updated for your specific example. Your command (or any other that returns a printable result) appears in a text input on the fiddle, and the output is printed below when the button is clicked.

The fiddle works by URL encoding the command and posting it to the server. If you don't want to use a form, you can read the input from a text field and submit it to the server with an XMLHttpRequest. Here's some sample code that sends your URL-encoded command to the server and opens an alert window with the result:

var xhr = new XMLHttpRequest(); 'POST', '', true );

xhr.onload = function() {

    var data = JSON.parse( xhr.responseText );
    alert( data.stdout );


xhr.setRequestHeader( 'content-type', 'application/x-www-form-urlencoded' );

xhr.send( 'code=P%3DPrimes()' );

And for command line people, here's how to get the same JSON data using cURL:

curl -d 'code=P%3DPrimes()'

References for SageMath as a web API came from this issue on GitHub. Thanks @novoselt!

edit flag offensive delete link more


@paulmasson: Thanks a lot ! They work well ! It's more than what I expected. Thanks again for your kind explanation.

Byungyun Jeon gravatar imageByungyun Jeon ( 2016-08-17 16:02:10 -0500 )edit

In URL encoding, without + and %3D, it also works fine like 'code=P=Primes()%3Bprint'.

Byungyun Jeon gravatar imageByungyun Jeon ( 2016-08-19 18:06:27 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-08-12 02:17:28 -0500

Seen: 353 times

Last updated: Mar 19