[IronPython] Python in the Browser updates

Michael Foord fuzzyman at voidspace.org.uk
Mon Jul 21 23:00:35 CEST 2008


Another alternative is for me to work with Dan Eloff, the author of this 
project which is pure Python and *very* slick...

http://code.google.com/p/silvershell/

Michael

Jimmy Schementi wrote:
> On Jul 20, 2008, at 7:53 AM, "Michael Foord"
> <fuzzyman at voidspace.org.uk> wrote:
>
>   
>> Jimmy Schementi wrote:
>>     
>>> Michael Foord wrote:
>>>
>>>       
>>>> Jimmy Schementi wrote:
>>>>
>>>>         
>>>>> Michael, question for you:
>>>>>
>>>>>
>>>>>
>>>>>           
>>>>>> On every keypress Javascript calls into IronPython (via the C#!).
>>>>>>
>>>>>>
>>>>>>             
>>>>> How come you used C# to handle the onkeypress event? This should do
>>>>>
>>>>>           
>>>> it in Python:
>>>>
>>>>         
>>>> Did you look at the code to see why? I'd be really interested to
>>>> know
>>>> if
>>>> I can do what I'm doing from Python inside Silverlight, but I
>>>> doubt it.
>>>> There's not much code in total, so it should be easy to follow.
>>>>
>>>>         
>>> Yep, I looked at it, and I'm pretty sure you can do everything from
>>> python. All the native Javascript functions are callable from
>>> python, or any .NET language, especially with
>>> HtmlPage.window.eval(), so there shouldn't be any limitation.
>>>
>>>
>>>       
>>>> I actually handle the onkeypress event from Javascript
>>>> (onkeypress.js).
>>>> This calls into 'getSelection' (cursor_pos.js) to work out the
>>>> current
>>>> selection in the textarea. Naturally this is easy to do in Firefox /
>>>> Safari and scarily awful in IE. :-)
>>>>
>>>>         
>>> Attaching the event is simple:
>>>
>>> from System.Windows.Browser.HtmlPage import Document
>>> from System.Windows.Input import KeyEventHandler
>>>
>>> Document.myTextAreaID.AttachEvent("onkeypress",
>>> KeyEventHander(onKeyPress))
>>>
>>> def onKeyPress(sender, eventArgs):
>>>  # Do something with eventArgs.CharacterCode
>>>  pass
>>>
>>> For converting the javascript to python, there are some special
>>> javascript variables that are in different places, like "window" is
>>> HtmlPage.Window, etc.
>>>
>>>
>>>       
>> I've used the event attaching stuff before, but some of the Javascript
>> is quite fiddly so I wasn't sure there was much benefit in moving it
>> into Python.
>>
>> My next version will probably be based on the Javascript UI of Try
>> Ruby
>> by Why the Luck Stiff (he has given me permission to use his JS):
>>
>> http://tryruby.hobix.com/
>>
>> Michael
>>     
>
> Well, I have a console ui in ruby at silverline.schementi.com/tryruby,
> so if you change your mind feel free to use that. It's not perfect
> yet, but I'm working on it.
>
>   
>>>> Having worked these out (and shortcut a few specific scenarios -
>>>> like
>>>> ctrl-A, ctrl-Z and ctrl-C), the Javascript then calls into
>>>> IronPython
>>>> with the selection start, selection end, and the character that was
>>>> pressed. I use C# to expose a scriptable type that Javascript can
>>>> call.
>>>>
>>>>         
>>> If you hooked all the events with Python, you wouldn't need to call
>>> back into it from Javascript. This is why I prefer doing everything
>>> with Python, so I avoid having a C# shim.
>>>
>>>
>>>       
>>>> I use attaching events from Python to provide the sample code. There
>>>> are
>>>> three links above the console and Python handles the 'onclick'
>>>> event to
>>>> swap the examples around.
>>>>
>>>>         
>>> Basically, if there is something pretty basic you can't do in
>>> Python that you can do from browser Javascript, it's a bug! =)
>>>
>>>
>>>       
>>>> Michael Foord
>>>>
>>>>
>>>>         
>>>>> <textarea id="code"></textarea>
>>>>> <div id="result"></div>
>>>>>
>>>>> from System import EventHandler
>>>>> from System.Windows.Browser import Htmlpage, HtmlEventArgs
>>>>>
>>>>> document = System.Windows.Browser.HtmlPage.Document
>>>>> def onkeypress(s, e):
>>>>>  document.result.innerHTML += "KeyPress<br />"
>>>>> document.code.AttachEvent("onkeypress",
>>>>>
>>>>>           
>>>> EventHandler[HtmlEventArgs](onkeypress))
>>>>
>>>>         
>>>>> Anyway, just wondering if anything was blocking you from hooking
>>>>> HTML
>>>>>
>>>>>           
>>>> events with Python.
>>>>
>>>>         
>>>>> ~js
>>>>>
>>>>>
>>>>>
>>>>>           
>>>>>> -----Original Message-----
>>>>>> From: users-bounces at lists.ironpython.com [mailto:users-
>>>>>> bounces at lists.ironpython.com] On Behalf Of Michael Foord
>>>>>> Sent: Tuesday, June 17, 2008 2:52 PM
>>>>>> To: Discussion of IronPython
>>>>>> Subject: Re: [IronPython] Python in the Browser updates
>>>>>>
>>>>>> Hello all,
>>>>>>
>>>>>> Sorry for the noise - but tomorrow I hope to package this for a
>>>>>>
>>>>>>             
>>>> release
>>>>
>>>>         
>>>>>> and if any of you get the chance to give me any feedback before
>>>>>>
>>>>>>             
>>>> then.
>>>>
>>>>         
>>>>>> http://www.voidspace.org.uk/ironpython/silverlight-
>>>>>>
>>>>>>             
>>>> console/console.html
>>>>
>>>>         
>>>>>> I think I have fixed all the basic usability issues (it is no
>>>>>> longer
>>>>>> possible to screw up the console so that it refuses further input
>>>>>>
>>>>>>             
>>>> for
>>>>
>>>>         
>>>>>> example). To *really* fix input I ought to move execution into its
>>>>>>
>>>>>>             
>>>> own
>>>>
>>>>         
>>>>>> thread so that we can interrupt infinite loops [1] - but that is a
>>>>>> bigger change so I am resisting it for the moment.
>>>>>>
>>>>>> The big change is that I have added some simple code snippets - as
>>>>>>
>>>>>>             
>>>> an
>>>>
>>>>         
>>>>>> example of how it could be used in tutorials or for teaching
>>>>>> Python.
>>>>>>
>>>>>> It is also easy to customize - the source code (200 lines of
>>>>>> Python,
>>>>>>
>>>>>>             
>>>> 70
>>>>
>>>>         
>>>>>> lines of Javascript, 25 lines of C#) shows how to prepopulate the
>>>>>> execution context and show the first few lines of code in the
>>>>>> interpreter.
>>>>>>
>>>>>> You can get the sourcecode from the repository of course:
>>>>>>
>>>>>> http://code.google.com/p/pythoninthebrowser/
>>>>>>
>>>>>> All the best,
>>>>>>
>>>>>> Michael Foord
>>>>>>
>>>>>> [1] and then decide whether on windows a ctrl-c should send a
>>>>>>
>>>>>>             
>>>> keyboard
>>>>
>>>>         
>>>>>> interrupt or copy to the clipboard...
>>>>>>
>>>>>> Michael Foord wrote:
>>>>>>
>>>>>>
>>>>>>             
>>>>>>> For those who are interested, I've updated "Python in the
>>>>>>> Browser".
>>>>>>>
>>>>>>> Both the repository:
>>>>>>>
>>>>>>> http://code.google.com/p/pythoninthebrowser/
>>>>>>>
>>>>>>> And the online demo:
>>>>>>>
>>>>>>> http://www.voidspace.org.uk/ironpython/silverlight-
>>>>>>>
>>>>>>>
>>>>>>>               
>>>>>> console/console.html
>>>>>>
>>>>>>
>>>>>>             
>>>>>>> There are a few minor but nice improvements:
>>>>>>>
>>>>>>> * The Javascript now works with IE 7!
>>>>>>> * The textarea scrolls as input is printed
>>>>>>> * The context window is disabled to prevent you pasting over the
>>>>>>>
>>>>>>>
>>>>>>>               
>>>>>> input
>>>>>>
>>>>>>
>>>>>>             
>>>>>>> area - not sure if this is really an improvement though...
>>>>>>> * The input area is cleared on load - to stop browsers like
>>>>>>> Firefox
>>>>>>> and Safari repopulating the textarea for us on browser refresh
>>>>>>> * Recursion limit is set
>>>>>>>
>>>>>>> I think it is basically in a useful state now... I might push
>>>>>>> out a
>>>>>>> '0.1' release from the current SVN head.
>>>>>>>
>>>>>>> All the best,
>>>>>>>
>>>>>>> Michael Foord
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>               
>>>>>> --
>>>>>> http://www.ironpythoninaction.com/
>>>>>> http://www.theotherdelia.co.uk/
>>>>>> http://www.voidspace.org.uk/
>>>>>> http://www.ironpython.info/
>>>>>> http://www.resolverhacks.net/
>>>>>>
>>>>>> _______________________________________________
>>>>>> Users mailing list
>>>>>> Users at lists.ironpython.com
>>>>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>>>>
>>>>>>
>>>>>>             
>>>>> _______________________________________________
>>>>> Users mailing list
>>>>> Users at lists.ironpython.com
>>>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>>>
>>>>>
>>>>>           
>>>> _______________________________________________
>>>> Users mailing list
>>>> Users at lists.ironpython.com
>>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>>
>>>>         
>>> _______________________________________________
>>> Users mailing list
>>> Users at lists.ironpython.com
>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>
>>>       
>> --
>> http://www.ironpythoninaction.com/
>> http://www.voidspace.org.uk/
>> http://www.trypython.org/
>> http://www.ironpython.info/
>> http://www.theotherdelia.co.uk/
>> http://www.resolverhacks.net/
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>     
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/
http://www.trypython.org/
http://www.ironpython.info/
http://www.theotherdelia.co.uk/
http://www.resolverhacks.net/




More information about the Ironpython-users mailing list