[Edu-sig] Interactive tutorial

Brent Burley Brent.Burley@disney.com
Fri, 08 Jun 2001 16:24:26 -0700


Jeffrey Elkner wrote:
> This is way too cool!!  I like the idea of
> www.python.org/cgi-bin/eval/python<version>, but if that isn't
> practical, Yorktown High School (as one of the major beneficiaries of
> this project ;-) would be glad to donate a server to it.  Just let me
> know.

Great!  The minimum to get started is a cgi script that can execute
python in a restricted environment.  Here's a condensed version of the
one from David Beazley's book:

eval.py:
--------
#!/usr/local/bin/python
import rexec, cgi, sys, string, resource
code = cgi.FieldStorage()['code'].value
code = string.replace(code, '\015', '') + '\n'
sys.stderr = sys.stdout
print 'Content-type: text/plain\nResult:\n'
class CGIExec(rexec.RExec):
    def r_open(*args):
	raise SystemError, 'open not supported'
r = CGIExec()
# limit to 4mb and 10 cpu secs
resource.setrlimit(resource.RLIMIT_DATA, (4000000,4000000))
resource.setrlimit(resource.RLIMIT_CPU, (10,10))
r.s_exec(code)

It has everything you need to be secure including cpu and memory
limits.  This can be tested locally using CGIHTTPServer.  Make a
directory called cgi-bin and put the above script in it.  Then from the
directory containing cgi-bin run: 'python -c import CGIHTTPServer;
CGIHTTPServer.test()'.  It should also be installable into any http
server that supports python cgi scripts.

You'll need an html form to post the python code to the server.  This
one is sufficient:

codeform.html:
--------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
    "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><title>Python Evaluator</title></head>
<body><form action="http://localhost:8000/cgi-bin/eval.py"
method=post><p>
<textarea name=code rows=10 cols=80>print 'hello world'</textarea><br>
<input type=submit value="Evaluate">
</form></body></html>

And you'll eventually want to wrap it up in a frameset with the
tutorial:

pytutor.html:
-------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN"
    "http://www.w3.org/TR/REC-html40/frameset.dtd">
<html><head><title>ThinkCSpy Interactive Version</title></head>
<frameset rows="70%,30%">
<frame name=lesson src='http://www.ibiblio.org/obp/thinkCSpy/'>
<frame name=exercise src='codeform.html'>
<noframes>
<body>Sorry, your browser doesn't support frames</body>
</noframes>
</frameset>
</html>


The exercises will all be variants on codeform.html that are linked into
the main document.  The links should specify "target=exercise" to bring
them up in the exercise frame.

Of course, there's a lot of improvements that would need to be made. 
The output of eval.py could be enhanced a lot.  Ideally, it would
execute the script a block at a time and interleave the code with the
results.  It should also automatically print the result of expressions
like the interactive interpreter does.  Syntax coloring would be nice. 
Error handling could be friendlier.  Tracebacks should be truncated down
to a meaningful level and perhaps eliminated altogether as they are
somewhat intimidating and probably not needed for this kind of
tutorial.  And the output could use some pretty printing too.  As for
functionality, the pseudo-files Chris suggested are a neat idea (hard to
teach file I/O otherwise).  All this could be done with a modest effort.

And then there's the content...

Once a functional eval server is up for everyone to play with, I can
post more examples and I imagine the rest will come together fairly
quickly.  ;-)

	Brent