ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 17 May 2013 02:00:18 +0200Is there an existing function detecting the use of the notebook ?https://ask.sagemath.org/question/9732/is-there-an-existing-function-detecting-the-use-of-the-notebook/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 ?Tue, 22 Jan 2013 07:25:48 +0100https://ask.sagemath.org/question/9732/is-there-an-existing-function-detecting-the-use-of-the-notebook/Answer by tmonteil for <p>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:</p>
<pre><code>def is_notebook():
sagenb.misc.support.automatic_names(True)
try:
_automatic_names
except NameError:
return False
else:
return True
</code></pre>
<p>Is there an existing function that does the job in sage ?</p>
https://ask.sagemath.org/question/9732/is-there-an-existing-function-detecting-the-use-of-the-notebook/?answer=14935#post-id-14935I 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 :)
Thu, 16 May 2013 09:58:32 +0200https://ask.sagemath.org/question/9732/is-there-an-existing-function-detecting-the-use-of-the-notebook/?answer=14935#post-id-14935Comment by Jason Grout for <p>I had a further look at this, and it seems that there are many variables named <code>EMBEDDED_MODE</code>, which are set to <code>False</code> by default in their respective module and set to <code>True</code> in <code>sage/server/support.py</code> when the notebook is running (and also modified is some doctests) :</p>
<pre><code>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
</code></pre>
<p>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.</p>
<p>The one that seems more appropriate is <code>sage.server.support.EMBEDDED_MODE</code>, and actually it is called by the function <code>embedded()</code> whose doctest claims to give the right answer:</p>
<pre><code>Return True if this copy of Sage is running embedded in the Sage
notebook.
</code></pre>
<p>Hence it seems that the right call is:</p>
<pre><code>sage: misc.embedded()
False
</code></pre>
<p>Which is also the shortest :)</p>
https://ask.sagemath.org/question/9732/is-there-an-existing-function-detecting-the-use-of-the-notebook/?comment=17677#post-id-17677I 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.patchFri, 17 May 2013 02:00:18 +0200https://ask.sagemath.org/question/9732/is-there-an-existing-function-detecting-the-use-of-the-notebook/?comment=17677#post-id-17677Answer by ppurka for <p>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:</p>
<pre><code>def is_notebook():
sagenb.misc.support.automatic_names(True)
try:
_automatic_names
except NameError:
return False
else:
return True
</code></pre>
<p>Is there an existing function that does the job in sage ?</p>
https://ask.sagemath.org/question/9732/is-there-an-existing-function-detecting-the-use-of-the-notebook/?answer=12774#post-id-12774I 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 :)Tue, 22 Jan 2013 13:13:14 +0100https://ask.sagemath.org/question/9732/is-there-an-existing-function-detecting-the-use-of-the-notebook/?answer=12774#post-id-12774