Ask Your Question
1

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

asked 2018-07-10 09:25:30 +0100

yoyostein gravatar image

updated 2018-07-10 09:52:48 +0100

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

Comments

You can find instructions for configuring PyCharm with sage here: https://ask.sagemath.org/question/397...

Iguananaut gravatar imageIguananaut ( 2020-02-04 15:24:15 +0100 )edit

2 Answers

Sort by » oldest newest most voted
12

answered 2018-07-10 10:42:54 +0100

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 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

edit flag offensive delete link more

Comments

2

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

kcrisman gravatar imagekcrisman ( 2018-07-12 19:11:57 +0100 )edit
1

answered 2020-01-15 16:58:43 +0100

56phil gravatar image

updated 2020-02-04 14:25:45 +0100

If one wants to use Sage in Jupyterlab on macOS Catalina 10.15.1 (19B88), this worked for me:

1. download Sage binaries
2. unpack download
3. move SAGE_ROOT to the desired location
4. set SAGE_ROOT and any other environment variables (e.g. SAGE_KEEP_BUILT_SPKGS) in ~/.zshrc
    (a) export SAGE_KEEP_BUILT_SPKGS="yes" 
    (b) export SAGE_NUM_THREADS=3
    (c) export SAGE_ROOT="$HOME/SageMath"
5. Update $PATH by editing /etc/paths Note: You’ll need to use SUDO.
    <$HOME>/SageMath/local/bin
    /usr/local/bin
    /usr/local/sbin
    /usr/bin
    /bin
    /usr/sbin
    /sbin
    <$HOME>/SageMath
6. cd $SAGE_ROOT && ./sage -i openssl && ./sage -f python3 && make ssl && sage –pip3 install jupyterlab

when all this is done, sage -n jupyterlab, should work.

Update: Avoid updating Python packages using pip. I broke the sage kernal in jupyterlab doing this.

edit flag offensive delete link more

Comments

1

This has been discussed a few times on sage-devel. Given the ongoing switch to Python 3, which shall enable the upgrade of a lot of Python libraries in Sage (namely ipython, jupyterund so weider...), the addition of an "official" Jupyterlab interface is probably a question of time...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-01-15 18:55:09 +0100 )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

4 followers

Stats

Asked: 2018-07-10 09:25:30 +0100

Seen: 8,693 times

Last updated: Feb 04 '20