[IronPython] Solved: JavaScript -> Python COM without COMVisible

Gutfreund, Yechezkal ygutfreund at draper.com
Wed Jan 3 15:45:26 CET 2007


A while back, I asked for advise in this group about how I could
communicate from Microsoft Virtual Earth (VE) back to a python app in
which it was embedded. VE needs to run in a WebBrowser control.
 
I was able to do this by creating at XAML file, with the Browser
control, and then to wrap all of this in IronPython with the avalon.py
include.
 
Calling JavaScript  from python is simple (.invokeScript()) but the
other direction was hard.
 
With your advice. I have gotton the following paradigm to work (which
uses an C# interceptor that fires events that are trapped by python).
 
JavaScript:
 
 window.external.dispatchString("shell", "mapClicked",
"something|Something|Something"); // first item is module name, second
is function, and third a | seperated list of arguments (a refinement of
this is to use xml, or there are some introspection techniques in C#
that are fairly heady, but can also be used. But there are real problems
in sending simple arrays straight to C# via interop).
 
C#:
 
namespace IPyTools
{
    [ComVisible(true)]
    public class PyArgs : EventArgs {
        public string module;
        public string function;
        public object[] args;
        public PyArgs(string module, string function, object[] args) {
            this.module = module;
            this.function = function;
            this.args = args;
        }
    }
    [ComVisible(true)]
    public class Interceptor {
        public delegate void IHandler(object source, PyArgs e);
        public event IHandler iPyEvents; 
        public void dispatch(string module, string function, params
object[]fArgs) {
            PyArgs e = null;
            if (iPyEvents != null)
            {
                e = new PyArgs(module, function, fArgs);
                iPyEvents(this, e);
            }
        }
        public void dispatchString(string module, string function,
string strArgs) {
            if (strArgs == "") {
                dispatch(module, function);
                return;
            }
            object[] fArgs = strArgs.Split('|');
            dispatch(module, function, fArgs);
        }
    }
}
 
 
 
Python:
 
 i = IPyTools.Interceptor()
 i.iPyEvents += dispatcher
 wb.ObjectForScripting = i
 
def dispatcher(who, e):
 callee = sys.modules[e.module].__dict__[e.function]
 apply(callee, e.args)
 
def mapClicked(style, cLat, cLong, mLat, mLong, zoom): 
 print "mapClicked: "
 print " style:", style
 print " center:", cLat +","+ cLong
 print " mouse:", mLat +","+ mLong
 print " zoom:", zoom
 
 
Dr. Yechezkal Gutfreund
Tactical ISR Department
Draper Laboratory
555 Technology Square, MS-77
Cambridge, MA 02139
+1-617-258-4206
ygutfreund at draper.com <mailto:ygutfreund at draper.com> 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20070103/9d2e9bfe/attachment.html>


More information about the Ironpython-users mailing list