Sage’s IPython Extension

A Sage extension which adds sage-specific features:

  • magics

    • %crun

    • %runfile

    • %attach

    • %display

    • %mode (like %maxima, etc.)

    • %%cython

    • %%fortran

  • preparsing of input

  • loading Sage library

  • running init.sage

  • changing prompt to Sage prompt

  • Display hook

class sage.repl.ipython_extension.SageCustomizations(shell=None)

Bases: object

Initialize the Sage plugin.

static all_globals()

Return a Python module containing all globals which should be made available to the user.

EXAMPLES:

sage: from sage.repl.ipython_extension import SageCustomizations
sage: SageCustomizations.all_globals()
<module 'sage.all_cmdline' ...>
init_environment()

Set up Sage command-line environment

init_inspector()
init_line_transforms()

Set up transforms (like the preparser).

register_interface_magics()

Register magics for each of the Sage interfaces

run_init()

Run Sage’s initial startup file.

class sage.repl.ipython_extension.SageJupyterCustomizations(shell=None)

Bases: sage.repl.ipython_extension.SageCustomizations

static all_globals()

Return a Python module containing all globals which should be made available to the user when running the Jupyter notebook.

EXAMPLES:

sage: from sage.repl.ipython_extension import SageJupyterCustomizations
sage: SageJupyterCustomizations.all_globals()
<module 'sage.repl.ipython_kernel.all_jupyter' ...>
class sage.repl.ipython_extension.SageMagics(shell=None, **kwargs)

Bases: IPython.core.magic.Magics

attach(*args, **kw)

Attach the code contained in the file s.

This is designed to be used from the command line as %attach /path/to/file.

  • s – string. The file to be attached

EXAMPLES:

sage: import os
sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: tmp = os.path.normpath(os.path.join(SAGE_TMP, 'run_cell.py'))
sage: with open(tmp, 'w') as f: _ = f.write('a = 2\n')
sage: shell.run_cell('%attach ' + tmp)
sage: shell.run_cell('a')
2
sage: sleep(1)  # filesystem timestamp granularity
sage: with open(tmp, 'w') as f: _ = f.write('a = 3\n')

Note that the doctests are never really at the command prompt, so we call the input hook manually:

sage: shell.run_cell('from sage.repl.attach import reload_attached_files_if_modified')
sage: shell.run_cell('reload_attached_files_if_modified()')
### reloading attached file run_cell.py modified at ... ###

sage: shell.run_cell('a')
3
sage: shell.run_cell('detach(%r)'%tmp)
sage: shell.run_cell('attached_files()')
[]
sage: os.remove(tmp)
sage: shell.quit()
crun(*args, **kw)

Profile C function calls

INPUT:

  • s – string. Sage command to profile.

EXAMPLES:

sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.run_cell('%crun sum(1/(1+n^2) for n in range(100))')   # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...
Using local file ...
Using local file ...
sage: shell.quit()
cython(*args, **kw)

Cython cell magic

This is syntactic sugar on the cython_compile() function.

INPUT:

  • line – ignored.

  • cell – string. The Cython source code to process.

OUTPUT:

None. The Cython code is compiled and loaded.

EXAMPLES:

sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.run_cell('''
....: %%cython
....: def f():
....:     print('test')
....: ''')
sage: f()
test
display(*args, **kw)

A magic command to switch between simple display and ASCII art display.

How to use: if you want to activate the ASCII art mode:

sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.run_cell('%display ascii_art')

That means you do not have to use ascii_art() to get an ASCII art output:

sage: shell.run_cell("i = var('i')")
sage: shell.run_cell('sum(i^2*x^i, i, 0, 10)')
     10       9       8       7       6       5       4      3      2
100*x   + 81*x  + 64*x  + 49*x  + 36*x  + 25*x  + 16*x  + 9*x  + 4*x  + x

Then when you want to return to ‘textual mode’:

