[IronPython] Get a string from JavaScript to IronPython

Kristian kristian.jaksch at gmail.com
Tue Jan 6 19:33:30 CET 2009


Thanks for the reply. I actually got it to work straight away :-)

On Jan 6, 4:07 pm, Michael Foord <fuzzy... at voidspace.org.uk> wrote:
> 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
> > Us... at lists.ironpython.com
> >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
> --http://www.ironpythoninaction.com/
>
> _______________________________________________
> Users mailing list
> Us... at lists.ironpython.comhttp://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list