Sage Worksheets

Create a “Sage Worksheet” file, ending with *.sagews, in order to start working with [SageMath] on [CoCalc].

Warning

This page is work in progress

Wiki resources

SageMath specific

Conversion utilities

  • There are buttons in the UI to convert to PDF, a print-button or a Jupyter Notebook
  • SageWS to HTML (including a utility to extract the sagews file from a generated HTML file)
  • Run smc-sagews2pdf --help in a Linux Terminal for more information about converting to PDF
  • Similarly, smc-sagews2ipynb is a command-line utility to convert to *.ipynb.

Howto

Attach Sage files to Sage Worksheets

Is there a way to write functions in one worksheet, and then important them to another and use them there?

Not exactly, but you can write code in a .sage file and then load it into another Sage Worksheet as illustrated at https://cocalc.com/share/4a5f0542-5873-4eed-a85c-a18c706e8bcd/support/2018-06-12-sage-code/?viewer=share

  1. Put code in a new file with extension .sage, e.g. code.sage
  2. In a Sage worksheet or the terminal, run this: %attach code.sage or attach("code.sage")
  3. Now all code in code.sage is available in your worksheet, and whenever it changes, it will get reloaded automatically.
  4. If you’re using Jupyter, this is all broken (see https://github.com/sagemathinc/cocalc/issues/2916), but at least you can use load("code.sage") instead.

How can I connect an HTML form with my Python code?

Note

The following explanation might be outdated!

To create a connection between your HTML form in a .sagews file created using HTML, CSS and JS, you need to use the worksheet.execute_code() function in your JS code.

Because worksheet.execute_code` isn't a standard JS function, but special CoCalc function, you need to load your JS code with ``worksheet.execute_code(). In particular do NOT use

load('path/to/js/code.js')

but instead use

salvus.javascript(open('path/to/js/code.js').read())

For example in a .sagews file suppose you created a div with id='myApp' as follows:

%html
<div id='myApp'>
....
    <div id="msgLog"></div>
    <div id="msgErr"></div>
</div>

Let’s say your Python function will double x:

def myfunc(x):
    print(x*2)

In your JS code type:

worksheet.execute_code({
    code: 'myfunc(n)',
    data: {n: 2},
    preparse: true,
    cb: function(msg){
            if(msg.stdout){$('#myApp #msgLog').html(msg.stdout);}
            if(msg.stderr){$('#myApp #msgErr').html(msg.stderr);}
    }
});

Please note that myfunc() doesn’t return anything. On the contrary, it uses print() to send output. This is because JS and python are different languages, and you can’t just use return in your Python function to return some answer. stdout in JS code means standard output stream. That is, the print function in your Python code places the result of myfunc() in the output stream. That’s why you need to use print() but not return() in your Python code.

Also, if your Python code will raise some exception, then it will result in output to stderr the standard error stream. If you JS code (as in the example above) catches stderr, you can get any error message from your Python code.