-
Notifications
You must be signed in to change notification settings - Fork 220
Programming
This FAQ is all about programming your own software (C, R, Fortran, Java, Octave, etc...) from SageMathCloud. A highly related page is the FAQ for Utilizing External Tools (and Jupyter) from SageMathCloud.
Remember: if you don't find what you need, or if you'd like to ask a question, then please email [email protected] at any time. We'd love to hear from you! Please include a link (the URL address in your browser) to any relevant project or document, as part of your email.
List of Questions:
-
I would like to create, compile and run a Fortran F90 program.
-
Stuff about Python
-
Stuff about R
-
Stuff about LaTeX
-
Working with the Shell/Terminal
-
I would like that all of my worksheets know where to find a given module that I write or install
-
How can I tell if my code is running in a SageMathCloud Worksheet (a sagews file)?
-
How do I access functionality specific to SageMathCloud Worksheets (sagews files)?
-
I want to run Sage locally, on my own machine. How do I do that?
-
I want to use my own custom built Sage binaries on SageMathCloud.
-
I want to use my own custom built-from-source copy of Sage. (Warning: this takes hours.)
-
How do I install some software into my own "Anaconda" environment in SageMathCloud?
-
Large-Scale Scientific Computations:
- I want to start long-running numerically intensive computations on SageMathCloud. What are the current limitations?
- I need some scratch space
- I want to see all processes running in my project
- I want to know how much memory I am using
- How do I raise the limit on the number of output messages per cell in a Sage worksheet?
Click here to View a Video about this Question.
-
Click +New, type a filename ending in ".c", e.g.,
foo.c
, and click "Create File" (or just press return). -
Paste this code into the file:
#include<stdio.h>
int main(void) {
printf("Hello World\n");
printf("this is SageMathCloud!\n");
}
- Open a terminal by clicking +New, clicking "Command Line Terminal" (or typing a filename ending in .term), and type
gcc foo.c
. Finally, run the program by typing./a.out
.
Click here to View a Video about this Question.
-
Create a file
sum.f90
by clicking +New, which I copy and paste from http://en.wikibooks.org/wiki/Fortran/Fortran_examples#Area_Of_a_Triangle_program -
Create a terminal and type
gfortran sum.f90
-
Run the program by typing
./a.out
Click here to View a Video about this Question.
- Create a file
HelloWorld.java
containing
public class HelloWorld {
public static void main (String[] args) {
System.out.println ("Hello World!");
}
}
-
Create a terminal and run
javac HelloWorld.java
to compile your program. -
Run
java HelloWorld
to see the output.
I've put an example IPython octave notebook and a Sage octave worksheet here:
Unfortunately, neither IPython nor Sage worksheets in Octave mode are "rock solid". For something that is rock solid, type "+New", click terminal, and type "octave" on the command line, and this should work well. You can type "+New", enter a filename that ends with .m, and edit it, then load it into the command line (by typing the filename without the extension).
You might also find this useful: https://github.com/sagemath/cloud/issues/97
-
Click +New, type a filename, then click the "Sage Worksheet" button.
-
To evaluate code using Julia, begin the cell with
%julia
, type the code, then press shift+enter. -
Type
%default_mode julia
in a cell and press shift+enter; now all cells will be evaluated using Julia by default. If you need to switch back, use%default_mode sage
(or%sage
to just switch back for one cell).
With full network access enabled, you can download and compile Python 3 this way. Last line sets a symlink to make it your default!
apt-get source python3.4-dev
cd python3.4-3.4.0/
./configure --prefix=$HOME
make
make install
cd ..
pip3 install numpy
pip3 install scipy
pip3 install matplotlib
pip3 install ipython
pip3 install pyzmq
pip3 install jinja2
pip3 install tornado
ln -s ~/bin/python3 ~/bin/python
By default, Sage parses the input commands and replaces some elements with its own parts and also adds some syntactic sugar. For example, an integer like 234
is translated to Integer(234)
in order to be more powerful and live as a part of Sage. To avoid this behaviour, either append an r
to the number, like 234r
or change the mode of the cell to Python by adding %python
at the top. You can also switch to pure Python mode by default via %default_mode python
. Alternatively, you can type Integer=int
and possibly also RealNumber=float
.
Question: How can I install Python packages from PyPi using pip?
WARNING: Due to people launching attacks from SMC, using pip install --user
will not work unless you pay to enable network access for your project.
Let's suppose the package is called ggplot
. Create a new Terminal in a project (+New-->Terminal) and type
sage -sh
pip install --user ggplot
Open the project's settings (the gear icon) and click "Restart Sage Worksheet Server" in the Restart section. If you're using Juoyter Notebook you may also have to click "Restart Project Server". Now ggplot should be available in any (restarted) worksheet (click the red restart icon in a worksheet to restart it). The restart is necessary, since the worksheet server uses forking to reduce startup time of worksheets from 5 seconds to 0.1 seconds.
To directly install your Python dependencies for your local user account,
you can place your packages here: $HOME/.local/lib/python2.7/site-packages/
.
The $HOME/.local/
path is the "canonical" root for some overlay directories
of linux's standard directory layout (For more information, type $ man hier
in a terminal).
To use binaries installed by pip add export PATH=~/.local/bin:$PATH
to ~/.bashrc
and run source ~/.bashrc
Here's a simple example of an HTTP server written using the Python SimpleHTTPServer class. Open a project and click "+New" then paste in this link, then click the "From Web" button:
https://gist.github.com/certik/7031075/raw/fe9eb600946e8b3a94b752a50f0ace9d5641354f/browser
Then in a terminal type
chmod +x browser
./browser
Here's a simple example of the above with Flask
You'll see a URL in a box that looks something like this
https://cloud.sagemath.com/4a5f0542-5873-4eed-a85c-a18c706e8bcd/port/22273/
Just paste it into your browser.
IMPORTANT: This web server is visible exactly to collaborators on your project and nobody else, and all communication is SSL encrypted. Thus you can't use this method to create a general purpose webserver available to the world. On the other hand, this is a good way to collaborate with a controlled group of people on development, without having to worry about security.
If you just want an http view of your files, use the raw servers, which is already available by default at https://cloud.sagemath.com/project_id/raw/
. The point of the above script is that you could modify it to provide all sorts of interesting functionality.
-
Click +New, type a filename, then click the "Sage Worksheet" button.
-
To evaluate code using R, begin the cell with
%r
, type the code, then press shift+enter. -
Type
%default_mode r
in a cell and press shift+enter; now all cells will be evaluated using R by default. If you need to switch back, use%default_mode sage
.
Open a terminal windows and type
`sage -R`
Then you can install packages as usual
`install.packages('packagename')`
Note that you must also upgrade your project to have network access (requires a subscription).
Question: I would like to override the default width and height for R svg figures in a Sage Worksheet.
To set width to 10 inches and height to 4 inches, use the sage command:
r.set_plot_options(width=10, height=4)
If you have set default_mode to r, then enter the command in a sage mode cell:
%sage r.set_plot_options(width=10, height=4)
You can change it by typing it again.
Whenever you save the LaTeX document, it will run a spell checker and underling the words that are not spelled correctly. By default, it uses the language you've set in your web browser.
You can change the autosave interval to be very short in account settings (under editor) if you need the spell checking to update frequently.
Seeing a list of alternative words (correct spellings) isn't supported directly in the editor yet https://github.com/sagemath/cloud/issues/50. For now, a workaround is to run LaTeX-aware aspell
, e.g.
1. open a terminal
2. aspell -t -c <filename.tex>
Suppose, your LaTeX project is composed of one master.tex
file and several chapter-1.tex
, chapter-2.tex
, etc. SageMathCloud's LaTeX editor only knows about the currently opened file, and using \import{}
doesn't work, because the chapter-*.tex
parts are not proper documents.
To solve this, use the subfiles package instead. It does not only collect the partial documents into one, but also extracts the preamble of the master.tex
file for each chapter-*.tex
in order to create valid subdocuments.
Following the documentation, do this:
-
\usepackage{subfiles}
inmaster.tex
-
\subfile{⟨subfile name ⟩}
for each subfile inmaster.tex
'sdocument
environment (i.e. instead of\include
or\import
). -
For each
chapter-*.tex
subfile:\documentclass[⟨master.tex file-name⟩]{subfiles} \begin{document} ⟨text, graphics, etc.⟩ \end{document}
After that, all *.tex
files can be compiled and all other features like forward/inverse search work, too.
You can make a symbolic link to the root of the filesystem by typing
ln -s / root
Now you can explore the complete filesystem. Note that there many files that you can only read and not write.
~/.bashrc
is run on startup and ~/.bash_profile
is not!
Hence, use ~/.bashrc
to customize your setup,
and you can also use ~/.bash_aliases
for your aliases (see ~/.bashrc
).a
Make sure you have at least 6GB disk space (look at quotas in project settings -- if you don't, don't want to upgrade, and are just playing around, cd to /tmp
instead), then type
install-latest-sage-release
Then type this, assuming the name of the install you just got is sage-6.7:
cd; mkdir -p bin; cd bin; ln -sf ~/sage-6.7/sage .
See the discussion about worksheet servers in the next FAQ question below. Also, now that you have your own copy of Sage, you can change anything in Sage! Do so, send us patches, etc. See http://www.sagemath.org/doc/developer/ for step-by-step instructions.
launch a Terminal and run the command
defaults write -g ApplePressAndHoldEnabled -bool false
Question: I would like that all of my worksheets know where to find a given module that I write or install.
Put an executable file with this content in $HOME/bin/sage:
#!/usr/bin/env bash
SAGE_PATH=$HOME/NEW_MODULE /usr/local/bin/sage "$@"
You could do this by making a new directory called bin, then a new file in there called "sage". Then in the terminal type the following to make "sage" executable.
cd; cd bin; chmod +x sage
This is also the file "sage" attached to this message.
Then restart the worksheet server by going project settings and clicking "Restart --> Worksheet server".
Now any newly (re-)started worksheet will run with the above modified SAGE_PATH. Since SAGE_PATH is added to PYTHONPATH when Sage starts, this does what you want.
Obviously, I plan to add a simple way to do something equivalent to the above, by filling in some settings box in the UI. I'll update this FAQ entry once I do that.
(From Nathan Dunfield) Another approach, which also works now and doesn't require the custom "$HOME/bin/sage", is to use http://docs.python.org/2/install/#alternate-installation-the-user-scheme
That is, one installs a module with "sage -python setup.py install --user" and it's dumped into
$HOME/.local/lib/python2.7/site-packages
This is location is searched automatically by Sage's Python without any intervention on the part of the user. (However, I did have to restart the worksheet server to access newly installed modules from a worksheet.) One can also put modules into the user's site-packages by hand and Sage will find them.
If your code is running in a SageMathCloud worksheet, then the global variable __SAGEWS__
will be defined.
Type salvus.[tab key]
.
There is a lovely tutorial on the web to help you do exactly that:
http://doc.sagemath.org/html/en/installation/index.html
See the instructions, immediately below, on using a custom built-from-scratch copy of Sage. Just substitute your own tar.gz file for the official build of Sage.
Open a Terminal. Grab the source tarball (requires network access). You can browse http://sage.math.washington.edu/home/release/ to find recent releases and testing versions.
To build, do the following in your terminal (no need to worry about screen or tmux, of course, since sessions are persistent even if your browser leaves), and check back in a few hours:
tar xvf sage-6.10.tar.gz && cd sage-6.10 && make
WARNING: Building can easily take more than 2 hours. By default SageMathCloud projects have an idle timeout that is smaller. If you aren't editing files in the project, your build will get killed part of the way through. If you're doing legit Sage development, email THE LINK TO YOUR PROJECT to [email protected] and we will increase the idle timeout, disk space, RAM, etc, so you can contribute to Sage.
After doing that, do something like this in the terminal:
cd; mkdir -p bin; cd bin; ln -s ~/sage-6.10/sage .
Then restart your worksheet server (in project settings). Then for that project, you'll have your own 100% customizable copy of Sage; and moreover, when the system-wide Sage is upgraded, your project isn't impacted at all -- that sort of stability is a major win for some people. This also uses little extra disk space in backups/snapshots, because of de-duplication. You can of course also install any custom packages you want into this copy of Sage. You can also help improve Sage: http://www.sagemath.org/doc/developer/
If you want to do Sage development see http://mathandhats.blogspot.com/2014/06/how-to-develop-for-sage-using-sage-math.html.
Important: Whenever you change Python code installed in that copy of Sage, you may have to restart the worksheet server and any running worksheets. This is inconvenient, but is necessary because the worksheet server starts one copy of Sage, then forks off additional copies each time you open a new worksheet, which greatly reduces the time from when you open a worksheet until it actually starts computing things.
Also Important: If your copy of Sage is messed up in some way, then the worksheet server can't start, hence worksheets won't open. To debug this, open a terminal and do this:
~$ cd .smc
~/.smc$ sage sage_server.py
you should see an error here, e.g.,
and fix whatever error you see. Also look at log files in ~/.smc/sage_server/
The task below is to create a custom Anaconda overlay environment called myconda
and, just for the sake of explanation,
- install "Microsoft's Open R" (which is an enhanced version of R by microsoft).
- Install the plotly library from PiPy
To get it installed as an average user in Anaconda, do this:
-
Open a terminal.
-
Type
anaconda3
-
Type
conda create -n myconda -c mro r
This creates a new local environment called "myconda" (name it as you wish) with the package "r" as it's source coming from the channel "mro" (Microsoft's Open R). Instead of that, you can add any other anaconda package in that spot. The example from the documentation is biopython, see http://conda.pydata.org/docs/using/envs.html#create-an-environment. -
When installing, it briefly shows you that it ends up in
~/.conda/envs/myconda/....
in your local files. Now that we have it installed, we can get out of this "root" environment via source deactivate or restart the session. In any case, you are back in the the normal Linux terminal environment. -
Now run this:
source ~/.conda/envs/myconda/bin/activate myconda
Note, that myconda is the name specified above, and the prompt's indicated switches to (myconda) $. which R shows:/projects/xxx-xxx-xxx/.conda/envs/myconda/bin/R
and of course, just running R gives:
R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
[...]
Microsoft R Open 3.2.3
Default CRAN mirror snapshot taken on 2016-01-01
The enhanced R distribution from Microsoft
- In the very same spirit, you can also run pip installations:
(myconda)~$ pip install plotly
Downloading/unpacking plotly
[...]
Successfully installed plotly requests six pytz
(myconda)~$ python -c 'import plotly; print(plotly)'
<module 'plotly' from '/projects/20e4a191-73ea-4921-80e9-0a5d792fc511/.local/lib/python2.7/site-packages/plotly/__init__.pyc'>
Note, that since I'm still in my own "myconda" overlay environment, the --user
switch in pip install
wasn't necessary. (Otherwise, it would be necessary.)
Question: I want to start long-running numerically intensive computations on SageMathCloud. What are the current limitations?
Open your project and click on Settings. The default limitations are listed under "Quotas" in the lower left. These can be raised, as mentioned there. Notes:
-
Projects on free non-members only Virtual Machines will get restarted regularly (these are hosted on Google preemtible instances). You can check if a VM rebooted by typing "uptime". crontab files are persistent.
-
If a project isn't used (via the web-based UI) for the idle timeout (as listed in quotas), then all processes in that project are terminated and the user is removed (so ssh into the project also is not possible). You can pay to raise the idle timeout.
Use /tmp. Files in /tmp may be deleted at any time, and aren't backed up.
Type exactly the following in a full terminal (+New--> Terminal) to see all processing running in a project:
htop
You can kill things, etc. See http://linux.die.net/man/1/htop.
Type exactly the following in a full terminal (+New--> Terminal):
smem -tk
It lists all processes and the bottom line shows the total sum.
The last RSS
column is probably the most interesting one, for more consult man smem
. The total used memory is also listed under 'Project usage and quotas" in project settings.
import sage_server
sage_server.MAX_OUTPUT_MESSAGES=100000
See this published worksheet for more details.
Also, type sage_server.[tab key]
to see information about other limitations.
Do not hesitate to email THE LINK TO YOUR PROJECT TO [email protected] or https://groups.google.com/forum/?fromgroups#!forum/sage-cloud
This Wiki is for CoCalc.com.
A more structured documentation is the CoCalc User Manual.
For further questions, please contact us.