RIA con Python...

Chema Cortes pych3m4 en gmail.com
Vie Ago 31 16:05:23 CEST 2007


El 30/08/07, Juan M. <juatman_2000 en yahoo.es> escribió:
>  Hola :-)
>  Buscando información y trasteando he conseguido desarrollar el ejemplo RIA
> (Rich Internet Application) de "mini-calculadora" para CherryPy2.2.1; así
> que funciona bien en TurboGears 1.
>  RIA, RIA con Python. :-))
>   He aquí el código; espero que os sea útil, saludos.
>

Contribuyo con un ejemplo mío hecho en mod_python. Es algo más robusto
en cuanto a portabilidad javascript entre navegadores, y tiene algunas
cosillas que pueden ser muy útiles. He juntado todo en un sólo
fichero, aunque en un despliegue normal cada cosa iría a un fichero
para aprovechar el proceso de plantillas que tiene mod_python:

def evalua(req,expr):
    #Make a protected enviroment
    env={"__builtins__":__builtins__}
    exec "from cmath import *" in env
    env["__builtins__"]={}
    env["fact"]=fact

    try:
        res=str(eval(expr,env))
    except:
        res="""<span style="color:red;">???</span>"""

    return "%s = %s"%(expr,res)

def fact(n):
    res=1
    for i in xrange(2,n+1):
       res*=i
    return res


def index(req):
    page="""
        <html>
        <head>
        <script type="text/javascript">

        function $(id) { return document.getElementById(id); }

        function createXMLHttpRequest() {
          try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
          try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
          try { return new XMLHttpRequest(); } catch (e) {}
          alert("XMLHttpRequest not supported");
          return null;
        }

        function docall(url, vars, dest) {
          var xhr=createXMLHttpRequest();

          xhr.onreadystatechange = function() {
            if (xhr.readyState==4) { //Request is finished
              if (xhr.status==200) {
                 $(dest).innerHTML=xhr.responseText;
              } else {
                 alert("Message returned with error status");
              }
            }
          }

          xhr.open("POST",url,true);
          xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
          xhr.setRequestHeader("Connection", "close");
          xhr.send(vars);
        }
        </script>

        </head>
        <body>

        <script type="text/javascript">

        function mostrar_resultado() {
          var url="evalua"
          var vars="expr="+encodeURIComponent($("expr").value)
          docall(url,vars,"resultado")
        }

	function borrar() {
          $("expr").value="";
          $("resultado").innerHTML="";
        }

        </script>
        <strong>Cálculo</strong>:
        <input id="expr" type="text" onkeyup="mostrar_resultado()"
size="40" value="%(inival)s">
        <button onclick="borrar()">Borrar</button><br />
        <strong>Resultado</strong>:
        <span id="resultado">%(inires)s</span>

        </body>
        </html>
    """
    inival="fact(5)+log(-1)"
    return page%({"inival":inival,"inires":evalua(req,inival)})




Más información sobre la lista de distribución Python-es