0

Javascript Export for Python?

I found javascriptMuExport(webView.page().mainFrame())

...but there doesn't seem to be an equivalent for python. How do you get rvsession.pyevaluate() to work on the javascript side? 

RV 4

4 条评论

  • 0
    Avatar
    Scott Willman

    Ok, so it looks like javascriptMuExport actually exports rvsession.pyevaluate as well. But I can't get it to do anything...

    Found it like this:

    if (typeof(rvsession.pyevaluate) != "undefined") {
        alert('found it');
    } else {
        alert('not here');
    }

    // found it

     

    Now I'm trying something like this with no result:

    rvsession.pyevaluate('from rv import extra_commands; extra_commands.displayFeedback("TEST", 2.0)');

    Any help?

  • 0
    Avatar
    Scott Willman

    I can't get rvsession.pyevaluate to do anything. Is it because my WebView is running from a Mu-based package?

    I'm trying this:

    <p><a href="javascript:play()">Play</a></p>
    <script language="javascript" type="text/javascript" charset="utf-8">
        function play() {
            alert(rvsession.pyevaluate("import commands; commands.play()"));
        }
    </script>

     

    The 'alert' returns this message:

    <!--StartFragment--><type 'exceptions.SyntaxError'>: invalid syntax (<string>, line 1)<!--EndFragment-->

  • 0
    Avatar
    Jon Morley

    For all those that may see this forum thread in the future, Scott and I solved this in a side conversation. What was missing was the use of rvsession.pyexec in place of rvsession.pyevalute. The difference in these two methods is exactly the same as the difference between Python's exec and eval methods. For Mu the two are the same.

  • 0
    Avatar
    Jon Morley

    I am adding some more notes here for others that find this thread from some investigating Scott and I did on the side:

    Mixing pyevaluate and pyexec with globals to handle expressions and statements:

    function myrun () {
    rvsession.pyexec("import os; global filelist; filelist = os.listdir(os.getcwd())");
    var tmp = rvsession.pyevaluate("filelist");
    document.write(tmp);
    }

    Using pyevaluate and builtins alone:

    function myrun () {
    var tmp = rvsession.pyevaluate("__import__('os').listdir(__import__('os').getcwd())");
    document.write(tmp);
    }

    Both of these methods have their shortcomings. Globals are always dangerous because you can never be sure something else won't modify them unexpectedly or deliberately due to name collisions. And using Python's builtins this way makes for difficult to read code.

    If it were up to me I would do as much as I could in a standalone Python module and make calls to that module through pyevaluate and explicit calls to the Python builtins like __import__.

请先登录再写评论。