1 | initial version |
SageMathCell isn't configured to work like an API (that's an interesting idea!), so here's one workaround. The code sent to the server and the result returned can be embedded in a web page but do not need to be displayed, so you can hide the input and output of the cell as described in this document. You can then access the DOM elements of the output using JavaScript. For your calculation, you'd be looking for this element in the source of the page:
<pre class="sagecell_stdout">2017</pre>
You can use JavaScript getElementsByClassName
to retrieve this tag and write its innerHTML
to the appropriate location in your document. If you want to do multiple calculations, you can retrieve the DOM element containing the Sage code, modify its innerHTML
, retrieve the "Evaluate" button with JavaScript and click
it to initiate evaluation.
The idea of a SageMath as a web API is now https://github.com/sagemath/sagecell/issues/469.
2 | No.2 Revision |
SageMathCell isn't configured to work like an API (that's an interesting idea!), so here's one workaround. The code sent to the server and the result returned can be embedded in a web page but do not need to be displayed, so you can hide the input and output of the cell as described in this document. You can then access the DOM elements of the output using JavaScript. For your calculation, you'd be looking for this element in the source of the page:
<pre class="sagecell_stdout">2017</pre>
You can use JavaScript getElementsByClassName
to retrieve this tag and write its innerHTML
to the appropriate location in your document. If you want to do multiple calculations, you can retrieve the DOM element containing the Sage code, modify its innerHTML
, retrieve the "Evaluate" button with JavaScript and click
it to initiate evaluation.
The idea of a SageMath as a web API is now https://github.com/sagemath/sagecell/issues/469.this issue on GitHub.
3 | No.3 Revision |
SageMathCell isn't configured to work like an API (that's an interesting idea!), so here's one workaround. The code sent to the server and the result returned can be embedded in a web page but do not need to be displayed, so you can hide the input and output of the cell as described in thisthis document document. . You can then access the DOM elements of the output using JavaScript. For your calculation, you'd be looking for this element in the source of the page:
<pre class="sagecell_stdout">2017</pre>
You can use JavaScript getElementsByClassName
to retrieve this tag and write its innerHTML
to the appropriate location in your document. If you want to do multiple calculations, you can retrieve the DOM element containing the Sage code, modify its innerHTML
, retrieve the "Evaluate" button with JavaScript and click
it to initiate evaluation.
The idea of a SageMath as a web API is now this issue on GitHub.
4 | No.4 Revision |
EDIT: It turns out there is an existing way to use SageMathCell as a web API. It's described on this page.
SageMathCell isn't configured to work like an (that's an interesting idea!), so here's one workaround. The code sent to the server and the result returned can be embedded in a web page but do not need to be displayed, so you can hide the input and output of the cell as described in this document. You can then access the DOM elements of the output using JavaScript. For your calculation, you'd be looking for this element in the source of the page:API API
<pre class="sagecell_stdout">2017</pre>
You can use JavaScript getElementsByClassName
to retrieve this tag and write its innerHTML
to the appropriate location in your document. If you want to do multiple calculations, you can retrieve the DOM element containing the Sage code, modify its innerHTML
, retrieve the "Evaluate" button with JavaScript and click
it to initiate evaluation.
The idea of a SageMath as a web API is now this issue on GitHub.
5 | No.5 Revision |
EDIT: It turns out there There is an existing way to use SageMathCell as a web API. It's described on this page.
SageMathCell isn't configured to work like an API (that's an interesting idea!), so here's one workaround. , but since the documentation needs improvement I'll record some information here.
On 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 code sent to the server and the result returned can be embedded documentation has a JSFiddle that I have updated for your specific example. Your command (or any other) appears in a web page but do not need to be displayed, so 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 hide read the input and output of the cell as described in this document. You can then access the DOM elements of the output using JavaScript. For from a text field and submit it to the server with an XMLHttpRequest. Here's some sample code that sends your calculation, you'd be looking for this element in the source of the page:URL-encoded command to the server and opens an alert window with the result:
<pre class="sagecell_stdout">2017</pre>
var xhr = new XMLHttpRequest();
xhr.open( 'POST', 'http://sagecell.sagemath.org/service', true );
xhr.onreadystatechange = function() {
if ( xhr.readyState === 4 ) {
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)' );
You can use JavaScript And for command line people, here's how to get the same JSON data using cURL:
getElementsByClassNamecurl -d 'code=P%3DPrimes()%3B+print+P.next(2016)' http://sagecell.sagemath.org/serviceto retrieve this tag and write itsinnerHTML
to the appropriate location in your document. If you want to do multiple calculations, you can retrieve the DOM element containing the Sage code, modify itsinnerHTML
, retrieve the "Evaluate" button with JavaScript andclick
it to initiate evaluation.The idea of a
References for SageMath as a web API is now came from this issue on GitHub.
6 | No.6 Revision |
There is an existing way to use SageMathCell as a web API. It's described on this page, but since the documentation needs improvement I'll record some information here.
On 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) 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.onreadystatechange = function() {
if ( xhr.readyState === 4 ) {
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!
7 | No.7 Revision |
There is an existing way to use SageMathCell as a web API. It's described on this page, but since the documentation needs improvement I'll record some information here.
On 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.onreadystatechange xhr.onload = function() {
if ( xhr.readyState === 4 ) {
var data = JSON.parse( xhr.responseText );
alert( data.stdout );
}
}
xhr.setRequestHeader( 'Content-type', '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!
8 | No.8 Revision |
There is an existing way to use SageMathCell as a web API. It's described on this pagebriefly here, but since the documentation needs improvement I'll record some information here.
On 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!