Open in CoCalc

A Simple Sage Worksheet that creates Sage Worksheets

The write_sagews function below creates a Sage worksheet with any desired input. It does not yet let you specify the output, though you can put %auto at the start of a cell to evaluate it when the user opens the worksheet (thus generating output).

Note that write_sagews is pure Python (except %auto); you can copy it into any Python script to generate worksheets.

Test this out -- if it is useful to people, I'll make a more general version just be built into SMC.

%auto MARKERS = {'cell':u"\uFE20", 'output':u"\uFE21"} from uuid import uuid4 def uuid(): return unicode(uuid4()) def write_sagews(input_cells, filename): out = u"" for x in input_cells: input = unicode(x, encoding='utf8') modes = u'' if '%auto' in input: modes += u'a' if '%hide' in input: modes += u'i' if '%hideall' in input: modes += u'o' out += MARKERS['cell'] + uuid() + modes + MARKERS['cell'] + u'\n' out += input out += u'\n' + MARKERS['output'] + uuid() + MARKERS['output'] + u'\n' open(filename,'wb').write(out.encode('utf8'))
# Example write_sagews(['2+2', '%auto\na=5', '%md What is your answer?', '%auto\n%md\nThis will be **marked up**'], 'autogenerated.sagews')

Try it out:

autogenerated.sagews