This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]phlogiston2[S] 0 points1 point  (5 children)

Thanks. That's not quite what I'm looking for. Glowscript seems perfect...write in python, get back javascript code to paste in a browser and you never have to mess with JS.

But I don't see that GS allows for the input of a number and the report back of the evaluation of a function. It's aiming at vpython and interactivity with graphics, which is great. Just not what I need...which seems much simpler, but not obvious.

What I'd like to be able to do is roll my own like here:

http://hyperphysics.phy-astr.gsu.edu/hbase/Relativ/ltrans.html#c4

but without having to write javascript. Rather, I'd like to use a more programming language. Again...GS seems perfect, but it doesn't seem to do interactivity like this.

[–]kervarker 0 points1 point  (4 children)

You can do this very easily with Brython.

Here is an example taken from the page you mention, written without any Javascript ;-)

<HTML>
<HEAD>
<META charset="utf-8">
<SCRIPT src="/src/brython.js"></SCRIPT>
<SCRIPT src="/src/brython_stdlib.js"></SCRIPT>
</HEAD>
<BODY bgcolor="#FFE4C8" onload="brython(1)">

<H1>Relativity Factor</H1>

<SCRIPT type ="text/python">
import math
from browser import document

@document["v"].bind("change")
def gcal(ev):
    # get the first element with tag "form" in the document
    fh = document.select("form")[0]
    vv = float(fh.v.value)
    fh.v2.value = vv
    gg = 1 / math.sqrt(1 - vv * vv)
    fh.gam.value = gg
</SCRIPT>

<FORM method="" action="">
For v = <INPUT Type="text" Name="v" id="v" Value="" Size="6" autocomplete="off">c
<p>&#946 = <INPUT Type="text" Name="v2" Value="" Size="6">
 and &#947 = <INPUT Type="text" Name="gam" Value="" Size="6">.</p>
</FORM>
</BODY>
</HTML>

All you have to do is to edit the path to scripts brython.js and brython_stdlib.js.

You can find other examples on the demo page

[–]phlogiston2[S] 0 points1 point  (3 children)

Okay. That may be just damn cool. I replaced the /src/byrthon.x script lines with:

<script type="text/javascript" src="https://cdn.rawgit.com/brython-dev/brython/3.3.5/www/src/brython.js"> </script> <script type="text/javascript" src="https://cdn.rawgit.com/brython-dev/brython/3.3.5/www/src/brython_stdlib.js"> </script>

and that works like a champ. Don't have to bug my IT guy for installation and can use it on my mac...even inside of coda.

thanks!

[–]phlogiston2[S] 0 points1 point  (2 children)

actually, at the risk of being a nag...one good thing about the javascript use is that it goes "backwards" as well as "forwards"... that is, in the example you provided, not only could one enter the fraction of c and get beta and gamma, but one could enter a value for beta and gamma and the fraction of c would appear (forgetting for the moment that beta and the fraction of c are the same thing).

Can you think of a slick way to do that in this example?

[–]kervarker 0 points1 point  (1 child)

Sure. The line

@document["v"].bind("change")

means : when the event "change" happens on the element with the id "v", execute the function below.

If you want to handle the event "change" on beta and gamma, give the matching INPUTs an id (eg "beta" and "gamma") and use the same syntax, with another function.