Skip to main content

Creating Turtle Graphics in IPython/Jupyter with ipycanvas

This is inspired by https://github.com/gkvoelkl/ipython-turtle-widget. Thanks.

I am using ipycanvas (https://github.com/martinRenou/ipycanvas). Thanks to Martin Renou.

pic/screen.png

If you like it, use it. If you have some suggestions, tell me (hebi@python-ninja.com).

Install

To install use pip:

` $ pip install ipyturtlenext `

Examples

The most examples are inspired by Harold Abelson, Andrea diSessa: Turtle Geometry, MIT Press 1986

ipyturtlenext should work in every jupyter environment, where ipycanvas works.

Start

from ipyturtlenext import Turtle

t = Turtle(width=200, height=200)
t

With width and height you can change the extension of the drawing canvas.

In Jupyterlab you can create with new view for outout a separeted tab.

First Steps

t.right(90)
t.heading()
t.forward(150)
t.left(45)
t.back(100)
t.left(45)
t.penup()
t.forward(100)

Square

t.reset() #clear canvas and start again
t.back(40)
t.forward(100)
t.position()
def square(size):
    for i in range(4):
        t.forward(size)
        t.right(90)
square(20)

Triangel

t.reset()
def triangle(size):
    for i in range(3):
        t.forward(size)
        t.right(120)
triangle(100)

House

t.reset()
def house(size):
    square(size)
    t.forward(size)
    t.right(30)
    triangle(size)
t.back(100)
house(100)

Circle

t.reset()

def circle():
    for i in range(360):
        t.forward(1)
        t.right(1)
circle()

Poly

t.reset()
def poly(side, angle):
    turn = 0
    while turn == 0 or turn % 360 != 0:
        t.forward(side)
        t.right(angle)
        turn += angle
poly(44,135)

Color

Return the current pen color as RGB tuple or web color name

t.reset()
t.pencolor()

Set pen color as web color name

t.pencolor('Green')

Set pen color with RGB value

t.pencolor(255,0,0)
t.forward(40)
t.right(120)
t.pencolor('Blue')
t.forward(40)
t.pencolor('Red')
t.pendown()
t.forward(100)

Branch

def lbranch(length, angle, level):
    t.pencolor('Green')
    t.forward(2*length)
    node(length, angle, level)
    t.back(2*length)

def rbranch(length, angle, level):
    t.pencolor('Brown')
    t.forward(length)
    node(length, angle, level)
    t.back(length)

def node(length, angle, level):
    if level==0:
        return
    t.left(angle)
    lbranch(length, angle, level-1)
    t.right(2*angle)
    rbranch(length, angle, level-1)
    t.left(angle)
t.reset()
node(8,24,7)

Nested Triangle

def nested_triangle(size):
    if size < 10:
        return
    for i in range(3):
        nested_triangle(size/2)
        t.forward(size)
        t.right(120)
t.reset()
nested_triangle(100)

Snowflake

def snowflake(size, level):
    for i in range(3):
        side(size, level)
        t.right(120)

def side(size, level):
    if level == 0:
        t.forward(size)
        return
    side(size/3, level - 1)
    t.left(60)
    side(size/3, level - 1)
    t.right(120)
    side(size/3, level - 1)
    t.left(60)
    side(size/3, level - 1)
t.reset()
snowflake(100,4)

Nested squares

t.reset()
sideLength = 40
for square in range(5):
    for side in range(4):
        t.forward(sideLength)
        t.left(90)
    sideLength += 10

Changelog

0.1.0 First published version

Metadata

Release files for ipyturtlenext 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ipyturtlenext 0.1.1
File Size Uploaded
ipyturtlenext-0.1.1.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ipyturtlenext 0.1.1
File Interpreter ABI Platform
ipyturtlenext-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.1 kB

Release files / ipyturtlenext-0.1.1.tar.gz

Download URL ipyturtlenext-0.1.1.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f9d962a458d6308f737671c2b2e7b92aa26aecf6a1c19275ee50d72e2203f187
BLAKE2b-256 checksum
How to use checksums
410b9f03be9c1707f93f03582983baa35c2dd4837a60e85b2a18e0089f9fb802
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release files / ipyturtlenext-0.1.1-py3-none-any.whl

Download URL ipyturtlenext-0.1.1-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
296cc51092b29b9b1da2d6ab52a9a7bbd46caf7d2925ef968843886784022f28
BLAKE2b-256 checksum
How to use checksums
4a85b144210fbba2a423f2fab5203ae90b13a6587cae059404f3c19e34afc9d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page