Turtle Graphics with CoCalc¶
There are three ways to use turtle graphics with CoCalc.
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.
To copy the example into a project of your own, do the following:
- Sign into CoCalc.
- 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.
Open in CoCalcat the upper right. A third browser tab will open.
Filesat upper left in this latest tab. You will see a Files listing with a line for the Turtle example.
- Click the
Copybutton at the left of the Turtle entry. You can now copy the worksheet to a destination project and folder of your own.
- In the x11 terminal, echo the DISPLAY environment variable to get the display number:
~$ echo $DISPLAY :581076966
- In your Jupyter notebook set DISPLAY to the same number:
import os os.environ['DISPLAY'] = ":581076966"
- 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.
This approach uses the enteract desktop application with X11.
- Open an X11 Desktop.
- Click the
nteractbutton 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.
- 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()
- 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.