tags:

views:

72

answers:

2

I need to execute code from my python script and take interpreter-style output like it's done here.

I am creating website on GAE using django, it must run user-entered code and print interpreter-style output as text.

A: 

There is an open-source console for app engine that does what you want (if I understood the question correctly). Have a look at it: http://con.appspot.com/console/

Instructions to integrate it with your application are available here.

jbochi
+1  A: 

there is code.InteractiveInterpreter available, but I think you can take an inspiration in the following simpler example:

import code

exprs = [
    'd = {}',
    'd',
    'd["x"] = 1',
    'd',
   ]

for e in exprs:
    print '>>> %s' % e
    cmd = code.compile_command(e)
    r = eval(cmd)
    if r:
        print repr(r)

producing the following output:

>>> d = {}
>>> d
{}
>>> d["x"] = 1
>>> d
{'x': 1}
mykhal