Ask Your Question

Revision history [back]

Ways to use SageMath

On my computer

  • the sage REPL or command-line-interface
    • launch Sage in a terminal, wait for the sage: prompt
    • type a command then hit the ENTER or RETURN key, get output
  • the Jupyter Notebook with the Sage kernel
    • ways to launch it
      • in a terminal, type sage -n jupyter
      • or use a launcher such as
        • the macOS "Sage app"
        • the Windows launcher
        • a desktop launcher for Linux
  • JupyterLab
    • install using sage --pip install jupyterlab
    • then in the terminal run sage -n jupyterlab
  • the legacy SageNB notebook
    • ways to launch it
      • in a terminal, type sage -n sagenb
      • or in the Sage REPL, type notebook()
      • or use a launcher
  • run a command with sage -c
    • example: in a terminal, run sage -c "print(2 + 2)"
  • run a file with extension .py or .sage
    • put the commands in a file such as myfile.sage or myfile.py
    • in the terminal, run sage myfile.sage or sage myfile.py
    • difference between .sage and .py files
      • for .sage files, the Sage preparser will be used
      • for .py files, the Sage preparser will not be used
  • use external files
    • commands
      • load, runfile, attach, %load, %runfile, %attach
      • examples
        • suppose the file myfile.sage contains def sq(a): return a*a
        • in the terminal, run sage -c "load('myfile.sage'); print(sq(2))"
  • from another program
  • use OpenMath and SCSCP to exchange mathematical objects with other mathematics software; work in progress, see

Online

  • SageCell
    • one-off computations on SageCell page
    • use it to include compute cells in a webpage
    • use it with PreTeXt
  • CoCalc
    • see dedicated item below
  • JupyterHub
    • there are some deployments of JupyterHub which offer the Sage kernel
  • mybinder
    • there are some mybinder instances that include Sage
  • Sage Notebook servers
    • there some deployments of the SageNB notebook; the one formerly at sagenb.org is no longer active

CoCalc

  • in a CoCalc terminal, run sage for the Sage REPL
  • use sage_select to select which version of Sage to use by default
  • CoCalc Sage worksheets (.sagews)
  • Jupyter Notebook worksheets (.ipynb)
    • using CoCalc's version of the Jupyter Notebook
    • using the Classic Jupyter Notebook
      • go to Project preferences and launch Classic Jupyter Notebook; this will open a separate browser tab which will connect to your project using the classic Jupyter Notebook protocol;
      • allows to use Jupyter Notebook extensions that are not yet implemented in "CoCalc Jupyter", such as widgets, RISE, ...

Sage: file formats, data formats

  • common file extensions
    • .py, .sage, .pyx, .spyx, ...
    • .sobj
    • SageNB notebook worksheets: .sws
    • CoCalc Sage worksheets: .sagews
    • .rst, .txt, .html
  • converters
    • rst2ipynb, ...
  • viewing, saving, copying, transferring worksheets

Run shell commands from within sage

  • any command starting with ! is executed as a shell command
  • many basic shell commands are available in IPython without !: ls, cd, pwd...
  • see also the Python modules os and sys

Read from, write to, append to file

  • this uses standard Python functionality
  • 'r' for read, 'w' for write (overwriting file), 'a' for append
  • read from file, all at once or line by line

    with open('/path/to/file.txt', 'r') as f:
        s = f.read()
    
    with open('/path/to/file.txt', 'r') as f:
        for line in f:
            <do something with line>
    
  • write to file, bit by bit or line by line

    with open('/path/to/file.txt', 'a') as f:
        f.write('blah')
        f.writelines(['haha', 'hehe', 'hihi'])
    
  • see also the csv module for "comma-separated-values" data files