ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 14 Aug 2018 13:11:31 -0500Is there a way to start a Sage session from a session of its Python interpreter ?http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/Yes, I know its sounds silly. But there *is* a point...
The R library [reticulate](https://github.com/rstudio/reticulate) allows to use a *persistent* Python session from a R session. One of its main uses is to allow to use Python code as well as R code in the creation of reports or papers. This is really useful for everyday statistics use...
One can do similar things with Sage and SageTeX, but using R and R objects is a bit awkward. Furthermore, the R tools offer abilities not easily emulated from within Sage. One *can* create composite documents using R facilities for R code (`knitr`) and SageTeX (or PythonTeX, better maintained nowadays) for supporting Sage code ; but usng such composite documents is a bit awkward : passing from the source to the compiled document entails :
* `knit` the R code of the `.Rnw` file, getting a `.tex` file ;
* `\LaTeX` the `.tex` file at least once ;
* `sage` (or `pythontex`) the resultant file
* re-`\LaTeX` the `.tex` file at least once.
This is a bit hard to automate... The same thing applies with aggravation to Markdown texts : the Sage chunks have to be wrapped in \LaTeX-only chunks, and the sage steps have to be done manually from the command line (or from emacs, which amounts to the same thing).
The availability of a persistent Sage session would solve the problem.
A small trial using Sage's R (launched by `sage -R`) shows that this is *almost* possible :
> library(reticulate)
> use_python("/usr/local/sage-8/sage") ## This is the main Sage script file
> repl_python()
Python 2.7.15 (/usr/local/sage-8/sage)
Reticulate 1.10 REPL -- A Python interpreter in R.
>>> 2^3
1
We are in python, no preparsing takes place.
>>> arctan
NameError: name 'arctan' is not defined
`arctan` is not defined : noting Sage-specific is known.
>>> from sage.all import *
>>> arctan
arctan
The import succeeded.
>>> x
NameError: name 'x' is not defined
But the (default) definition of x as a symbolic variable has not been done.
>>> var("x")
x
>>> x
x
>>> foo=arctan(x).integrate(x)
>>> exit
We are back to R, from which we can access toplevel objects in the Python session :
> py$foo
x*arctan(x) - 1/2*log(x^2 + 1)
> py$latex(py$foo)
x \arctan\left(x\right) - \frac{1}{2} \, \log\left(x^{2} + 1\right)
> py_to_r(py$latex(py$foo))
x \arctan\left(x\right) - \frac{1}{2} \, \log\left(x^{2} + 1\right)
Not a "standard" R characer vector :
> class(py_to_r(py$latex(py$foo)))
[1] "sage.misc.latex.LatexExpr" "python.builtin.str"
[3] "python.builtin.basestring" "python.builtin.object"
But it can be used as such :
> paste("** ",py$latex(py$foo)," **", sep="")
[1] "** x \\arctan\\left(x\\right) - \\frac{1}{2} \\, \\log\\left(x^{2} + 1\\right) **"
Now, it is possible to insert the loading of a *Python* module *before* the launch of the Python REPL : From the doc of `repl_python` :
module: An (optional) Python module to be imported before the REPL is
launched.
So the question is : is it possible to write a module correctly importing `sage.all` **AND** whose `__init__` function would ***replace*** Python's REPL by Sage's ?
ISTR that a few years ago, before the introduction of Sage's Jupyter notebook, such tricks were used in Jupyter to start a Sage session (complete with preparser) from an "ordinary" Jupyter notebook. But for the life of me, I haven't been able to retrieve the relevant pages...
Any thoughts ?
**EDIT :** A bit of googling using the former name "IPython notebook" led me to this StackOverflow [question](https://stackoverflow.com/questions/23384070/taking-advantage-of-sage-and-ipython-notebook-in-the-same-page-or-rather-combi), whose first answer, by no other than William Stein, tells the user that using `%load_ext sage` would start Sage from a (conventient) IPython session. Indeed :
charpent@asus16-ec:~$ sage -ipython
Python 2.7.15 (default, May 19 2018, 18:46:27)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: 2^3
Out[1]: 1
So we are in Python, no preprocessing
In [2]: x
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-6fcf9dfbd479> in <module>()
----> 1 x
NameError: name 'x' is not defined
x not defined.
In [3]: %load_ext sage
In [4]: 2^3
Out[4]: 8
We are in Sage !
In [5]: x
Out[5]: x
x has been defined
In [6]: quit()
Exiting Sage (CPU time 0m0.13s, Wall time 0m14.53s).
So this `sage` notebook extension to IPython still exists, and does what I want. Its dissection should give me what I need to write my helper module.
But for the life of me, I have been unable to divine its source. what is it, and where is it ?
**EDIT on 2018-08-13 :** that source is in `$SAGE_ROOT/src/sage/repl/ipython_extension.py`, but is insufficient (to me !) to build a solution. Question re-asked on [sage-devel](https://groups.google.com/forum/#!topic/sage-devel/xGbk8FD-3LA) in order to reach new eyeballs...Sun, 05 Aug 2018 14:11:20 -0500http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/Comment by Iguananaut for <p>Yes, I know its sounds silly. But there <em>is</em> a point...</p>
<p>The R library <a href="https://github.com/rstudio/reticulate">reticulate</a> allows to use a <em>persistent</em> Python session from a R session. One of its main uses is to allow to use Python code as well as R code in the creation of reports or papers. This is really useful for everyday statistics use...</p>
<p>One can do similar things with Sage and SageTeX, but using R and R objects is a bit awkward. Furthermore, the R tools offer abilities not easily emulated from within Sage. One <em>can</em> create composite documents using R facilities for R code (<code>knitr</code>) and SageTeX (or PythonTeX, better maintained nowadays) for supporting Sage code ; but usng such composite documents is a bit awkward : passing from the source to the compiled document entails :</p>
<ul>
<li><code>knit</code> the R code of the <code>.Rnw</code> file, getting a <code>.tex</code> file ;</li>
<li><code>\LaTeX</code> the <code>.tex</code> file at least once ;</li>
<li><code>sage</code> (or <code>pythontex</code>) the resultant file</li>
<li>re-<code>\LaTeX</code> the <code>.tex</code> file at least once.</li>
</ul>
<p>This is a bit hard to automate... The same thing applies with aggravation to Markdown texts : the Sage chunks have to be wrapped in \LaTeX-only chunks, and the sage steps have to be done manually from the command line (or from emacs, which amounts to the same thing).</p>
<p>The availability of a persistent Sage session would solve the problem.</p>
<p>A small trial using Sage's R (launched by <code>sage -R</code>) shows that this is <em>almost</em> possible : </p>
<pre><code>&gt; library(reticulate)
&gt; use_python("/usr/local/sage-8/sage") ## This is the main Sage script file
&gt; repl_python()
Python 2.7.15 (/usr/local/sage-8/sage)
Reticulate 1.10 REPL -- A Python interpreter in R.
&gt;&gt;&gt; 2^3
1
</code></pre>
<p>We are in python, no preparsing takes place.</p>
<pre><code>&gt;&gt;&gt; arctan
NameError: name 'arctan' is not defined
</code></pre>
<p><code>arctan</code> is not defined : noting Sage-specific is known.</p>
<pre><code>&gt;&gt;&gt; from sage.all import *
&gt;&gt;&gt; arctan
arctan
</code></pre>
<p>The import succeeded.</p>
<pre><code>&gt;&gt;&gt; x
NameError: name 'x' is not defined
</code></pre>
<p>But the (default) definition of x as a symbolic variable has not been done.</p>
<pre><code>&gt;&gt;&gt; var("x")
x
&gt;&gt;&gt; x
x
&gt;&gt;&gt; foo=arctan(x).integrate(x)
&gt;&gt;&gt; exit
</code></pre>
<p>We are back to R, from which we can access toplevel objects in the Python session :</p>
<pre><code>&gt; py$foo
x*arctan(x) - 1/2*log(x^2 + 1)
&gt; py$latex(py$foo)
x \arctan\left(x\right) - \frac{1}{2} \, \log\left(x^{2} + 1\right)
&gt; py_to_r(py$latex(py$foo))
x \arctan\left(x\right) - \frac{1}{2} \, \log\left(x^{2} + 1\right)
</code></pre>
<p>Not a "standard" R characer vector :</p>
<pre><code>&gt; class(py_to_r(py$latex(py$foo)))
[1] "sage.misc.latex.LatexExpr" "python.builtin.str"
[3] "python.builtin.basestring" "python.builtin.object"
</code></pre>
<p>But it can be used as such :</p>
<pre><code>&gt; paste("** ",py$latex(py$foo)," **", sep="")
[1] "** x \\arctan\\left(x\\right) - \\frac{1}{2} \\, \\log\\left(x^{2} + 1\\right) **"
</code></pre>
<p>Now, it is possible to insert the loading of a <em>Python</em> module <em>before</em> the launch of the Python REPL : From the doc of <code>repl_python</code> :</p>
<p>module: An (optional) Python module to be imported before the REPL is
launched.</p>
<p>So the question is : is it possible to write a module correctly importing <code>sage.all</code> <strong>AND</strong> whose <code>__init__</code> function would <strong><em>replace</em></strong> Python's REPL by Sage's ?</p>
<p>ISTR that a few years ago, before the introduction of Sage's Jupyter notebook, such tricks were used in Jupyter to start a Sage session (complete with preparser) from an "ordinary" Jupyter notebook. But for the life of me, I haven't been able to retrieve the relevant pages...</p>
<p>Any thoughts ?</p>
<p><strong>EDIT :</strong> A bit of googling using the former name "IPython notebook" led me to this StackOverflow <a href="https://stackoverflow.com/questions/23384070/taking-advantage-of-sage-and-ipython-notebook-in-the-same-page-or-rather-combi">question</a>, whose first answer, by no other than William Stein, tells the user that using <code>%load_ext sage</code> would start Sage from a (conventient) IPython session. Indeed : </p>
<pre><code>charpent@asus16-ec:~$ sage -ipython
Python 2.7.15 (default, May 19 2018, 18:46:27)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -&gt; Introduction and overview of IPython's features.
%quickref -&gt; Quick reference.
help -&gt; Python's own help system.
object? -&gt; Details about 'object', use 'object??' for extra details.
In [1]: 2^3
Out[1]: 1
</code></pre>
<p>So we are in Python, no preprocessing</p>
<pre><code>In [2]: x
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
&lt;ipython-input-2-6fcf9dfbd479&gt; in &lt;module&gt;()
----&gt; 1 x
NameError: name 'x' is not defined
</code></pre>
<p>x not defined.</p>
<pre><code>In [3]: %load_ext sage
In [4]: 2^3
Out[4]: 8
</code></pre>
<p>We are in Sage !</p>
<pre><code>In [5]: x
Out[5]: x
</code></pre>
<p>x has been defined</p>
<pre><code>In [6]: quit()
Exiting Sage (CPU time 0m0.13s, Wall time 0m14.53s).
</code></pre>
<p>So this <code>sage</code> notebook extension to IPython still exists, and does what I want. Its dissection should give me what I need to write my helper module.</p>
<p>But for the life of me, I have been unable to divine its source. what is it, and where is it ?</p>
<p><strong>EDIT on 2018-08-13 :</strong> that source is in <code>$SAGE_ROOT/src/sage/repl/ipython_extension.py</code>, but is insufficient (to me !) to build a solution. Question re-asked on <a href="https://groups.google.com/forum/#!topic/sage-devel/xGbk8FD-3LA">sage-devel</a> in order to reach new eyeballs...</p>
http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43376#post-id-43376`eval` only takes an expression as input (or a pre-compiled `code` object). An assignment is a statement in Python, not an expression, so trying to eval (or `sage_eval`) an assignment statement will result in a `SyntaxError`.Tue, 14 Aug 2018 13:11:31 -0500http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43376#post-id-43376Comment by Emmanuel Charpentier for <p>Yes, I know its sounds silly. But there <em>is</em> a point...</p>
<p>The R library <a href="https://github.com/rstudio/reticulate">reticulate</a> allows to use a <em>persistent</em> Python session from a R session. One of its main uses is to allow to use Python code as well as R code in the creation of reports or papers. This is really useful for everyday statistics use...</p>
<p>One can do similar things with Sage and SageTeX, but using R and R objects is a bit awkward. Furthermore, the R tools offer abilities not easily emulated from within Sage. One <em>can</em> create composite documents using R facilities for R code (<code>knitr</code>) and SageTeX (or PythonTeX, better maintained nowadays) for supporting Sage code ; but usng such composite documents is a bit awkward : passing from the source to the compiled document entails :</p>
<ul>
<li><code>knit</code> the R code of the <code>.Rnw</code> file, getting a <code>.tex</code> file ;</li>
<li><code>\LaTeX</code> the <code>.tex</code> file at least once ;</li>
<li><code>sage</code> (or <code>pythontex</code>) the resultant file</li>
<li>re-<code>\LaTeX</code> the <code>.tex</code> file at least once.</li>
</ul>
<p>This is a bit hard to automate... The same thing applies with aggravation to Markdown texts : the Sage chunks have to be wrapped in \LaTeX-only chunks, and the sage steps have to be done manually from the command line (or from emacs, which amounts to the same thing).</p>
<p>The availability of a persistent Sage session would solve the problem.</p>
<p>A small trial using Sage's R (launched by <code>sage -R</code>) shows that this is <em>almost</em> possible : </p>
<pre><code>&gt; library(reticulate)
&gt; use_python("/usr/local/sage-8/sage") ## This is the main Sage script file
&gt; repl_python()
Python 2.7.15 (/usr/local/sage-8/sage)
Reticulate 1.10 REPL -- A Python interpreter in R.
&gt;&gt;&gt; 2^3
1
</code></pre>
<p>We are in python, no preparsing takes place.</p>
<pre><code>&gt;&gt;&gt; arctan
NameError: name 'arctan' is not defined
</code></pre>
<p><code>arctan</code> is not defined : noting Sage-specific is known.</p>
<pre><code>&gt;&gt;&gt; from sage.all import *
&gt;&gt;&gt; arctan
arctan
</code></pre>
<p>The import succeeded.</p>
<pre><code>&gt;&gt;&gt; x
NameError: name 'x' is not defined
</code></pre>
<p>But the (default) definition of x as a symbolic variable has not been done.</p>
<pre><code>&gt;&gt;&gt; var("x")
x
&gt;&gt;&gt; x
x
&gt;&gt;&gt; foo=arctan(x).integrate(x)
&gt;&gt;&gt; exit
</code></pre>
<p>We are back to R, from which we can access toplevel objects in the Python session :</p>
<pre><code>&gt; py$foo
x*arctan(x) - 1/2*log(x^2 + 1)
&gt; py$latex(py$foo)
x \arctan\left(x\right) - \frac{1}{2} \, \log\left(x^{2} + 1\right)
&gt; py_to_r(py$latex(py$foo))
x \arctan\left(x\right) - \frac{1}{2} \, \log\left(x^{2} + 1\right)
</code></pre>
<p>Not a "standard" R characer vector :</p>
<pre><code>&gt; class(py_to_r(py$latex(py$foo)))
[1] "sage.misc.latex.LatexExpr" "python.builtin.str"
[3] "python.builtin.basestring" "python.builtin.object"
</code></pre>
<p>But it can be used as such :</p>
<pre><code>&gt; paste("** ",py$latex(py$foo)," **", sep="")
[1] "** x \\arctan\\left(x\\right) - \\frac{1}{2} \\, \\log\\left(x^{2} + 1\\right) **"
</code></pre>
<p>Now, it is possible to insert the loading of a <em>Python</em> module <em>before</em> the launch of the Python REPL : From the doc of <code>repl_python</code> :</p>
<p>module: An (optional) Python module to be imported before the REPL is
launched.</p>
<p>So the question is : is it possible to write a module correctly importing <code>sage.all</code> <strong>AND</strong> whose <code>__init__</code> function would <strong><em>replace</em></strong> Python's REPL by Sage's ?</p>
<p>ISTR that a few years ago, before the introduction of Sage's Jupyter notebook, such tricks were used in Jupyter to start a Sage session (complete with preparser) from an "ordinary" Jupyter notebook. But for the life of me, I haven't been able to retrieve the relevant pages...</p>
<p>Any thoughts ?</p>
<p><strong>EDIT :</strong> A bit of googling using the former name "IPython notebook" led me to this StackOverflow <a href="https://stackoverflow.com/questions/23384070/taking-advantage-of-sage-and-ipython-notebook-in-the-same-page-or-rather-combi">question</a>, whose first answer, by no other than William Stein, tells the user that using <code>%load_ext sage</code> would start Sage from a (conventient) IPython session. Indeed : </p>
<pre><code>charpent@asus16-ec:~$ sage -ipython
Python 2.7.15 (default, May 19 2018, 18:46:27)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -&gt; Introduction and overview of IPython's features.
%quickref -&gt; Quick reference.
help -&gt; Python's own help system.
object? -&gt; Details about 'object', use 'object??' for extra details.
In [1]: 2^3
Out[1]: 1
</code></pre>
<p>So we are in Python, no preprocessing</p>
<pre><code>In [2]: x
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
&lt;ipython-input-2-6fcf9dfbd479&gt; in &lt;module&gt;()
----&gt; 1 x
NameError: name 'x' is not defined
</code></pre>
<p>x not defined.</p>
<pre><code>In [3]: %load_ext sage
In [4]: 2^3
Out[4]: 8
</code></pre>
<p>We are in Sage !</p>
<pre><code>In [5]: x
Out[5]: x
</code></pre>
<p>x has been defined</p>
<pre><code>In [6]: quit()
Exiting Sage (CPU time 0m0.13s, Wall time 0m14.53s).
</code></pre>
<p>So this <code>sage</code> notebook extension to IPython still exists, and does what I want. Its dissection should give me what I need to write my helper module.</p>
<p>But for the life of me, I have been unable to divine its source. what is it, and where is it ?</p>
<p><strong>EDIT on 2018-08-13 :</strong> that source is in <code>$SAGE_ROOT/src/sage/repl/ipython_extension.py</code>, but is insufficient (to me !) to build a solution. Question re-asked on <a href="https://groups.google.com/forum/#!topic/sage-devel/xGbk8FD-3LA">sage-devel</a> in order to reach new eyeballs...</p>
http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43375#post-id-43375@rburing : doesn't work. From R :
> S='sage_eval("foo=2^3")'
> py_run_string(S)
Error in py_run_string_impl(code, local, convert) :
SyntaxError: invalid syntax (<string>, line 1)
Detailed traceback:
File "<string>", line 1, in <module>
File "/usr/local/sage-8/local/lib/python2.7/site-packages/sage/misc/sage_eval.py", line 203, in sage_eval
return eval(source, sage.all.__dict__, locals)
>
I think that `sage_eval` doesn't find the "right" Python eval...Tue, 14 Aug 2018 09:00:23 -0500http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43375#post-id-43375Comment by rburing for <p>Yes, I know its sounds silly. But there <em>is</em> a point...</p>
<p>The R library <a href="https://github.com/rstudio/reticulate">reticulate</a> allows to use a <em>persistent</em> Python session from a R session. One of its main uses is to allow to use Python code as well as R code in the creation of reports or papers. This is really useful for everyday statistics use...</p>
<p>One can do similar things with Sage and SageTeX, but using R and R objects is a bit awkward. Furthermore, the R tools offer abilities not easily emulated from within Sage. One <em>can</em> create composite documents using R facilities for R code (<code>knitr</code>) and SageTeX (or PythonTeX, better maintained nowadays) for supporting Sage code ; but usng such composite documents is a bit awkward : passing from the source to the compiled document entails :</p>
<ul>
<li><code>knit</code> the R code of the <code>.Rnw</code> file, getting a <code>.tex</code> file ;</li>
<li><code>\LaTeX</code> the <code>.tex</code> file at least once ;</li>
<li><code>sage</code> (or <code>pythontex</code>) the resultant file</li>
<li>re-<code>\LaTeX</code> the <code>.tex</code> file at least once.</li>
</ul>
<p>This is a bit hard to automate... The same thing applies with aggravation to Markdown texts : the Sage chunks have to be wrapped in \LaTeX-only chunks, and the sage steps have to be done manually from the command line (or from emacs, which amounts to the same thing).</p>
<p>The availability of a persistent Sage session would solve the problem.</p>
<p>A small trial using Sage's R (launched by <code>sage -R</code>) shows that this is <em>almost</em> possible : </p>
<pre><code>&gt; library(reticulate)
&gt; use_python("/usr/local/sage-8/sage") ## This is the main Sage script file
&gt; repl_python()
Python 2.7.15 (/usr/local/sage-8/sage)
Reticulate 1.10 REPL -- A Python interpreter in R.
&gt;&gt;&gt; 2^3
1
</code></pre>
<p>We are in python, no preparsing takes place.</p>
<pre><code>&gt;&gt;&gt; arctan
NameError: name 'arctan' is not defined
</code></pre>
<p><code>arctan</code> is not defined : noting Sage-specific is known.</p>
<pre><code>&gt;&gt;&gt; from sage.all import *
&gt;&gt;&gt; arctan
arctan
</code></pre>
<p>The import succeeded.</p>
<pre><code>&gt;&gt;&gt; x
NameError: name 'x' is not defined
</code></pre>
<p>But the (default) definition of x as a symbolic variable has not been done.</p>
<pre><code>&gt;&gt;&gt; var("x")
x
&gt;&gt;&gt; x
x
&gt;&gt;&gt; foo=arctan(x).integrate(x)
&gt;&gt;&gt; exit
</code></pre>
<p>We are back to R, from which we can access toplevel objects in the Python session :</p>
<pre><code>&gt; py$foo
x*arctan(x) - 1/2*log(x^2 + 1)
&gt; py$latex(py$foo)
x \arctan\left(x\right) - \frac{1}{2} \, \log\left(x^{2} + 1\right)
&gt; py_to_r(py$latex(py$foo))
x \arctan\left(x\right) - \frac{1}{2} \, \log\left(x^{2} + 1\right)
</code></pre>
<p>Not a "standard" R characer vector :</p>
<pre><code>&gt; class(py_to_r(py$latex(py$foo)))
[1] "sage.misc.latex.LatexExpr" "python.builtin.str"
[3] "python.builtin.basestring" "python.builtin.object"
</code></pre>
<p>But it can be used as such :</p>
<pre><code>&gt; paste("** ",py$latex(py$foo)," **", sep="")
[1] "** x \\arctan\\left(x\\right) - \\frac{1}{2} \\, \\log\\left(x^{2} + 1\\right) **"
</code></pre>
<p>Now, it is possible to insert the loading of a <em>Python</em> module <em>before</em> the launch of the Python REPL : From the doc of <code>repl_python</code> :</p>
<p>module: An (optional) Python module to be imported before the REPL is
launched.</p>
<p>So the question is : is it possible to write a module correctly importing <code>sage.all</code> <strong>AND</strong> whose <code>__init__</code> function would <strong><em>replace</em></strong> Python's REPL by Sage's ?</p>
<p>ISTR that a few years ago, before the introduction of Sage's Jupyter notebook, such tricks were used in Jupyter to start a Sage session (complete with preparser) from an "ordinary" Jupyter notebook. But for the life of me, I haven't been able to retrieve the relevant pages...</p>
<p>Any thoughts ?</p>
<p><strong>EDIT :</strong> A bit of googling using the former name "IPython notebook" led me to this StackOverflow <a href="https://stackoverflow.com/questions/23384070/taking-advantage-of-sage-and-ipython-notebook-in-the-same-page-or-rather-combi">question</a>, whose first answer, by no other than William Stein, tells the user that using <code>%load_ext sage</code> would start Sage from a (conventient) IPython session. Indeed : </p>
<pre><code>charpent@asus16-ec:~$ sage -ipython
Python 2.7.15 (default, May 19 2018, 18:46:27)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -&gt; Introduction and overview of IPython's features.
%quickref -&gt; Quick reference.
help -&gt; Python's own help system.
object? -&gt; Details about 'object', use 'object??' for extra details.
In [1]: 2^3
Out[1]: 1
</code></pre>
<p>So we are in Python, no preprocessing</p>
<pre><code>In [2]: x
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
&lt;ipython-input-2-6fcf9dfbd479&gt; in &lt;module&gt;()
----&gt; 1 x
NameError: name 'x' is not defined
</code></pre>
<p>x not defined.</p>
<pre><code>In [3]: %load_ext sage
In [4]: 2^3
Out[4]: 8
</code></pre>
<p>We are in Sage !</p>
<pre><code>In [5]: x
Out[5]: x
</code></pre>
<p>x has been defined</p>
<pre><code>In [6]: quit()
Exiting Sage (CPU time 0m0.13s, Wall time 0m14.53s).
</code></pre>
<p>So this <code>sage</code> notebook extension to IPython still exists, and does what I want. Its dissection should give me what I need to write my helper module.</p>
<p>But for the life of me, I have been unable to divine its source. what is it, and where is it ?</p>
<p><strong>EDIT on 2018-08-13 :</strong> that source is in <code>$SAGE_ROOT/src/sage/repl/ipython_extension.py</code>, but is insufficient (to me !) to build a solution. Question re-asked on <a href="https://groups.google.com/forum/#!topic/sage-devel/xGbk8FD-3LA">sage-devel</a> in order to reach new eyeballs...</p>
http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43373#post-id-43373How about wrapping commands in `sage_eval(...)`? That should give you access to the preparser from Python.Mon, 13 Aug 2018 16:42:49 -0500http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43373#post-id-43373Comment by Iguananaut for <p>Yes, I know its sounds silly. But there <em>is</em> a point...</p>
<p>The R library <a href="https://github.com/rstudio/reticulate">reticulate</a> allows to use a <em>persistent</em> Python session from a R session. One of its main uses is to allow to use Python code as well as R code in the creation of reports or papers. This is really useful for everyday statistics use...</p>
<p>One can do similar things with Sage and SageTeX, but using R and R objects is a bit awkward. Furthermore, the R tools offer abilities not easily emulated from within Sage. One <em>can</em> create composite documents using R facilities for R code (<code>knitr</code>) and SageTeX (or PythonTeX, better maintained nowadays) for supporting Sage code ; but usng such composite documents is a bit awkward : passing from the source to the compiled document entails :</p>
<ul>
<li><code>knit</code> the R code of the <code>.Rnw</code> file, getting a <code>.tex</code> file ;</li>
<li><code>\LaTeX</code> the <code>.tex</code> file at least once ;</li>
<li><code>sage</code> (or <code>pythontex</code>) the resultant file</li>
<li>re-<code>\LaTeX</code> the <code>.tex</code> file at least once.</li>
</ul>
<p>This is a bit hard to automate... The same thing applies with aggravation to Markdown texts : the Sage chunks have to be wrapped in \LaTeX-only chunks, and the sage steps have to be done manually from the command line (or from emacs, which amounts to the same thing).</p>
<p>The availability of a persistent Sage session would solve the problem.</p>
<p>A small trial using Sage's R (launched by <code>sage -R</code>) shows that this is <em>almost</em> possible : </p>
<pre><code>&gt; library(reticulate)
&gt; use_python("/usr/local/sage-8/sage") ## This is the main Sage script file
&gt; repl_python()
Python 2.7.15 (/usr/local/sage-8/sage)
Reticulate 1.10 REPL -- A Python interpreter in R.
&gt;&gt;&gt; 2^3
1
</code></pre>
<p>We are in python, no preparsing takes place.</p>
<pre><code>&gt;&gt;&gt; arctan
NameError: name 'arctan' is not defined
</code></pre>
<p><code>arctan</code> is not defined : noting Sage-specific is known.</p>
<pre><code>&gt;&gt;&gt; from sage.all import *
&gt;&gt;&gt; arctan
arctan
</code></pre>
<p>The import succeeded.</p>
<pre><code>&gt;&gt;&gt; x
NameError: name 'x' is not defined
</code></pre>
<p>But the (default) definition of x as a symbolic variable has not been done.</p>
<pre><code>&gt;&gt;&gt; var("x")
x
&gt;&gt;&gt; x
x
&gt;&gt;&gt; foo=arctan(x).integrate(x)
&gt;&gt;&gt; exit
</code></pre>
<p>We are back to R, from which we can access toplevel objects in the Python session :</p>
<pre><code>&gt; py$foo
x*arctan(x) - 1/2*log(x^2 + 1)
&gt; py$latex(py$foo)
x \arctan\left(x\right) - \frac{1}{2} \, \log\left(x^{2} + 1\right)
&gt; py_to_r(py$latex(py$foo))
x \arctan\left(x\right) - \frac{1}{2} \, \log\left(x^{2} + 1\right)
</code></pre>
<p>Not a "standard" R characer vector :</p>
<pre><code>&gt; class(py_to_r(py$latex(py$foo)))
[1] "sage.misc.latex.LatexExpr" "python.builtin.str"
[3] "python.builtin.basestring" "python.builtin.object"
</code></pre>
<p>But it can be used as such :</p>
<pre><code>&gt; paste("** ",py$latex(py$foo)," **", sep="")
[1] "** x \\arctan\\left(x\\right) - \\frac{1}{2} \\, \\log\\left(x^{2} + 1\\right) **"
</code></pre>
<p>Now, it is possible to insert the loading of a <em>Python</em> module <em>before</em> the launch of the Python REPL : From the doc of <code>repl_python</code> :</p>
<p>module: An (optional) Python module to be imported before the REPL is
launched.</p>
<p>So the question is : is it possible to write a module correctly importing <code>sage.all</code> <strong>AND</strong> whose <code>__init__</code> function would <strong><em>replace</em></strong> Python's REPL by Sage's ?</p>
<p>ISTR that a few years ago, before the introduction of Sage's Jupyter notebook, such tricks were used in Jupyter to start a Sage session (complete with preparser) from an "ordinary" Jupyter notebook. But for the life of me, I haven't been able to retrieve the relevant pages...</p>
<p>Any thoughts ?</p>
<p><strong>EDIT :</strong> A bit of googling using the former name "IPython notebook" led me to this StackOverflow <a href="https://stackoverflow.com/questions/23384070/taking-advantage-of-sage-and-ipython-notebook-in-the-same-page-or-rather-combi">question</a>, whose first answer, by no other than William Stein, tells the user that using <code>%load_ext sage</code> would start Sage from a (conventient) IPython session. Indeed : </p>
<pre><code>charpent@asus16-ec:~$ sage -ipython
Python 2.7.15 (default, May 19 2018, 18:46:27)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -&gt; Introduction and overview of IPython's features.
%quickref -&gt; Quick reference.
help -&gt; Python's own help system.
object? -&gt; Details about 'object', use 'object??' for extra details.
In [1]: 2^3
Out[1]: 1
</code></pre>
<p>So we are in Python, no preprocessing</p>
<pre><code>In [2]: x
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
&lt;ipython-input-2-6fcf9dfbd479&gt; in &lt;module&gt;()
----&gt; 1 x
NameError: name 'x' is not defined
</code></pre>
<p>x not defined.</p>
<pre><code>In [3]: %load_ext sage
In [4]: 2^3
Out[4]: 8
</code></pre>
<p>We are in Sage !</p>
<pre><code>In [5]: x
Out[5]: x
</code></pre>
<p>x has been defined</p>
<pre><code>In [6]: quit()
Exiting Sage (CPU time 0m0.13s, Wall time 0m14.53s).
</code></pre>
<p>So this <code>sage</code> notebook extension to IPython still exists, and does what I want. Its dissection should give me what I need to write my helper module.</p>
<p>But for the life of me, I have been unable to divine its source. what is it, and where is it ?</p>
<p><strong>EDIT on 2018-08-13 :</strong> that source is in <code>$SAGE_ROOT/src/sage/repl/ipython_extension.py</code>, but is insufficient (to me !) to build a solution. Question re-asked on <a href="https://groups.google.com/forum/#!topic/sage-devel/xGbk8FD-3LA">sage-devel</a> in order to reach new eyeballs...</p>
http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43273#post-id-43273Have you tried running `sage-ipython` instead?Mon, 06 Aug 2018 10:33:20 -0500http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43273#post-id-43273Answer by tmonteil for <p>Yes, I know its sounds silly. But there <em>is</em> a point...</p>
<p>The R library <a href="https://github.com/rstudio/reticulate">reticulate</a> allows to use a <em>persistent</em> Python session from a R session. One of its main uses is to allow to use Python code as well as R code in the creation of reports or papers. This is really useful for everyday statistics use...</p>
<p>One can do similar things with Sage and SageTeX, but using R and R objects is a bit awkward. Furthermore, the R tools offer abilities not easily emulated from within Sage. One <em>can</em> create composite documents using R facilities for R code (<code>knitr</code>) and SageTeX (or PythonTeX, better maintained nowadays) for supporting Sage code ; but usng such composite documents is a bit awkward : passing from the source to the compiled document entails :</p>
<ul>
<li><code>knit</code> the R code of the <code>.Rnw</code> file, getting a <code>.tex</code> file ;</li>
<li><code>\LaTeX</code> the <code>.tex</code> file at least once ;</li>
<li><code>sage</code> (or <code>pythontex</code>) the resultant file</li>
<li>re-<code>\LaTeX</code> the <code>.tex</code> file at least once.</li>
</ul>
<p>This is a bit hard to automate... The same thing applies with aggravation to Markdown texts : the Sage chunks have to be wrapped in \LaTeX-only chunks, and the sage steps have to be done manually from the command line (or from emacs, which amounts to the same thing).</p>
<p>The availability of a persistent Sage session would solve the problem.</p>
<p>A small trial using Sage's R (launched by <code>sage -R</code>) shows that this is <em>almost</em> possible : </p>
<pre><code>&gt; library(reticulate)
&gt; use_python("/usr/local/sage-8/sage") ## This is the main Sage script file
&gt; repl_python()
Python 2.7.15 (/usr/local/sage-8/sage)
Reticulate 1.10 REPL -- A Python interpreter in R.
&gt;&gt;&gt; 2^3
1
</code></pre>
<p>We are in python, no preparsing takes place.</p>
<pre><code>&gt;&gt;&gt; arctan
NameError: name 'arctan' is not defined
</code></pre>
<p><code>arctan</code> is not defined : noting Sage-specific is known.</p>
<pre><code>&gt;&gt;&gt; from sage.all import *
&gt;&gt;&gt; arctan
arctan
</code></pre>
<p>The import succeeded.</p>
<pre><code>&gt;&gt;&gt; x
NameError: name 'x' is not defined
</code></pre>
<p>But the (default) definition of x as a symbolic variable has not been done.</p>
<pre><code>&gt;&gt;&gt; var("x")
x
&gt;&gt;&gt; x
x
&gt;&gt;&gt; foo=arctan(x).integrate(x)
&gt;&gt;&gt; exit
</code></pre>
<p>We are back to R, from which we can access toplevel objects in the Python session :</p>
<pre><code>&gt; py$foo
x*arctan(x) - 1/2*log(x^2 + 1)
&gt; py$latex(py$foo)
x \arctan\left(x\right) - \frac{1}{2} \, \log\left(x^{2} + 1\right)
&gt; py_to_r(py$latex(py$foo))
x \arctan\left(x\right) - \frac{1}{2} \, \log\left(x^{2} + 1\right)
</code></pre>
<p>Not a "standard" R characer vector :</p>
<pre><code>&gt; class(py_to_r(py$latex(py$foo)))
[1] "sage.misc.latex.LatexExpr" "python.builtin.str"
[3] "python.builtin.basestring" "python.builtin.object"
</code></pre>
<p>But it can be used as such :</p>
<pre><code>&gt; paste("** ",py$latex(py$foo)," **", sep="")
[1] "** x \\arctan\\left(x\\right) - \\frac{1}{2} \\, \\log\\left(x^{2} + 1\\right) **"
</code></pre>
<p>Now, it is possible to insert the loading of a <em>Python</em> module <em>before</em> the launch of the Python REPL : From the doc of <code>repl_python</code> :</p>
<p>module: An (optional) Python module to be imported before the REPL is
launched.</p>
<p>So the question is : is it possible to write a module correctly importing <code>sage.all</code> <strong>AND</strong> whose <code>__init__</code> function would <strong><em>replace</em></strong> Python's REPL by Sage's ?</p>
<p>ISTR that a few years ago, before the introduction of Sage's Jupyter notebook, such tricks were used in Jupyter to start a Sage session (complete with preparser) from an "ordinary" Jupyter notebook. But for the life of me, I haven't been able to retrieve the relevant pages...</p>
<p>Any thoughts ?</p>
<p><strong>EDIT :</strong> A bit of googling using the former name "IPython notebook" led me to this StackOverflow <a href="https://stackoverflow.com/questions/23384070/taking-advantage-of-sage-and-ipython-notebook-in-the-same-page-or-rather-combi">question</a>, whose first answer, by no other than William Stein, tells the user that using <code>%load_ext sage</code> would start Sage from a (conventient) IPython session. Indeed : </p>
<pre><code>charpent@asus16-ec:~$ sage -ipython
Python 2.7.15 (default, May 19 2018, 18:46:27)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -&gt; Introduction and overview of IPython's features.
%quickref -&gt; Quick reference.
help -&gt; Python's own help system.
object? -&gt; Details about 'object', use 'object??' for extra details.
In [1]: 2^3
Out[1]: 1
</code></pre>
<p>So we are in Python, no preprocessing</p>
<pre><code>In [2]: x
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
&lt;ipython-input-2-6fcf9dfbd479&gt; in &lt;module&gt;()
----&gt; 1 x
NameError: name 'x' is not defined
</code></pre>
<p>x not defined.</p>
<pre><code>In [3]: %load_ext sage
In [4]: 2^3
Out[4]: 8
</code></pre>
<p>We are in Sage !</p>
<pre><code>In [5]: x
Out[5]: x
</code></pre>
<p>x has been defined</p>
<pre><code>In [6]: quit()
Exiting Sage (CPU time 0m0.13s, Wall time 0m14.53s).
</code></pre>
<p>So this <code>sage</code> notebook extension to IPython still exists, and does what I want. Its dissection should give me what I need to write my helper module.</p>
<p>But for the life of me, I have been unable to divine its source. what is it, and where is it ?</p>
<p><strong>EDIT on 2018-08-13 :</strong> that source is in <code>$SAGE_ROOT/src/sage/repl/ipython_extension.py</code>, but is insufficient (to me !) to build a solution. Question re-asked on <a href="https://groups.google.com/forum/#!topic/sage-devel/xGbk8FD-3LA">sage-devel</a> in order to reach new eyeballs...</p>
http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?answer=43264#post-id-43264The source can be found in the `SAGE_ROOT/src/sage/repl/` directory, in particular `interpreter.py`.
**EDIT** : if you want to run ipython interpreter from Python's one, (and then Sage the way you suggested), you can do:
>>> import IPython
>>> IPython.embed()
Python 2.7.15 (default, Jul 23 2018, 10:39:00)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: %load_ext sage
In [2]: 2^2
Out[2]: 4
In [3]: sin(x)
Out[3]: sin(x)Mon, 06 Aug 2018 05:56:38 -0500http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?answer=43264#post-id-43264Comment by Pedro for <p>The source can be found in the <code>SAGE_ROOT/src/sage/repl/</code> directory, in particular <code>interpreter.py</code>.</p>
<p><strong>EDIT</strong> : if you want to run ipython interpreter from Python's one, (and then Sage the way you suggested), you can do:</p>
<pre><code>&gt;&gt;&gt; import IPython
&gt;&gt;&gt; IPython.embed()
Python 2.7.15 (default, Jul 23 2018, 10:39:00)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -&gt; Introduction and overview of IPython's features.
%quickref -&gt; Quick reference.
help -&gt; Python's own help system.
object? -&gt; Details about 'object', use 'object??' for extra details.
In [1]: %load_ext sage
In [2]: 2^2
Out[2]: 4
In [3]: sin(x)
Out[3]: sin(x)
</code></pre>
http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43374#post-id-43374Meanwhile there is no solution:
>>> exec(preparse(r"""
... a = 2^3
... print a
... """))
8
>>> a
8
>>> exit
> py$a
8
or also:
>>> load("some.sage")Mon, 13 Aug 2018 19:43:48 -0500http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43374#post-id-43374Comment by Emmanuel Charpentier for <p>The source can be found in the <code>SAGE_ROOT/src/sage/repl/</code> directory, in particular <code>interpreter.py</code>.</p>
<p><strong>EDIT</strong> : if you want to run ipython interpreter from Python's one, (and then Sage the way you suggested), you can do:</p>
<pre><code>&gt;&gt;&gt; import IPython
&gt;&gt;&gt; IPython.embed()
Python 2.7.15 (default, Jul 23 2018, 10:39:00)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -&gt; Introduction and overview of IPython's features.
%quickref -&gt; Quick reference.
help -&gt; Python's own help system.
object? -&gt; Details about 'object', use 'object??' for extra details.
In [1]: %load_ext sage
In [2]: 2^2
Out[2]: 4
In [3]: sin(x)
Out[3]: sin(x)
</code></pre>
http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43274#post-id-43274That works. But it is not enough for what I want to do.
The point of `reticulate` is that it creates a *permanent* Python session, that can be left (with `exit`) and **resumed** at a later point, without loss of the previous work (which can be accessed from R, BTW : that's another big part of the point !).
When one launches an IPython session, all works. However,when one *resumes* this session, one lands in a *Python* session ; therefore one has to re-`IPython.embed()`, and there, the session is a *Python* session : `2^3` returns `1` no matter what. Even `reload_ext sage` won't re-establish the pre-parsing.
What I want to do is to establish all the specifics of a Sage terminal session *without* launching an IPython sesion. That means importing `sage.all`, establish pre-parsing, etc...Mon, 06 Aug 2018 11:17:44 -0500http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43274#post-id-43274Comment by tmonteil for <p>The source can be found in the <code>SAGE_ROOT/src/sage/repl/</code> directory, in particular <code>interpreter.py</code>.</p>
<p><strong>EDIT</strong> : if you want to run ipython interpreter from Python's one, (and then Sage the way you suggested), you can do:</p>
<pre><code>&gt;&gt;&gt; import IPython
&gt;&gt;&gt; IPython.embed()
Python 2.7.15 (default, Jul 23 2018, 10:39:00)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -&gt; Introduction and overview of IPython's features.
%quickref -&gt; Quick reference.
help -&gt; Python's own help system.
object? -&gt; Details about 'object', use 'object??' for extra details.
In [1]: %load_ext sage
In [2]: 2^2
Out[2]: 4
In [3]: sin(x)
Out[3]: sin(x)
</code></pre>
http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43270#post-id-43270I edited my answer.Mon, 06 Aug 2018 08:59:35 -0500http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43270#post-id-43270Comment by Emmanuel Charpentier for <p>The source can be found in the <code>SAGE_ROOT/src/sage/repl/</code> directory, in particular <code>interpreter.py</code>.</p>
<p><strong>EDIT</strong> : if you want to run ipython interpreter from Python's one, (and then Sage the way you suggested), you can do:</p>
<pre><code>&gt;&gt;&gt; import IPython
&gt;&gt;&gt; IPython.embed()
Python 2.7.15 (default, Jul 23 2018, 10:39:00)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -&gt; Introduction and overview of IPython's features.
%quickref -&gt; Quick reference.
help -&gt; Python's own help system.
object? -&gt; Details about 'object', use 'object??' for extra details.
In [1]: %load_ext sage
In [2]: 2^2
Out[2]: 4
In [3]: sin(x)
Out[3]: sin(x)
</code></pre>
http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43267#post-id-43267Thanks ! But I have trouble finding what I should call to init the Sage session. I'm a bit out of my bepth here...Mon, 06 Aug 2018 08:27:39 -0500http://ask.sagemath.org/question/43258/is-there-a-way-to-start-a-sage-session-from-a-session-of-its-python-interpreter/?comment=43267#post-id-43267