<> == Tips for Using Sage == * '''Vim filetypes''' To get Vim to use Python syntax highlighting, indentation, and so on for {{{.sage}}} files, put the following in {{{$VIM/filetype.vim}}}: {{{ augroup filetypedetect au! BufRead,BufNewFile *.sage,*.spyx,*.pyx setfiletype python augroup END }}} * '''Terminal colorscheme''' The colorscheme of the terminal can be changed as in IPython. For a colorscheme that fits a dark background one can use {{{ %colors Linux }}} To automatically execute it in SageMath start-up, you can create a file called `50-color.ipy` in `$DOT_SAGE/ipython-X.Y.Z/profile_default/startup` with the above line. One can also use the many different Pygments styles that new IPython versions have (included since SageMath 7.4) in your [[https://ipython.readthedocs.io/en/stable/config/intro.html|config file]]. See also [[https://ipython.readthedocs.io/en/stable/config/details.html#terminal-colors|Terminal Colors]] in the IPython documentation for more details. * Add [[http://mvngu.wordpress.com/2009/05/21/add-colour-to-your-sage-session/ | color to your command line ]] by uncommenting the color scheme you like in {{{$HOME/.sage/ipython/ipythonrc}}}. * '''Sage development using Vim''' The lines below can be added to the {{{.vimrc}}} file. The makeprg line allows one to build and test a source file from within Vim simply by typing {{{:make}}}. {{{ """""" " Sage settings (from Franco Saliola) autocmd BufRead,BufNewFile *.sage,*.pyx,*.spyx set filetype=python autocmd Filetype python set tabstop=4|set shiftwidth=4|set expandtab autocmd FileType python set makeprg=sage\ -b\ &&\ sage\ -t\ % }}} * '''Autocompletion for the bash shell''' So, you'd like bash to autocomplete Sage files, but you want to somehow exclude those {{{.py}}} files that have been autogenerated by Sage. Here's a snippet that will do that for you. Put this somewhere where it will get sourced, such as your {{{.bashrc}}}, {{{.bash_profile}}}, or whatever. Often bash is set up so that it will automatically pull in completions defined in {{{~/.bash_completion}}}. Here's the code: {{{ _sagecomp() { local cur files COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" files="$(compgen -o dirnames -X '!*.@(sage|?(s)pyx|py)' -f ${cur})" if [[ "$files" ]] then COMPREPLY=($(egrep -L '^# This file was \*autogenerated\* from the file .*\.sage' $files)) fi } complete -F _sagecomp sage }}} . See [[bash-completion]] for more complicated completion and [[http://www.debian-administration.org/articles/317|this article]] for more information about completion in general. --DanDrake * '''Determining if your script is attached or being run at the command line''' Say you have a script in which some functions are defined. Sometimes you want to attach the file to an interactive session and play with the functions, uh, interactively. But perhaps you'd like the script to do something else when run at the command line; perhaps it should calculate a bunch of values and save them to a file. In other words, you want the standard {{{if __name__ == '__main__'}}} Python idiom. How to get that? One way is to look at {{{sys.argv}}} and compare it to {{{SAGE_TMP}}}. When you attach a file to a running session, the preparsed version of the script gets put into the directory {{{SAGE_TMP}}} (in recent versions of Sage, at least) and {{{sys.argv}}} for that script starts with {{{SAGE_TMP}}}. Put the following code in your script: {{{#!python numbers=none import sys import os def is_attached(): return os.path.split(sys.argv[0])[0] == os.path.normpath(SAGE_TMP) }}} Another way which is perhaps nicer but not quite as portable is to define a boolean in the file {{{$HOME/init.sage}}}, which is sourced on interactive startup; then you could test for that boolean in your script. --DanDrake * '''Cython''': Note that if you have an `.spyx` file that you want to `load` or `attach` in `sage`, and you want to link it against some library (GMP, NTL, etc), or possibly compile with `g++` instead of `gcc`, you need to add something like the following to the top of the file: {{{ #clang c++ #clib ntl }}} For more info, see the help for the function `sage.misc.cython.pyx_preparse`. * '''[[devel/FunctionalProgramming | Functional programming]]''' --- This tutorial discusses some techniques of functional programming that might be of interest to mathematicians or people who use Python for scientific computation. * '''[[Cantor]]''' --- a front-end for Sage. == Accessing Sage through a firewall using an SSH proxy server == Suppose you have Sage running on your office computer at your university, but the university firewall prevents you from accessing it from home. Suppose also that you can SSH into a server at the university from which you *can* see your office computer. There is a simple procedure that allows you to use the SSH server to enable Firefox or other web browsers to access your work computer. 1. Log in to the university ssh server using the following command: {{{ ssh -D 10000 ssh.university.edu}}} (The {{{-D 10000}}} sets up a SOCKS proxy through the SSH connection.) 1. Configure your browser to use the proxy. A very simple, easy way to do this in Firefox is to install FoxyProxy, which lets you pick proxy servers per tab. Just install FoxyProxy and click on the icon in the lower left in the status bar. Configure it to use the SOCKS proxy with host "localhost" and port "10000". 1. Log in to your office computer (for example, you could log in through your ssh connection above). Start up the Sage notebook, but make sure that you can access the Sage notebook from ssh.university.edu (e.g., use the interface keyword when starting up the notebook) 1. Now just direct your browser to your office computer address. Any addresses you type into the browser are sent over the SSH connection first, so you should be able to see your office computer as if you were browsing from the SSH server.