Ask Your Question

Can't print from init.sage

asked 2021-05-13 05:24:35 +0200

cybervigilante gravatar image

updated 2021-05-13 05:25:28 +0200

I have some useful functions in init.sage. I wanted to list them and their parameters as an easy mnemonic, but although I can put functions in init.sage, a print statement doesn't work. Nothing prints when the kernel starts. Can I print from init.sage in some way?

edit retag flag offensive close merge delete


Please provide an example and tell us exactly what you're doing (for example, notebook or command line?). Also tell us your version of Sage and your OS.

John Palmieri gravatar imageJohn Palmieri ( 2021-05-13 06:55:37 +0200 )edit

Using Win 10, Notebook server, Sagemath 9.2. I was thinking I should target a particular cell, but not sure how to do that. It appears to work in Linux according to the note below, but I cloned that print and nothing happened. I realized that's using the REPL and I'm using the Notebook so it's probably a cell-targeting problem.

cybervigilante gravatar imagecybervigilante ( 2021-05-14 03:53:25 +0200 )edit

2 Answers

Sort by ┬╗ oldest newest most voted

answered 2021-05-20 02:51:12 +0200

dsejas gravatar image

Hello, @cybervigilante! I believe I have a solution for your problem. Let me say this was extremely difficult, and it is a little "hacky", but it should work just fine. I say "hacky" because I am going to exploit the very same fact that is causing you trouble: Jupyter notebooks don't show the output of print() functions from the file init.sage.

Sage notebooks use the custom SageKernel kernel class to run. This just inherits from the IPythonKernel class, and makes some customizations. One of these customizations is the banner that is displayed at startup. We are going to add some text to that banner. Here is some the example I will use:

from sage.repl.ipython_kernel.kernel import SageKernel
from sage.misc.banner import banner_text

%colors Linux

f(x) = 2*x^2

def my_banner(show_banner=False):
    if show_banner:
        text = [banner_text()]
        text = []
    text.append('Ctrl+D ends the session')
    return '\n'.join(text)

SageKernel.banner = my_banner(True)

Let me explain what this all means. First, we import the SageKernel class, which will be automatically used when you run Jupter(lab), and also the banner_text() function, which creates the nice SageMath banner. The line %colors Linux is there as it seems to be a popular choice for Sage Shell colors, but also in order to show that this solution does not interfere with other user code in init.sage. Then, I defined a function f to serve as an example of some user-defined content that can be displayed with this solution.

The important part is the my_banner() function. It creates a list of text (creatively called text in the code) that you want to print. All you need to do is append to that variable text. In the code, I appended as string representation of my function f and some line to remember the Crtl+D shortcut. The function will concatenate everything you put in text, with every entry separated with a new line character (that's the part \n.join(text).)

Then comes the complicated part part. When you start a terminal session of Sage, the banner seems to come from some other part (which I couldn't determine conclusively.) That is why I printed the output of my_banner() using the default value of show_banner=False; otherwise, we'd get the banner twice. On the other hand, when you run a notebook, the banner comes from SageKernel.banner, so I assigned it the output of my_banner() with show_banner set to True; otherwise, the banner won't show up because we are overwriting it.

This brings me to the "hacky part": the print(my_banner()) line is executed when you use a terminal, but the SageKernel.banner = my_banner(True) line has no effect in that case; all the way around, the former has no effect for notebooks, while the latter shows output.

I hope this helps!

edit flag offensive delete link more

answered 2021-05-13 19:10:28 +0200

slelievre gravatar image

updated 2021-05-14 19:14:28 +0200

My init.sage is as follows. It is run at the start of each Sage REPL session.

### Custom banner, displays even when starting Sage in quiet mode with `sage -q`

print("\n# SageMath {}, released {}, based on Python {}.{}.{}."
.format(sage.version.version,, *sys.version_info[:3]))

### Set colors depending on terminal background color

# Set color -- valid schemes: 'NoColor', 'Linux', 'LightBG', Neutral', ''

# To set the color scheme, use one of the following,
# where 'LightBG' is well suited for light background,
# while 'Linux' is well suited for dark background:

%colors LightBG
%colors Linux
%colors Neutral
%colors NoColor

# This can also be changed in the IPython configuration file,
# see answer by S├ębastien to Ask Sage question 

It includes two commands that print things out, and that works fine.

Beware that print is a function in Python 3, while it was a statement in Python 2.

So make sure you use print(...) rather than print ....

I'm not sure whether init.sage is executed when starting a Jupyter worksheet with the SageMath kernel; and if it is, I'm not sure where things are printed to; any print statements might be lost.

As a workaround, you could execute the following in the first cell of your Jupyter sheets:

edit flag offensive delete link more


Tried your print (py3). No go. Looks like it works in Linux but not Windows. Oh, you're using a REPL session. I think the problem is I don't know how to target a "cell" in the Sagemath notebook.

cybervigilante gravatar imagecybervigilante ( 2021-05-14 03:55:04 +0200 )edit

I have the same experience: print(...) works in REPL but not in the notebook. I wonder if there's a jupyter notebook setting that would help.

John Palmieri gravatar imageJohn Palmieri ( 2021-05-14 05:30:01 +0200 )edit

init.sage is executed in Jupyter notebooks, but print statements seem to have no effect.

John Palmieri gravatar imageJohn Palmieri ( 2021-05-14 20:57:56 +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: 2021-05-13 05:24:35 +0200

Seen: 437 times

Last updated: May 20 '21