Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello. Inspired by @slelievre's answer, I have come up with a way to do what I wanted. However, it seems a little cumbersome, so I am not considering it a 100% of an answer (although I am posting it as one). If somebody can improve this, please, your help is welcome!

I use the following code in a file called test.sage:

from sage.repl.interpreter import SageTerminalApp
app = SageTerminalApp.instance()

To my understanding, this creates some sort of instance of an interactive Sage session. If I add the following line:


and run sage test.sage, then the Sage prompt appears on my terminal, but it's not from the sage part of the command, but the contents of the file created this Sage session.

However, since I don't want to interact with Sage, I have used the app.code_to_run. For example, if I want to execute the

for i in range(4):

of my question, including the standard output redirection, I just add

app.code_to_run = r"""
class verboseBuffer:
def __init__(self):
    self.buffer = ''
def write(self, s):
    self.buffer += s
_verbOut_ = verboseBuffer()
sys.stdout = _verbOut_
for i in range(4):
sys.stdout = sys.__stdout__

right before app.initialize(). Now, if I run sage test.sage, the code is executed, and the session is immediately finished, without the prompt appearing (i.e., it is not an interactive session!).

The good thing is the following: Notice that I didn't use print(i^2)? Well, even so, the results of this line are passed to standard output (which is catch on _verbOut_.buffer in this case, of course), exactly as happens in the interactive terminal. To see this for yourselves, just delete the line print(_verbOut_.buffer) and execute the file again.

In summary: I can now execute Sage code in a file as if it were manually introduced to the sage terminal, which is a leap forward! But I don't completely understand my own code, so I don't know if this the best path to procedd, and I think there is space for improvement and perfectioning.