Ask Your Question

Is there an existing function detecting the use of the notebook ?

asked 2013-01-22 00:25:48 -0500

tmonteil gravatar image

I would like to know whether the user is using the notebook or the command line, in order to behave differently depending on that (for example to output links in html or text). The following works but is a bit hackish:

def is_notebook():
    except NameError:
        return False
        return True

Is there an existing function that does the job in sage ?

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2013-05-16 02:58:32 -0500

tmonteil gravatar image

updated 2013-05-16 03:01:32 -0500

I had a further look at this, and it seems that there are many variables named EMBEDDED_MODE, which are set to False by default in their respective module and set to True in sage/server/ when the notebook is running (and also modified is some doctests) :

Hence, they will provide the right information unless someone (like the user) set one of them to a different value to get some particuler behaviour for some particular module, independently of whether the notebook is running or not.

The one that seems more appropriate is, and actually it is called by the function embedded() whose doctest claims to give the right answer:

Return True if this copy of Sage is running embedded in the Sage

Hence it seems that the right call is:

sage: misc.embedded()

Which is also the shortest :)

edit flag offensive delete link more


I combine a lot of these and clean up the mess in this patch:

Jason Grout gravatar imageJason Grout ( 2013-05-16 19:00:18 -0500 )edit

answered 2013-01-22 06:13:14 -0500

ppurka gravatar image

I use this to check whether one is running in the notebook or not

if sage.misc.latex.EMBEDDED_MODE: # If in the notebook
    # do something specific to notebook
    # do something that is possible in command line,
    # like carriage returns :)
edit flag offensive delete link more

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: 2013-01-22 00:25:48 -0500

Seen: 177 times

Last updated: May 16 '13