Simple image rotation with Python and DOM using RapydScript
<p>I have this code write in Python and works fine with Brython. 
This code rotate image in this case a cog. 
How Can I change it, and what change to work with RapydScript? I am new at programming so please have patience :D</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;

&lt;!-- load Brython --&gt;
&lt;script src="http://brython.info/src/brython_dist.js"&gt;&lt;/script&gt;


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

&lt;!-- I know that here, I must use this t0 = Date.now() --&gt;

t0 = time.time()


def user_agent():
   """ Helper function for determining the user agent """
   if window.navigator.userAgent.find('Chrome'):
       return 'chrome'
   elif window.navigator.userAgent.find('Firefox'):
       return 'firefox'
   elif window.navigator.userAgent.find('MSIE'):
       return 'msie'
   elif window.navigator.userAgent.find('Opera'):
       return 'opera'

# Dict Mapping UserAgents to Transform Property names
rotate_property = {
   'chrome':'WebkitTransform',
   'firefox':'MozTransform',
   'msie':'msTransform',
   'opera':'OTransform'
}

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

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

   # Get the element by id
   el = document[elem_id]

   # Set the rotation of the element
   setattr(el.style, prop, "rotate("+str(degrees)+"deg)")
   document['status'].innerHTML = "rotate("+str(degrees)+" deg)"


   # Increase the rotation
   degrees += 1
   if degrees &gt; 360:
       # Stops the animation after 360 steps
       timer.clear_interval(tm)
       degrees = 0

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

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

&lt;!-- I know that here i must use this: "Time of execution python code", Date.now()-t0, "ms") --&gt;
&lt;/script&gt;

&lt;/head&gt;

&lt;!-- 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. --&gt;
&lt;body onload='brython(1)'&gt;

&lt;img id="img1" src="cog1.png" alt="cog1"&gt;
&lt;script&gt;animation_step("img1",30);&lt;/script&gt;
&lt;h2 style="width:200px;" id="status"&gt;&lt;/h2&gt;
&lt;h2 style="width:800px;" id="status3"&gt;&lt;/h2&gt;


&lt;/body&gt;  
&lt;/html&gt;
</code></pre>

<p>I'm not too familiar with Brython, but right away I can tell you that to port it to RapydScript you simply need to drop most of the unnecessary abstractions that I see the code importing, since RapydScript is closer to native JavaScript. As far as having the RapydScript code in the browser, you have a couple options:</p>

<ul>
<li>(recommended) compile your code ahead of time and include the .js file in your html (similar to Babel, UglifyJS, etc.), this way the code will run much faster and won't require you to include the compiler in the page</li>
<li>use an in-browser RapydScript compiler (this one seems to be the most up-to-date if you don't want to tinker with compilation: <a href="https://github.com/adousen/RapydScript-pyjTransformer" rel="nofollow">https://github.com/adousen/RapydScript-pyjTransformer</a>) and include the code inside <code>&lt;script type="text/pyj"&gt;</code> tag similar to what you have done in the example.</li>
</ul>

<p>Now, assuming you pick the recommended option above, the next step is to drop the Brython boilerplate from the code, here is what your logic would look like in RapydScript (note that I also refactored it a little, removing the unnecessary 2-stage rotate method resolution and unneeded lambda call):</p>

<pre><code>t0 = Date.now()

def rotate_property():
   """ Helper function mapping user agents to transform proeprty names """
   if 'Chrome' in window.navigator.userAgent: return 'webkitTransform'
   elif 'Firefox' in window.navigator.userAgent: return 'MozTransform'
   elif 'MSIE' in window.navigator.userAgent: return 'msTransform'
   elif 'Opera' in window.navigator.userAgent: return 'OTransform'
   return 'transform'

degrees = 0
def animation_step(elem_id='img1'):
   """ Called every 30msec to increase the rotatation of the element. """
   nonlocal degrees

   # Get the right property name according to the useragent
   prop = rotate_property()

   # Get the element by id
   el = document.getElementById(elem_id)

   # Set the rotation of the element
   el.style[prop] = "rotate(" + degrees + "deg)"
   document.getElementById('status').innerHTML = "rotate(" + degrees + "deg)"

   # Increase the rotation
   degrees += 1
   if degrees &gt; 360:
       # Stops the animation after 360 steps
       clearInterval(tm)
       degrees = 0

# Start the animation
tm = setInterval(animation_step, 30)
document.getElementById('status3').innerHTML = "Time of execution python code(" + (Date.now() - t0) + " ms)"
</code></pre>

<p>A few things to note:</p>

<ul>
<li>imports are no longer needed, RapydScript doesn't need boilerplate to interact with JavaScript</li>
<li>Pythonic <code>timer.set_interval</code> and <code>timer.clear_interval</code> have been replaced with JavaScript equivalents (setInterval and clearInterval)</li>
<li><code>document</code> you see in my code is the DOM itself, <code>document</code> you have in Brython code is a wrapper around it, hence accessing it works a bit differently</li>
<li>RapydScript dropped <code>global</code> a long time ago in favor of Python 3's safer <code>nonlocal</code>, which is what I used in the code instead</li>
<li>instead of <code>time</code> module, RapydScript has direct access to JavaScript's <code>Date</code> class, which I used in the code to time it</li>
<li>I would also recommend moving <code>prop = rotate_property()</code> call outside the function, since the user agent won't change between function calls (in this case the operation is relatively cheap, but for more complex logic this will improve your performance)</li>
<li>you seem to be launching Brython from HTML via body <code>onload</code>, get rid of that as well as the line that says <code>&lt;script&gt;animation_step("img1",30);&lt;/script&gt;</code>, the above code should trigger for you automatically as soon as page loads courtesy of setInterval call</li>
<li>since RapydScript uses unicode characters to avoid name collisions, you need to tell HTML to treat the document as unicode by adding this line to head: <code>&lt;meta charset="UTF-8"&gt;</code></li>
<li>for future reference, none of your <code>onload</code> calls to RapydScript will work, because unlike Brython, RapydScript protects itself in its own scope, invisible to the outside (this has been the accepted practice in JavaScript world for a long time), your options for making <code>onload</code> work are:

<ul>
<li>(not recommended) compile the file with <code>-b</code> flag to omit the self-protecting scope</li>
<li>(recommended) inside your code, attach your functions to the global <code>window</code> object if you want them accessible from outside</li>
</ul></li>
</ul>

<p>Your actual html code that calls the compiled version of the above code would then look like this:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="UTF-8"&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;img id="img1" src="cog1.png" alt="cog1"&gt;
&lt;h2 style="width:200px;" id="status"&gt;&lt;/h2&gt;
&lt;h2 style="width:800px;" id="status3"&gt;&lt;/h2&gt;
&lt;script src="myfile.js"&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

python rapydscript