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)[source]¶
- Bases: - object- Initialize the Sage plugin. - static all_globals()[source]¶
- 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' ...> - >>> from sage.all import * >>> from sage.repl.ipython_extension import SageCustomizations >>> SageCustomizations.all_globals() <module 'sage.all_cmdline' ...> 
 
- class sage.repl.ipython_extension.SageJupyterCustomizations(shell=None)[source]¶
- Bases: - SageCustomizations- static all_globals()[source]¶
- 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' ...> - >>> from sage.all import * >>> from sage.repl.ipython_extension import SageJupyterCustomizations >>> SageJupyterCustomizations.all_globals() <module 'sage.repl.ipython_kernel.all_jupyter' ...> 
 
- class sage.repl.ipython_extension.SageMagics(**kwargs: Any)[source]¶
- Bases: - Magics- attach(s)[source]¶
- 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
 - See also - This is the same as - attach().- EXAMPLES: - sage: from sage.repl.interpreter import get_test_shell sage: shell = get_test_shell() sage: from tempfile import NamedTemporaryFile as NTF sage: with NTF(mode='w+t', suffix='.py', delete=False) as f: ....: _ = f.write('a = 2\n') sage: shell.run_cell('%attach ' + f.name) sage: shell.run_cell('a') 2 sage: sleep(1) # filesystem timestamp granularity sage: with open(f.name, 'w') as f: _ = f.write('a = 3\n') - >>> from sage.all import * >>> from sage.repl.interpreter import get_test_shell >>> shell = get_test_shell() >>> from tempfile import NamedTemporaryFile as NTF >>> with NTF(mode='w+t', suffix='.py', delete=False) as f: ... _ = f.write('a = 2\n') >>> shell.run_cell('%attach ' + f.name) >>> shell.run_cell('a') 2 >>> sleep(Integer(1)) # filesystem timestamp granularity >>> with open(f.name, '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 ... modified at ... ### sage: shell.run_cell('a') 3 sage: shell.run_cell('detach(%r)' % f.name) sage: shell.run_cell('attached_files()') [] sage: os.remove(f.name) sage: shell.quit() - >>> from sage.all import * >>> shell.run_cell('from sage.repl.attach import reload_attached_files_if_modified') >>> shell.run_cell('reload_attached_files_if_modified()') ### reloading attached file ... modified at ... ### >>> shell.run_cell('a') 3 >>> shell.run_cell('detach(%r)' % f.name) >>> shell.run_cell('attached_files()') [] >>> os.remove(f.name) >>> shell.quit() 
 - crun(s)[source]¶
- 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() - >>> from sage.all import * >>> from sage.repl.interpreter import get_test_shell >>> shell = get_test_shell() >>> 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 ... >>> shell.quit() 
 - cython(line, cell)[source]¶
- Cython cell magic. - This is syntactic sugar on the - cython_compile()function.- Note that there is also the - %%cythoncell magic provided by Cython, which can be loaded with- %load_ext cython, see Cython documentation for more details. The semantic is slightly different from the version provided by Sage.- INPUT: - line– parsed as keyword arguments. The allowed arguments are:- --verbose N/- -v N
- --compile-message
- --use-cache
- --create-local-c-file
- --annotate
- --view-annotate
- --sage-namespace
- --create-local-so-file
- --no-compile-message,- --no-use-cache, etc.
 - See - cython()for details.- If - --view-annotateis given, the annotation is either displayed inline in the Sage notebook or opened in a new web browser, depending on whether the Sage notebook is used.- You can override the selection by specifying - --view-annotate=webbrowseror- --view-annotate=displayhtml.
- cell– string; the Cython source code to process
 - OUTPUT: none; the Cython code is compiled and loaded - EXAMPLES: - sage: # needs sage.misc.cython sage: from sage.repl.interpreter import get_test_shell sage: shell = get_test_shell() sage: shell.run_cell( ....: ''' ....: %%cython -v1 --annotate --no-sage-namespace ....: def f(): ....: print('test') ....: ''') Compiling ....pyx because it changed. [1/1] Cythonizing ....pyx sage: f() test - >>> from sage.all import * >>> # needs sage.misc.cython >>> from sage.repl.interpreter import get_test_shell >>> shell = get_test_shell() >>> shell.run_cell( ... ''' ... %%cython -v1 --annotate --no-sage-namespace ... def f(): ... print('test') ... ''') Compiling ....pyx because it changed. [1/1] Cythonizing ....pyx >>> f() test 
 - display(args)[source]¶
