On Tue, Jan 29, 2013 at 4:13 PM, Andre Roberge <andre.roberge@gmail.com> wrote:
On Tue, Jan 29, 2013 at 4:43 PM, Francois Dion <francois.dion@gmail.com> wrote:
To test this, I grabbed a simple python text adventure from a blog ( http://livingcode.org/entries/2008-02-22_simple-text-adventure/ ) and plugged it in the web page, with minimum change, namely to convert it to Python 3.x syntax. The purpose of getting code I didn't write to test the idea, was that I knew how to code around the limitations of a web browser (event driven vs the linear approach of a typical Python script), so if my code ran ok, it wouldn't prove the suitability of the solution. So the code from the blog ran, but it's not 100% what one would expect. You can check it out for yourselves here (it's my Brython playground, on free hosting so it's not particularly fast to answer):
Having just tried it, I noticed a problem with it that I had not anticipated when thinking of using prompt for input. the information written in the textarea (?) needs to be scrolled up; however, the prompt prevents a user from doing so.
Easily solved using: cs = doc["console"] cs.scrollTop = cs.scrollHeight - cs.clientHeight I did a quick live edit on the file, if you refresh, should work. The text area will scroll as output is printed. So once that is integrated to the console output class, and combining that with the separate file for python, You could reuse the same html / brython template for all the various adventures that students write. If anybody is interested in that, I'll post the template.
A more proper way of doing this does require eliminating the while True: loop and replacing it with a function, and moving the input to the end of the function:
This works much better. The way I would describe, without looking at the code, would be like a state machine. The "world" is stored as an object; when the user enters a command, a single function call is issue [ "update(user_input)"] and some feedback is given. Impressive.
André
But it uses an onchange event. Although Brython introduces events as native to "Python", I'm still hoping to find an even better solution. Or at least wrap the code automatically. Francois -- www.pyptug.org - raspberry-python.blogspot.com - @f_dion