Ask Your Question
3

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

asked 2016-08-12 16:49:38 +0100

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 P.next(2016) )

How can I build the code?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2016-08-15 04:57:45 +0100

updated 2017-03-19 21:02:53 +0100

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 http://sagecell.sagemath.org/service (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();

xhr.open( 'POST', 'http://sagecell.sagemath.org/service', 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()%3B+print+P.next(2016)' );

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

curl -d 'code=P%3DPrimes()%3B+print+P.next(2016)' http://sagecell.sagemath.org/service

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

edit flag offensive delete link more

Comments

@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 23:02:10 +0100 )edit

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

Byungyun Jeon gravatar imageByungyun Jeon ( 2016-08-20 01:06:27 +0100 )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

Stats

Asked: 2016-08-12 09:17:28 +0100

Seen: 1,035 times

Last updated: Mar 19 '17