-1

I am making a website with flask framework as a simple project, and I'm wondering if there is a way to embed a python turtle script, I know I can use the jinja {% python stuff %}{% end python stuff %} for simple python statements but I don't know how to run a script.

To be even more specific, I want to run a python turtle game I made, so I need to somehow embed the turtle window and make it "listen" for user input until you press certain keys, like the esc or p key for example.

Say I want to embed this simple turtle game:

import turtle

#screen
wn = turtle.Screen()
wn.setup(width = 600,height = 600)
wn.title("Turtle Game")

#player
player = turtle.Turtle()
player.shape("turtle")
player.color("green")
player.up()
player.speed(0)
player.setposition(0,0)

#controls
def go_forw():
    player.forward(10)

def go_back():
    player.forward(-10)

def turn_r():
    player.right(5)

def turn_l():
    player.left(5)

#key input & loop
turtle.listen()
turtle.onkey(go_forw,'Up')
turtle.onkey(go_back,'Down')
turtle.onkey(turn_l,'Left')
turtle.onkey(turn_r,'Right')

turtle.mainloop()

I know it can be done using replit, but replit doesn't support some modules I want to use, is it possible to embed a turtle script like this one without hosting your code somewhere online?

I will be glad for any answer.

Thanks in advance.

Sklenik
  • 9
  • 4
  • Already asked in [Running Python 3 Turtle programs in the browser](https://stackoverflow.com/questions/69326598/running-python-3-turtle-programs-in-the-browser) which offers a few packages you could experiment with. – ggorlen Apr 23 '22 at 06:00

1 Answers1

1

You unfortunately cannot show the turtle GUI to users via flask, although you could export the canvas as a PNG, and then host the PNG via flask.

Sid Nutthi
  • 161
  • 8