Skip to content

Instantly share code, notes, and snippets.

@williamstein
Created September 19, 2013 22:01
Show Gist options
  • Save williamstein/6630484 to your computer and use it in GitHub Desktop.
Save williamstein/6630484 to your computer and use it in GitHub Desktop.
Load this then put "%default_mode tofile" in a cloud.sagemath worksheet.
def tofile(s):
maxout=500
import uuid
filename = str(uuid.uuid4()) +'.log'
o = open(filename,'w')
stdout = sys.stdout
class Stdout:
def __init__(self):
self.t = 0
self.extra_out = False
def reset(self):
stdout.reset()
def write(self, s):
if self.t < maxout:
stdout.write(s[:maxout-self.t])
self.t += min(len(s),maxout-self.t)
if self.t >= maxout:
self.extra_out = True
stdout.write("...")
stdout.flush()
else:
self.extra_out = True
o.write(s)
def flush(self):
o.flush()
sys.stdout.flush()
sys.stdout = Stdout()
salvus.execute(s)
extra_out = sys.stdout.extra_out
sys.stdout = stdout
o.close()
if extra_out:
url = salvus.file(filename, show=False)
salvus.html("<a href='%s' target='_blank' class='pull-right btn btn-info'>more output...</a></b>"%url)
os.unlink(filename)
salvus.stdout('',done=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment