Ask Your Question

Button to copy SageMath cell to clipboard

asked 2019-08-31 20:37:06 +0200

holistone gravatar image

updated 2019-09-01 19:23:23 +0200

I will be using a link to a simple SageMathCell html page during Canvas quizzes in an online Discrete Math course. I would like to provide an additional "copy" button on the SageCell page that would allow the student to copy both their code and evaluated output into a textbox in a different browser window.

I realize it is now possible to separately cut and past the code and the output into a different textbox, but I would like to keep it as simple as possible. Any suggestions for the additional html that would add the button are appreciated.

To be more specific, I realize I could create an html button using:

<button onclick="myCopySageInOut()">Copy Sage Cell</button>

and I would need JavaScript code for the myCopySageInOut() function that would copy the input box text, append the output text and place in the clipboard. This is when I could use an example of the JavaScript code.

edit retag flag offensive close merge delete


Do you mean a HTML page with an embedded SageMathCell?

rburing gravatar imagerburing ( 2019-09-01 14:24:38 +0200 )edit

Yes. A simple html page.

holistone gravatar imageholistone ( 2019-09-01 16:38:03 +0200 )edit

I just tried to make this, but it's complicated. The output can contain LaTeX, images, etc. I can post a script that works only with text output (and ignores all other output), if you're interested.

rburing gravatar imagerburing ( 2019-09-01 23:33:33 +0200 )edit

Yes, please post. Any starting point would be help and I am only interested in the text. The assignments will be simple code with simple text output.

holistone gravatar imageholistone ( 2019-09-02 01:34:12 +0200 )edit

2 Answers

Sort by » oldest newest most voted

answered 2019-09-01 13:21:44 +0200

Emmanuel Charpentier gravatar image

What's wrong with the "Share" button ?

edit flag offensive delete link more


The "Share" button creates a link to another html page which contains the code and the output and yet another "Share" button, and so on.

I want to be able, in a single step, to copy both the code and the output into the clipboard so it can be pasted into a simple text box in another html page.

holistone gravatar imageholistone ( 2019-09-01 16:43:12 +0200 )edit

answered 2019-09-02 12:15:18 +0200

rburing gravatar image

For a proper solution you should contact the developers of the SageMathCell.

Here is a hack that seems to work with text:

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <script src=""></script>
function add_copy_button() {
        var eval_button = $('#mysagecell').find('.sagecell_evalButton');
        var copy_button = $('<button class="ui-button ui-corner-all ui-widget">Copy input and output</button>'); {
            var input_lines = $('#mysagecell .sagecell_input pre')
            var output_lines = $('#mysagecell .sagecell_output pre')
            var input = $.map(input_lines, function(element) { return $(element).text() }).join('\n');
            var output = $.map(output_lines, function(element) { return $(element).text() }).join('\n');
            $('#clipboard').val('Input:\n\n' + input + '\n\nOutput:\n\n' + output);
$(document).ready(function() {
        sagecell.makeSagecell({inputLocation: '#mysagecell',
                           evalButtonText: 'Evaluate'});
        var buttonExists = setInterval(function() {
           if ($('#mysagecell .sagecell_evalButton').length) {
        }, 100);

<h2>Your own computations</h2>
Type your own Sage computation below and click “Evaluate”.
<div id="mysagecell"><script type="text/x-sage">print next_prime(7)
print next_prime(11)
print next_prime(13)</script></div>
<textarea id="clipboard"></textarea>
edit flag offensive delete link more


That's an excellent starting point. Just a few minor modifications. Many thanks.

holistone gravatar imageholistone ( 2019-09-03 05:38:52 +0200 )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


Asked: 2019-08-31 20:37:06 +0200

Seen: 654 times

Last updated: Sep 02 '19