ipython-turtle-widget

Creating Turtle Graphics in IPython/Jupyter

Draw on the page or use an extra window. under construction

If you like it, use it. If you have some suggestions, tell me (gkvoelkl@nelson-games.de).

Install

To install use pip:

``` $ pip install ipyturtle $ jupyter nbextension enable --py --sys-prefix ipyturtle # can be skipped for notebook 5.3 and above ```

To install JupyterLab extension:

``` $ jupyter labextension install ipyturtle ```

For a development installation (requires npm),

``` $ git clone https://github.com/gkvoelkl/ipython-turtle-widget.git $ cd ipython-turtle-widget $ pip install -e . $ jupyter nbextension install --py --symlink --sys-prefix ipyturtle $ jupyter nbextension enable --py --sys-prefix ipyturtle ```

Examples

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

Start


In [ ]:
from ipyturtle import Turtle
t = Turtle()
t

The turtle is drawn on its own part of the screen.








Or integrate the turtle graphic into the page


In [ ]:
t = Turtle(fixed=False, width=100, height=100)
t






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

First Steps


In [ ]:
t.right(90)
t.heading()

In [ ]:
t.forward(150)
t.left(45)

In [ ]:
t.back(100)

In [ ]:
t.left(45)
t.penup()
t.forward(100)

Square


In [ ]:
t.reset() #clear canvas and start again

In [ ]:
t.back(40)
t.forward(100)
t.position()

In [ ]:
def square(size):
    for i in range(4):
        t.forward(size)
        t.right(90)

In [ ]:
square(20)

Triangel


In [ ]:
t.reset()

In [ ]:
def triangle(size):
    for i in range(3):
        t.forward(size)
        t.right(120)

In [ ]:
triangle(100)

House


In [ ]:
t.reset()

In [ ]:
def house(size):
    square(size)
    t.forward(size)
    t.right(30)
    triangle(size)

In [ ]:
t.back(100)
house(100)

Circle


In [ ]:
t = Turtle(fixed=False, width=120, height=120)

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

t







In [ ]:
circle()

Poly


In [ ]:
t.reset()

In [ ]:
def poly(side, angle):
    turn = 0
    while turn == 0 or turn % 360 != 0:
        t.forward(side)
        t.right(angle)
        turn += angle

In [ ]:
poly(44,135)

In [ ]:
t.reset()

Color

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


In [ ]:
t.pencolor()

Set pen color as web color name


In [ ]:
t.pencolor('Green')

Set pen color with RGB value


In [ ]:
t.pencolor(255,0,0)

In [ ]:
t.forward(40)
t.right(120)
t.pencolor('Blue')
t.forward(40)

Branch


In [ ]:
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)

In [ ]:
t.reset()
node(8,24,7)

Nested Triangle


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

In [ ]:
t.reset()
nested_triangle(100)

Snowflake


In [ ]:
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)

In [ ]:
t.reset()
snowflake(100,4)

Nested squares


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

Changelog

0.2.0 First published version
0.2.4 More Examples. Moveable Windows.

Sources

More about Creating Custom Widgets in IPython https://github.com/ipython/ipywidgets
A template Widget Project https://github.com/ipython/ipywidgets


In [ ]: