build sagecel input programmatically

62 views
Skip to first unread message

Jorge Manrique

unread,
Oct 29, 2017, 8:13:15 PM10/29/17
to sage-cell
Hello everyone,
I want to build an expression programmatically and after cleaning it, I want to send it for evaluation to sagecell.
Is it possible to do that?

Any help would be appreciated,
Jorge


Andrey Novoseltsev

unread,
Oct 29, 2017, 8:27:41 PM10/29/17
to sage-cell

Ingo Dahn

unread,
Oct 31, 2017, 3:54:42 AM10/31/17
to sage-cell
My use case: Users are teachers who - knowing some Sage,Maxima,R but very little of programming - want to automatically configure SageCell worksheets, For example they can author randomized test questions in the php based IMathAS system and want to provide for the students supportive SageCell worksheets that adapt to the randomization of the question.
My solution:  Transmit the required modifications as URL parameters and embed some Javascript into the HTML pages containing the Sage cells for feeding the parameter values into the SageCells.
Examples: See http://dahn-research.eu:4000/sagecell, German descriptions of the available parameters are given in Details under Dokumentation. A rather generic example is https://netmath.vcrp.de/downloads/Systeme/Sage/Allgemein.html.
Limitations: I cannot get back values from SageCell, except by the user copy/paste.

Jorge Manrique

unread,
Nov 3, 2017, 6:11:17 PM11/3/17
to sage-cell
Hello everyone,
I'm stuck, I've tried two ways and none of them work for me.
 
1.- POST / service
 
$(function() {
    $("#btres").click(function() {
        $.post(
            "code:factorial(10)",
            function(d, st, xhr) {
               alert(xhr.responseText);
            },
            "json"
         );
     })
})

 The alert shows:
{"execute_reply": {"status": "ok", "execution_count": 0, "user_expressions": {}, "payload": []}, "success": true}

As you have noticed: status is ok and success is true but user_expressions is empty
I've also tried with "code = factorial (10)", with and without {}

2.- Websocket

$(function() {
    $("#btres").click(function() {
        $.post(
            wsconnect,
            "json"
        );
    })
})
var shlws, iopws;
function wsconnect(data, st, xhr) {
   var wsshell = data.ws_url + "kernel/" + data.id + "/shell";
   var wsiopub = data.ws_url + "kernel/" + data.id + "/iopub";
   alert(wsshell);
   alert(wsiopub);
   try {
     shlws = new WebSocket(wsshell);
     iopws = new WebSocket(wsiopub);
   } catch(e) {
     alert(e);
   }
}

The alerts show:
WebSocket connection to 'wss://sagecell.sagemath.org/kernel/0bd6d4cc-8d9f-4ee3-bf00-5d2d3ec57cc2/shell' failed: Error during WebSocket handshake: Unexpected response code: 404
WebSocket connection to 'wss://sagecell.sagemath.org/kernel/0bd6d4cc-8d9f-4ee3-bf00-5d2d3ec57cc2/iopub' failed: Error during WebSocket handshake: Unexpected response code: 404

I'm really lost. What am I doing wrong?
Any help would be appreciated.

Jorge

Andrey Novoseltsev

unread,
Nov 4, 2017, 1:15:10 AM11/4/17
to sage-cell
Hi Jorge,

Thanks a lot for trying and reporting results!

For POST / service: can you try print(factorial(10)) with explicit output? I suspect the display hook works differently for it, that's why the health check uses code = 'print({} + {})'.format(a, b)

For Websockets: where did you learn shell/iopub additions? They were indeed used a couple years ago, but now there is a single link with channel name embedded in messages, https://github.com/sagemath/sagecell/blob/master/contrib/sagecell-client/sagecell-client.py#L31 is relevant.

Best,
Andrey

Jorge Manrique

unread,
Nov 4, 2017, 7:51:07 PM11/4/17
to sage-cell
Hello Andrey,

POST /service worked, thanks.

Regarding websockets, I read about the shell/iopub couple here
I replaced the shell/iopub pair with:

surl = data.ws_url + "kernel/" + data.id + "/channels";
try {
  wso = new WebSocket(surl);
} catch(e) {
  alert(e);
}

And now the error is:

WebSocket connection to 'wss://sagecell.sagemath.org/kernel/74d6b265-9e9d-4eea-b6a6-ef6d1ace3da1/channels' failed: Error during WebSocket handshake: Unexpected response code: 403

I guess the error is because I do not include the header:
   Jupyter-Kernel-ID:" + data.id
when I create the websocket
The issue is that the Javascript Websocket API does not allow to include a header just like the Python API does
I think SockJS API does not allow it either

Any suggestions?
Jorge

Andrey Novoseltsev

unread,
Nov 5, 2017, 3:28:41 PM11/5/17
to sage-cell
On Saturday, 4 November 2017 17:51:07 UTC-6, Jorge Manrique wrote:
Hello Andrey,

POST /service worked, thanks.

Regarding websockets, I read about the shell/iopub couple here
I replaced the shell/iopub pair with:

surl = data.ws_url + "kernel/" + data.id + "/channels";
try {
  wso = new WebSocket(surl);
} catch(e) {
  alert(e);
}

And now the error is:

WebSocket connection to 'wss://sagecell.sagemath.org/kernel/74d6b265-9e9d-4eea-b6a6-ef6d1ace3da1/channels' failed: Error during WebSocket handshake: Unexpected response code: 403

I guess the error is because I do not include the header:
   Jupyter-Kernel-ID:" + data.id
when I create the websocket
The issue is that the Javascript Websocket API does not allow to include a header just like the Python API does
I think SockJS API does not allow it either

Is it somehow against socket standards that no custom headers can be added? It is used for load balancing and gives much better results than alternatives (which are cookies that tend to create new and new troubles or IP addresses). It is probably possible to add another matching rule like the one for files https://github.com/sagemath/sagecell/blob/master/contrib/vm/container_manager.py#L264 to make all URLs with explicit kernel id match it.
 

Andrey Novoseltsev

unread,
Dec 3, 2017, 9:21:32 PM12/3/17
to sage-cell
On Sunday, 5 November 2017 13:28:41 UTC-7, Andrey Novoseltsev wrote:
On Saturday, 4 November 2017 17:51:07 UTC-6, Jorge Manrique wrote:
Hello Andrey,

POST /service worked, thanks.

Regarding websockets, I read about the shell/iopub couple here
I replaced the shell/iopub pair with:

surl = data.ws_url + "kernel/" + data.id + "/channels";
try {
  wso = new WebSocket(surl);
} catch(e) {
  alert(e);
}

And now the error is:

WebSocket connection to 'wss://sagecell.sagemath.org/kernel/74d6b265-9e9d-4eea-b6a6-ef6d1ace3da1/channels' failed: Error during WebSocket handshake: Unexpected response code: 403

I guess the error is because I do not include the header:
   Jupyter-Kernel-ID:" + data.id
when I create the websocket
The issue is that the Javascript Websocket API does not allow to include a header just like the Python API does
I think SockJS API does not allow it either

Is it somehow against socket standards that no custom headers can be added? It is used for load balancing and gives much better results than alternatives (which are cookies that tend to create new and new troubles or IP addresses). It is probably possible to add another matching rule like the one for files https://github.com/sagemath/sagecell/blob/master/contrib/vm/container_manager.py#L264 to make all URLs with explicit kernel id match it.

Hi Jorge,

I've changed the matching rule to pick up kernel ID from URL always, not just for files. Can you check if your code behaves any better now?

Thank you!
Andrey

Reply all
Reply to author
Forward
0 new messages