Turtle Graphics with CoCalc

There are three ways to use turtle graphics with CoCalc.

Sage worksheet with Turtle module

There is an example of a Sage worksheet using Turtle on the CoCalc share server. In the Sage worksheet, code and turtle graphics output are visible in the same tab.

../_images/turtle-spirals.png

Turtle spirals in the example Sage worksheet

To copy the example into a project of your own, do the following:

  1. Sign into CoCalc.
  2. While signed in, open another browser tab in the same browser session and navigate to the share link above. After a moment, you will see the published read-only copy of the worksheet.
  3. Click Open in CoCalc at the upper right. A third browser tab will open.
  4. Click Files at upper left in this latest tab. You will see a Files listing with a line for the Turtle example.
  5. Click the Copy button at the left of the Turtle entry. You can now copy the worksheet to a destination project and folder of your own.
../_images/only-public.png

copying worksheet from share server to personal project

Note

The remaining methods use the Python turtle library. This library relies on tkinter. You can use tkinter in CoCalc only from the the X11 Desktop. Code for the examples below is taken from the Turtle star on the doc page for the Python turtle library.

X11 terminal with Jupyter notebook

../_images/jupyter-turtle.png

python turtle display created by jupyter notebook in a second tab

  1. In the x11 terminal, echo the DISPLAY environment variable to get the display number:
~$ echo $DISPLAY
:581076966
  1. In your Jupyter notebook set DISPLAY to the same number:
import os
os.environ['DISPLAY'] = ":581076966"
  1. Turtle graphics work in the Jupyter notebook, but appear in the X11 desktop. You may have to run the graphics cell twice to see the turtle graphics.

X11 terminal with nteract

../_images/nteract-turtle.png

nteract python turtle display

This approach uses the enteract desktop application with X11.

  1. Open an X11 Desktop.
  2. Click the nteract button at the bottom, or type “nteract” at the shell prompt. Wait for nteract to start. This can take up to a minute the first time you run it in CoCalc X11.
  3. Paste this code and hit shift enter:
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()
  1. A new tab with title “Python Turtle Graphics” will appear in the X11 display when you run the code. It may not look right the first time. If that happens, return to the compute cell in the nteract tab and run the code again. You can click the vertical split button (upper right) and see the Jupyter notebook and graphics side-by-side, as shown in the screenshot.