Simple image rotation with Python and DOM

Skip to first unread message

Sep 25, 2016, 11:38:34 AM9/25/16
to Users
Hello I want to rotate image (cog) wit Python code and DOM elements.

I have this code write in Python adn work with Brython. How can I change it to use it on PyJS? I want use Python, not JavaScript.

<!DOCTYPE html>

<!-- load Brython -->
<script src=""></script>

<!-- the main script; after loading, Brython will run all 'text/python3' scripts -->
<script type='text/python'>
from browser
import window, timer, document, html
import time

<!-- I know that here, I must use this t0 = -->

= time.time()

def user_agent
""" Helper function for determining the user agent """
if window.navigator.userAgent.find('Chrome'):
return 'chrome'
   elif window
return 'firefox'
   elif window
return 'msie'
   elif window
return 'opera'
# Dict Mapping UserAgents to Transform Property names
= {

= 0
def animation_step
""" Called every 30msec to increase the rotatation of the element. """
   global degrees
, tm

# Get the right property name according to the useragent
= user_agent()
= rotate_property.get(agent,'transform')

# Get the element by id
= document[elem_id]

# Set the rotation of the element
(, prop, "rotate("+str(degrees)+"deg)")
['status'].innerHTML = "rotate("+str(degrees)+" deg)"
# Increase the rotation
+= 1
if degrees > 360:
# Stops the animation after 360 steps
= 0

# Start the animation
= timer.set_interval(lambda id='img1':animation_step(id),30)

['status3'].innerHTML = "Time of execution python code("+str(time.time()-t0)+" ms)"

<!-- I know that here i must use this: "Time of execution python code",, "ms") -->


<!-- After the page has finished loading, run bootstrap Brython by running
     the Brython function. The argument '1' tells Brython to print error
     messages to the console. -->

<body onload='brython(1)'>

<img id="img1" src="cog1.png" alt="cog1">
<h2 style="width:200px;" id="status"></h2>
<h2 style="width:800px;" id="status3"></h2>

Reply all
Reply to author
0 new messages