Skip to content

Instantly share code, notes, and snippets.

Forked from magopian/
Last active September 18, 2023 23:07
Show Gist options
  • Save davidbgk/21aefce3f26423034f0f to your computer and use it in GitHub Desktop.
Save davidbgk/21aefce3f26423034f0f to your computer and use it in GitHub Desktop.
Auto-reload turtle code on file save (
"""Reloads the `` code on save.
Put simple turtle instructions in the `` file,
and they'll be re-run (on a clean window) on each file save.
1/ put some turtle instructions in a `` file
(eg `turtle.forward(100)`)
2/ run `python` on a commandline
(no dependencies needed)
3/ play around by adding/modifiying some turtle instructions
in the `` file
4/ when you're happy with your modifications, save the file
(it'll empty the turtle screen and re-run the instructions)
5/ back to 3/
Stop by pressing CTRL+C in the terminal.
import os
import traceback
import sys
import time
import turtle
# Get the last modification time. Not as effective as some
# libs out there, but doesn't needs dependencies.
get_last_mod = lambda: os.stat('').st_mtime
# Fancier than the original arrow shape.
# Initialize with a dummy value.
last_modification = 0
while True: # Run until a KeyboardInterrupt (CTRL+C).
if get_last_mod() != last_modification:
# Has the `` file been saved?
last_modification = get_last_mod()
# Empty the window, start from scratch.
# Re-run the instructions in the `` file.
with open('', 'r') as my_turtle:
time.sleep(1) # Poll once a second.
except KeyboardInterrupt: # CTRL+C in the terminal.
# Anything wrong in the `` file?
# Don't fail, just display the traceback, and continue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment