Ask Your Question
2

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

asked 2013-01-22 07:25:48 +0100

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():
    sagenb.misc.support.automatic_names(True)
    try:
        _automatic_names
    except NameError:
        return False
    else:
        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
3

answered 2013-05-16 09:58:32 +0100

tmonteil gravatar image

updated 2013-05-16 10:01:32 +0100

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/support.py when the notebook is running (and also modified is some doctests) :

sage.server.support.EMBEDDED_MODE
sage.plot.plot.EMBEDDED_MODE
sage.misc.latex.EMBEDDED_MODE
sage.misc.pager.EMBEDDED_MODE
sage.misc.sageinspect.EMBEDDED_MODE

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 sage.server.support.EMBEDDED_MODE, 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
notebook.

Hence it seems that the right call is:

sage: misc.embedded()
False

Which is also the shortest :)

edit flag offensive delete link more

Comments

I combine a lot of these and clean up the mess in this patch: https://github.com/sagemath/sagecell/blob/master/sage-patches/01-sage-embedded.patch

Jason Grout gravatar imageJason Grout ( 2013-05-17 02:00:18 +0100 )edit
1

answered 2013-01-22 13:13:14 +0100

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
else:
    # 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

Stats

Asked: 2013-01-22 07:25:48 +0100

Seen: 556 times

Last updated: May 16 '13