Ask Your Question

Other than "Jupyter Notebook", is there any other way to use Sage?

asked 2018-07-10 02:25:30 -0500

yoyostein gravatar image

updated 2018-07-10 02:52:48 -0500

I am searching for a "compiler" way to use Sage.

Something like TeXShop, I write everything in a single file and press compile to get the results.

Is there an easy way to accomplish this?

Currently I am using the "Notebook" form of Sage which is neat but kind of gets in the way of more complicated programming.

(To be more clear, I am looking for something like an IDE "Integrated development environment", something like PyCharm for Python.)

Thanks a lot.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-07-10 03:42:54 -0500

slelievre gravatar image

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
    • in the terminal, run sage myfile.sage or sage
    • 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


  • 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 is no longer active


  • 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 =
    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.writelines(['haha', 'hehe', 'hihi'])
  • see also the csv module for "comma-separated-values" data files

edit flag offensive delete link more


This is ridiculously comprehensive and should be on the main Sage website in a prominent place ...

kcrisman gravatar imagekcrisman ( 2018-07-12 12:11:57 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-07-10 02:25:30 -0500

Seen: 31 times

Last updated: Jul 10