sage: shell.run_cell('%display text plain')
sage: shell.run_cell('%display plain')        # shortcut for "text plain"
sage: shell.run_cell('sum(i^2*x^i, i, 0, 10)')
100*x^10 + 81*x^9 + 64*x^8 + 49*x^7 + 36*x^6 + 25*x^5 + 16*x^4 + 9*x^3 + 4*x^2 + x

Sometime you could have to use a special output width and you could specify it:

sage: shell.run_cell('%display ascii_art')
sage: shell.run_cell('StandardTableaux(4).list()')
[
[                                                                  1  4    1  3
[                 1  3  4    1  2  4    1  2  3    1  3    1  2    2       2
[   1  2  3  4,   2      ,   3      ,   4      ,   2  4,   3  4,   3   ,   4   ,

           1 ]
   1  2    2 ]
   3       3 ]
   4   ,   4 ]
sage: shell.run_cell('%display ascii_art 50')
sage: shell.run_cell('StandardTableaux(4).list()')
[
[
[                 1  3  4    1  2  4    1  2  3
[   1  2  3  4,   2      ,   3      ,   4      ,

                                          1 ]
                  1  4    1  3    1  2    2 ]
  1  3    1  2    2       2       3       3 ]
  2  4,   3  4,   3   ,   4   ,   4   ,   4 ]

As yet another option, typeset mode. This is used in the emacs interface:

sage: shell.run_cell('%display text latex')
sage: shell.run_cell('1/2')
1/2

Switch back:

sage: shell.run_cell('%display default')

Switch graphics to default to vector or raster graphics file formats:

sage: shell.run_cell('%display graphics vector')
fortran(*args, **kw)

Fortran cell magic.

This is syntactic sugar on the fortran() function.

INPUT:

  • line – ignored.

  • cell – string. The Cython source code to process.

OUTPUT:

None. The Fortran code is compiled and loaded.

EXAMPLES:

sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.run_cell('''
....: %%fortran
....: C FILE: FIB1.F
....:       SUBROUTINE FIB(A,N)
....: C
....: C     CALCULATE FIRST N FIBONACCI NUMBERS
....: C
....:       INTEGER N
....:       REAL*8 A(N)
....:       DO I=1,N
....:          IF (I.EQ.1) THEN
....:             A(I) = 0.0D0
....:          ELSEIF (I.EQ.2) THEN
....:             A(I) = 1.0D0
....:          ELSE
....:             A(I) = A(I-1) + A(I-2)
....:          ENDIF
....:       ENDDO
....:       END
....: C END FILE FIB1.F
....: ''')
sage: fib
<fortran object>
sage: from numpy import array
sage: a = array(range(10), dtype=float)
sage: fib(a, 10)
sage: a
array([  0.,   1.,   1.,   2.,   3.,   5.,   8.,  13.,  21.,  34.])
iload(*args, **kw)

A magic command to interactively load a file as in MAGMA.

  • args – string. The file to be interactively loaded

Note

Currently, this cannot be completely doctested as it relies on raw_input().

EXAMPLES:

sage: ip = get_ipython()           # not tested: works only in interactive shell
sage: ip.magic_iload('/dev/null')  # not tested: works only in interactive shell
Interactively loading "/dev/null"  # not tested: works only in interactive shell
runfile(*args, **kw)

Execute the code contained in the file s.

This is designed to be used from the command line as %runfile /path/to/file.

  • s – string. The file to be loaded.

EXAMPLES:

sage: import os
sage: from sage.repl.interpreter import get_test_shell
sage: from sage.misc.temporary_file import tmp_dir
sage: shell = get_test_shell()
sage: tmp = os.path.join(tmp_dir(), 'run_cell.py')
sage: with open(tmp, 'w') as f:
....:     _ = f.write('a = 2\n')
sage: shell.run_cell('%runfile '+tmp)
sage: shell.run_cell('a')
2
sage: shell.quit()
sage.repl.ipython_extension.load_ipython_extension(*args, **kwargs)

Load the extension in IPython.

sage.repl.ipython_extension.run_once(func)

Runs a function (successfully) only once.

The running can be reset by setting the has_run attribute to False