CoCalc Blog

Week 35, 2016

• smc

Last week marks the end of the summer and we noticed a significant increase in overall traffic and activity. This adds more pressure to the ongoing Kubernetes rewrite of the SMC back-end. We also started to collect a few courses teaching with SMC — if you are also teaching with SMC, please let us know!

Multi-user sync-aware full document undo/redo

williamstein • • dev

Today – motivated by a challenge from a c9.io developer at a recent meetup in Seattle – I finally implemented multi-user sync-aware full document undo/redo, at least for code editors, sage worksheets, and Jupyter notebooks. If you’ve ever edited a file, worksheet, or Jupyter notebook at the same time as somebody else, and you hit control+z (or click undo) right after they type something, you would have undid their last thing. That’s because the undo/redo would use the underlying Codemirror editor’s undo/redo functionality. I wrote a new implementation of undo/redo built on top of the realtime multiuser sync functionality. Instead of undoing the last change (or changes if you undo or redo multiple times) to the document, it undoes only the changes that you made during this session.

For Jupyter notebooks in SageMathCloud this has an interesting side effect. Vanilla Jupyter itself doesn’t have any global undo – instead they have a local undo in each cell, which you could only use via the keyboard. With this change, now Jupyter notebooks in SMC have a global undo: make some changes in any cell(s), move cells around, delete cells, etc., then click undo/redo or use the keyboard to undo/redo, and the undo should undo everything you actually did across all cells.

Tip #1: Restart Project

haraldschilly • • tipps

Today’s little usage tip is about resource usage and project restarts. Each time you open up Sage Worksheets or Jupyter Notebooks, the state of it needs to be stored in memory. This can become quite costly if you open many of them after another! They also continue to run in the background when you close the tab.

For example, you’re grading a lot of homework from your students, or you’re torn apart working with many files at once.

The solution is to either explicitly stop each running instance with the stop button (available for both types of documents) after you’re finished with it, or restart the entire project.

Restarting the project is like rebooting your computer. Everything is cleaned up and you end up with a blank state. Go to the project settings, and then click “restart project” in “project control”.

If there were still Jupyter Notebooks open, they might give you little error messages about being cut off abruptly. Well, don’t worry, just close and re-open them.

Pro-tip: In these project settings, on the left hand side, you can see the current memory usage and the quota. At the latest when it does grow above the quota, things might no longer work as well as they should.

Nightly Changelog #2

johnjeng • • smc

Our Nightly Changelog keeps you updated on small feature changes, bugfixes, and quality of life improvements. For upcoming changes, see our weekly progress report column.

General Usage

Quality of Life

Bugfixes

Nightly Changelog #1

johnjeng • • smc

Our Nightly Changelog keeps you updated on small feature changes, bugfixes, and quality of life improvements. For upcoming changes, see our weekly progress report column.

General Usage

Quality of Life

How do I start a Jupyter kernel in a SageMath Worksheet?

• smc

For a quick reminder, sample code is available for opening an Anaconda3 session. In the Sage worksheet toolbar, select Modes > Jupyter bridge.

Use the jupyter command to launch any installed Jupyter kernel from a Sage worksheet

py3 = jupyter("python3")

After that, any cell that begins with %py3 will send statements to the Python3 kernel that you just started. If you want to draw graphics, there is no need to call %matplotlib inline.

%py3
print(42)

import numpy as np; import pylab as plt
x = np.linspace(0, 3*np.pi, 500)
plt.plot(x, np.sin(x**2))
plt.show()

You can set the default mode to be your Jupyter kernel for all cells in the worksheet: after putting the following in a cell, click the “restart” button, and you have an anaconda worksheet.

%auto
anaconda3 = jupyter('anaconda3')
%default_mode anaconda3

Creating Custom "Mode Commands" in Sage Worksheets

• smc

What is a Mode Command?

By default, running a cell in a Sage worksheet causes the input to be run as Sage commands, with output from Sage written to the output of the cell. Mode commands in a Sage worksheet cause the input to be run through some other process to create cell output. For example,

There are many built-in modes (e.g. Cython, GAP, Pari, R, Python, Markdown, HTML, etc…)

Note: If it is not the default mode of your *.sagews worksheet, a mode command must be the first line of a cell. In other words, make sure the command %md, %r, or %HTML is the first line of a cell.

Alternatively, you can make any mode the default for all cells in the worksheet using %default_mode <some_mode>. Then all cells will be using that chosen mode. If you choose this approach, you may still explicitly use %sage for cells you want processed by the Sage interpreter (or %foo to explicitly switch to any non-default mode).

There is an entire section of the FAQ page SageMathCloud Worksheet (and User Interface) Help dedicated to questions about the built-in modes. It had 10 questions-and-answers in it as of July 28, 2016.

Is there a list of all currently supported % modes in SageMathCloud?

You can view available built-in modes by selecting Help > Mode commands in the Sage toolbar while cursor is in a sage cell. That will insert the line print('\n'.join(modes())) into the current cell.

What is a Custom Mode Command?

Custom mode commands are modes defined by the user. Like any mode command, a custom mode command processes the input section of a cell and writes the output. As stated in the help for modes,

Create your own mode command by defining a function that takes a string as input and outputs a string. (Yes, it is that simple.)

Examples of Custom Mode Commands

Custom mode commands can be used to

Here are some examples:

Example 1: View CSV data as a table

Define the mode in a sage cell, as follows:

import pandas as pd
from StringIO import StringIO
def csv_table(str):
    print(pd.read_csv(StringIO((str)),index_col = 0))

Input:

%csv_table
Sample,start,middle,end
A,2,5,51
B,6,8,11
C,7,22,41

Output:

        start  middle  end
Sample
A           2       5   51
B           6       8   11
C           7      22   41

NOTE: Sage’s show command is also aware of Pandas tables, so if you instead define

def csv_table(str):
    show(pd.read_csv(StringIO((str)),index_col = 0))

then %csv_table will produce nice HTML output.

Hello World

haraldschilly • • smc

We are the SageMathCloud developers!

This is just a test and we love math:

$\Phi(x) = \frac{1}{\sqrt{2 \pi}} \int_{-\infty}^x e^{-\xi^2/2}\; d\xi$

Expect more soon!