首先,安装ipyturtle3 ref:ipyturtle3 · PyPI
pip install ipyturtle3
然后,安装ipycanvas
ipycanvas是一个需要安装在与JupyterLab实例相同环境的包。此外,您需要安装nodejs,并启用JupyterLab ipycanvas小部件。
所有这些都在ipycanvas安装说明中有详细介绍:
使用conda
conda install -c conda-forge ipycanvas
JupyterLab扩展
如果您已经安装了JupyterLab,还需要安装JupyterLab扩展。为了安装JupyterLab扩展,您需要先安装npm。您可以使用conda轻松安装npm:
conda install -c conda-forge nodejs
然后您可以安装JupyterLab扩展:
jupyter labextension install @jupyter-widgets/jupyterlab-manager ipycanvas
由于您提到您正在使用jupyterhub,如果您在集群上并且不是管理员,可能没有权限执行这些操作。如果是这种情况,请联系您的集群管理员,他们可能不允许用户启用扩展。
可以采用如下的代码测试是否安装成功文章来源:https://www.toymoban.com/news/detail-662818.html
import ipyturtle3 as turtle
from ipyturtle3 import hold_canvas
myCanvas=turtle.Canvas(width=500,height=250)
display(myCanvas)
myTS=turtle.TurtleScreen(myCanvas)
myTS.clear()
myTS.bgcolor("lightgreen")
bob=turtle.Turtle(myTS)
jess=turtle.Turtle(myTS,isHolonomic=True)
jess.shape("square")
bob.shape("turtle")
myTS.delay(200)
# Usual turtle Non-holonomic example
bob.forward(50)
bob.left(90)
bob.forward(50)
bob.left(90)
bob.forward(50)
bob.left(90)
bob.forward(50)
#Holonomic
jess.moveleft(200)
jess.moveup(100)
jess.moveright(100)
jess.movedown(100)
jess.turnright(45)
jess.turn(-45)
jess.distance_at_angle(50,45)
myTS.clear()
# Rainbow benzene
colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']
t = turtle.Turtle(myTS)
myTS.bgcolor('black')
for x in range(50):
with(hold_canvas(myCanvas)):
t.pencolor(colors[x%6])
t.width(x//100 + 1)
t.forward(x)
t.left(59)
ref:https://stackoverflow.com/questions/72410176/how-to-display-canvas-with-ipycanvas-on-jupyterhub文章来源地址https://www.toymoban.com/news/detail-662818.html
到了这里,关于在jupyter notebook中使用海龟绘图的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!