[IronPython] Get a string from JavaScript to IronPython

Michael Foord fuzzyman at voidspace.org.uk
Tue Jan 6 16:07:35 CET 2009


Kristian wrote:
> I have a code editor written in JavaScript on my page and I want to
> send the code that it is typed in as a string to IronPython so that it
> can be executed from IronPython. I got this to work in C# but not in
> IronPython. Below is the JavaScript I use:
>
>     <script type="text/javascript">
>
>
>                  var textarea = document.getElementById('code');
>                  var editor = new CodeMirror(CodeMirror.replace
> (textarea), {
>                  height: "350px",
>                      width: "500px",
>                      content: textarea.value,
>                      parserfile: ["tokenizejavascript.js",
> "parsejavascript.js"],
>                      stylesheet: "css/jscolors.css",
>                      path: "js/",
>                      autoMatchParens: true
>                  });
>
>                  function invokeManagedCode() {
>                      var form = document.getElementById("editor");
>                      var user = form.user.value;
>                      var control = document.getElementById("sl2");
>                      control.Content.codeProvider.Code(editor.getCode
> ());
>                  }
>     </script>
>
> The code replaces a standard HTML-textarea with the JavaScript editor.
> The function "invokeManagedCode()" sends the text in the code editor
> to a C# method. The C# code is really simple:
>
> namespace htmlDom1
> {
>     public partial class Page : UserControl
>     {
>         public Page()
>         {
>             InitializeComponent();
>             HtmlPage.RegisterScriptableObject("codeProvider", this);
>         }
>
>         [ScriptableMember()]
>         public void Code(string codeString)
>         {
>             this.Message.Text = codeString;
>         }
>
>     }
> }
>
>
> How can I achieve the same in IronPython? I tried to do exactly the
> same as in C#. See below:
>
> [ScriptableMember()]
>   
Although this is valid Python syntax, it doesn't do what you think... 
(It actually created a new list - but as you don't assign a name to it, 
it will be garbage collected.)

Unfortunately we can't apply attributes to Python classes. I get round 
this particular problem by creating stub classes in C# and inheriting 
from them in Python. See:

http://www.voidspace.org.uk/ironpython/silverlight/scriptable.shtml

All the best,

Michael

> def Code(codeString):
>     root.sl2TextBox.Text = codeString # Show that it works by writing
> the string
>                                                        # from the
> javascript code editor to a Silverlight textbox
>
> HtmlPage.RegisterScriptableObject("codeProvider", Code)
>
>
> But this doesn't work. How to do it properly?
>
> Thanks for help!
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


-- 
http://www.ironpythoninaction.com/




More information about the Ironpython-users mailing list