Detecting edits in SageCells

15 views
Skip to first unread message

Ingo Dahn

unread,
Jan 14, 2023, 3:39:28 PM1/14/23
to sage-cell
When I have a SageCell html node, are there events that can be used to detect, whether the user has edited the cell input or evaluated the cell with shift+return?

Andrey Novoseltsev

unread,
Jan 14, 2023, 4:41:13 PM1/14/23
to sage-cell
There is obviously an event handler that evaluates the cell. I am not sure how easy it is to add extra code to it for the user, but surely we can make it easy.

For cell edits, I don't think that SageCell tracks it in any way internally, but I imagine Codemirror has some hooks, so if you access its component, you can attach your handlers to them.

Ingo Dahn

unread,
Jan 15, 2023, 1:04:34 AM1/15/23
to sage-cell
Thanks for the info. I can indeed detect and handle edits with the codemirror addEventListener method in a SageCell callback.. 
I might also detect shift+enter keyboard events and I can handle clicks on the Evaluate button through SageCells callback. 
But it might be more clean if I could instead add to SageCell (as for codemirror) event handler for evaluation start,  evaluation success and evaluation fail. I would not need the SageCell error message in the latter case, though.

Andrey Novoseltsev

unread,
Jan 16, 2023, 1:15:42 PM1/16/23
to sage-cell
Do you have some particular interface in mind for that?

Also I am not sure that SageMathCell has a concept of evaluation success/fail. There may be a network problem or some other reason when the communication with the kernel is lost and that's what is considered a failure. If your code leads to an unhandled exception, on the other hand, the traceback is the successful output of evaluating your code. (And the exception is eventually intercepted by IPython, just not your code.) Reliable detection of it may be tricky - even if we scan for "Traceback" in the output, the output may come in several parts and it is displayed as it comes intentionally.

Ingo Dahn

unread,
Jan 16, 2023, 5:22:37 PM1/16/23
to Andrey Novoseltsev, sage-cell
I understand that judging the success of an evaluation may not be feasible. For now it would help me to register when a cell is evaluated, but I could imagine other useful infos for the future.

For a possible form of the interface - I like that of CodeMirror:
When CM is a CoceMirror node, 
  • I can add a Handler for an Event by calling CM.addEventListener(Event,Handler) and
  • I can call a Method of the CodeMirror API by CM.CodeMirror.Method()
Similarly, for a SageCell node SC one might call SC.addEventListener('isEvaluated',Handler) and something like SC.SageCellIInput.Method() where Method is a CodeMirror method passed through to SageCell's CodeMirror.
Later one might add further methods, also to access the output of a SageCell in a similar way, if needed.

Background: I am experimenting with a Vue app that creates SageCells dynamically and I'd be ready to share the Vue component that builds the SageCell as a Github gist. For this component I'd prefer, if I could make its methods independent of the current internal structure of SageCells.

Reply all
Reply to author
Forward
0 new messages