- A magic command to switch between simple display and ASCII art display. - args– string. See- sage.repl.rich_output.preferencesfor allowed values. If the mode is- ascii_art, it can optionally be followed by a width.
 - 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') - >>> from sage.all import * >>> from sage.repl.interpreter import get_test_shell >>> shell = get_test_shell() >>> 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')") # needs sage.symbolic sage: shell.run_cell('sum(i^2*x^i, i, 0, 10)') # needs sage.symbolic 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 - >>> from sage.all import * >>> shell.run_cell("i = var('i')") # needs sage.symbolic >>> shell.run_cell('sum(i^2*x^i, i, 0, 10)') # needs sage.symbolic 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)') # needs sage.symbolic 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 - >>> from sage.all import * >>> shell.run_cell('%display text plain') >>> shell.run_cell('%display plain') # shortcut for "text plain" >>> shell.run_cell('sum(i^2*x^i, i, 0, 10)') # needs sage.symbolic 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()') # needs sage.combinat [ [ 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()') # needs sage.combinat [ [ [ 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 ] - >>> from sage.all import * >>> shell.run_cell('%display ascii_art') >>> shell.run_cell('StandardTableaux(4).list()') # needs sage.combinat [ [ 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 , <BLANKLINE> 1 ] 1 2 2 ] 3 3 ] 4 , 4 ] >>> shell.run_cell('%display ascii_art 50') >>> shell.run_cell('StandardTableaux(4).list()') # needs sage.combinat [ [ [ 1 3 4 1 2 4 1 2 3 [ 1 2 3 4, 2 , 3 , 4 , <BLANKLINE> 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 - >>> from sage.all import * >>> shell.run_cell('%display text latex') >>> shell.run_cell('1/2') 1/2 - Switch back: - sage: shell.run_cell('%display default') - >>> from sage.all import * >>> shell.run_cell('%display default') - Switch graphics to default to vector or raster graphics file formats: - sage: shell.run_cell('%display graphics vector') - >>> from sage.all import * >>> shell.run_cell('%display graphics vector') 
 - fortran(line, cell)[source]¶
- 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: # needs numpy 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 ...> 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.]) - >>> from sage.all import * >>> # needs numpy >>> from sage.repl.interpreter import get_test_shell >>> shell = get_test_shell() >>> 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 ... ''') >>> fib <fortran ...> >>> from numpy import array >>> a = array(range(Integer(10)), dtype=float) >>> fib(a, Integer(10)) >>> a array([ 0., 1., 1., 2., 3., 5., 8., 13., 21., 34.]) 
 - iload(args)[source]¶
- 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 - >>> from sage.all import * >>> ip = get_ipython() # not tested: works only in interactive shell >>> ip.magic_iload('/dev/null') # not tested: works only in interactive shell Interactively loading "/dev/null" # not tested: works only in interactive shell 
 - magics = {'cell': {'cython': 'cython', 'fortran': 'fortran'}, 'line': {'attach': 'attach', 'crun': 'crun', 'display': 'display', 'iload': 'iload', 'runfile': 'runfile'}}¶
 - registered = True¶
 - runfile(s)[source]¶
- 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
 - See also - This is the same as - load().- 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() - >>> from sage.all import * >>> import os >>> from sage.repl.interpreter import get_test_shell >>> from sage.misc.temporary_file import tmp_dir >>> shell = get_test_shell() >>> tmp = os.path.join(tmp_dir(), 'run_cell.py') >>> with open(tmp, 'w') as f: ... _ = f.write('a = 2\n') >>> shell.run_cell('%runfile '+tmp) >>> shell.run_cell('a') 2 >>> shell.quit()