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.Wed, 14 Jul 2021 15:24:39 +0200set_trace() analog for Sagehttps://ask.sagemath.org/question/58014/set_trace-analog-for-sage/ In sage one can use `import pdb;pdb.set_trace()` or in case of tests: `import pytest; pytest.set_trace()`. But these commands invoke pure python console without Sage improvements.
Are there something like `sage.set_trace()` command which opens Sage REPL instead if Python REPL?petRUShkaWed, 14 Jul 2021 15:24:39 +0200https://ask.sagemath.org/question/58014/REPL-like outputhttps://ask.sagemath.org/question/46294/repl-like-output/Hello, Sage Community.
If I run the sage REPL in a terminal, and execute something like
for i in range(4):
print(i^2)
then we all know that Sage prints
0
1
4
9
In this case, I could create a class to intercept standard output to a variable, like:
class verboseBuffer:
def __init__(self):
self.buffer = ''
def write(self, s):
self.buffer += s
Then I just make
_verbOut_ = verboseBuffer()
sys.stdout = _verbOut_
and if I run the first chunk of code, then the integers will be stored in `_verbOut_.buffer`, instead of being printed to standard output as before. I finally can do
sys.stdout = sys.__stdout__
print(_verbOut_.buffer)
and this prints the contents of `_verbOut_.buffer`, i.e., the list at the beginning.
Now consider the same loop without the `print`:
for i in range(4):
i^2
Executed in the REPL, Sage will print exactly the same as before (even if I haven't specified to `print`), and I could do the same thing to redirect the standard output to a variable.
Now, on the other hand, I have a huge file for which I would like to print everything in the same way it is printed in the REPL. I can't add `print` to every single part of the file, so I have tried to redirect output to a variable as before, and print it in the last line. The problem is that, things like the last chunk of code, without an explicit `print` won't be stored in `_verbOut_.buffer` at the end of the execution.
My questions are:
1. Why does the Sage REPL prints all computations not assigned to a variable, like the `i^2` in the loop, but that doesn't happen if the code is in a file?
2. How can I obtain a REPL-like output without having to add `print` to everything in my long file?dsejasSun, 21 Apr 2019 05:10:21 +0200https://ask.sagemath.org/question/46294/Is there a way to start a Sage session from a session of its Python interpreter ?https://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...Emmanuel CharpentierSun, 05 Aug 2018 21:11:20 +0200https://ask.sagemath.org/question/43258/Launch Jupyter notebook from Sage?https://ask.sagemath.org/question/31088/launch-jupyter-notebook-from-sage/When starting the SageMath REPL, the user is reminded how to launch the Sage Notebook:
$ sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath Version 6.9, Release Date: 2015-10-10 │
│ Type "notebook()" for the browser-based notebook interface. │
│ Type "help()" for help. │
└────────────────────────────────────────────────────────────────────┘
sage:
Is there a command to launch the Jupyter Notebook (formerly IPython Notebook) from Sage?slelievreThu, 26 Nov 2015 14:51:36 +0100https://ask.sagemath.org/question/31088/