sagecell icon indicating copy to clipboard operation
sagecell copied to clipboard

How to send multiple and consecutive execute_request ?

Open haotianzhu opened this issue 5 years ago • 8 comments

I am trying to send consecutive executable codes to the sage server. I follow the instructions of sagecell_client.py. I find that after the code is executed, the socket and session are auto-closed. Is there a way to stop WebSocket closing after the first code is executed? Or is there any flag to allow the sage server to receive multiple requirements in one channel?

Thanks

haotianzhu avatar Dec 08 '19 23:12 haotianzhu

This is by design - it is a single request with a single response. What is your use case for multiple requests to a single kernel? Why can't you feed everything at once?

novoselt avatar Dec 09 '19 01:12 novoselt

Something like the colab, multiple cells are executed from top to bottom. My first idea is that each cell will call a single request to an open WebSocket. After every cell is executed, WebSocket is closed.

haotianzhu avatar Dec 18 '19 21:12 haotianzhu

We do support linked cells - do they suit your needs?

novoselt avatar Dec 26 '19 00:12 novoselt

I am trying something similar with a very naive approach. One click on "evalAll" should trigger all three evaluations.

With the first click on "evalAll", the results 4, 6 and 402 are all written into the first output-div.

With the second, third, ... click on "evalAll" the results are then correctly distributed to the different output-divs (even if I change the value of a or b).

Why does it behave differently with the first click?

Code:

<!doctype html>
<html lang="en">
  <head>
    <script src="https://sagecell.sagemath.org/static/embedded_sagecell.js"></script>
  </head>
  <body>

    <div class="cell" id="0">
      <script type="text/x-sage">
a=2
2*a
      </script>
    </div>
    <div class="cell" id="1">
      <script type="text/x-sage">
b=20
3*a
      </script>
    </div>
    <div class="cell" id="2">
      <script type="text/x-sage">
b^2+a
      </script>
    </div>

    <button type="button" id="evalAll">evalAll</button>

    <script>
      sagecell.makeSagecell({
        inputLocation: '.cell',
        linked: true,
      });

      var evalAll = document.getElementById('evalAll');
      evalAll.addEventListener('click', event => {
        var evalBtns = [].slice.call(document.getElementsByClassName('sagecell_evalButton'));
        evalBtns.forEach(btn => {
          console.log(btn.closest('div.cell').id);
          btn.click();
        });
      });
    </script>
  </body>
</html>

ro70 avatar Aug 01 '21 16:08 ro70

What is the use case? If you want to execute a bunch of code at once - put it in the same cell. If you want to change code of the second cell based on the output of the first one - use linked cells and let the user press the button when needed. There is apparently some race condition with clicking on several buttons automatically. I am not convinced that there is a point in figuring out how to make it work.

novoselt avatar Aug 01 '21 21:08 novoselt

As a use case, I imagine solving a task with different parts.

E.G. a) Determine the derivative of f. b) Determine the derivative of f at the point 1. c) ... d) ...

If I can distribute these parts to different (linked) sage cells, I don't need print commands for the output of the results and all results are nicely ordered. I can clearly separate the individual parts of the task.

I'd prefer something like that

f(x)=3*x^2
df(x)=diff(f(x),x)
df(x)
df(1)
...

to this

f(x)=3*x^2
df(x)=diff(f(x),x)
print(df(x))
df(1)
...

ro70 avatar Aug 02 '21 06:08 ro70

Ok, and what is the problem with having the user to execute each of the parts if they are supposed to be interactive? If the user is not supposed to change the code, then it may look even better if you post a printout of a session, with no recomputation on each load.

novoselt avatar Aug 02 '21 18:08 novoselt

The individual cells should be interactive. If each cell has its evaluation button, all subsequent evaluation buttons must be clicked when a change is made (in the worst case). This is tedious and it is easy to change a cell and forget to click an evaluation button. Then the results in other cells may no longer be correct.

Pupils are to work with the planned interface. It should therefore be as simple as possible.

ro70 avatar Aug 03 '21 06:08 ro70