Dmitry Demidenko
Dmitry Demidenko

Reputation: 3407

Executing python code from script and getting interpreter-style output

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.

Upvotes: 0

Views: 392

Answers (2)

mykhal
mykhal

Reputation: 19924

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}

Upvotes: 1

jbochi
jbochi

Reputation: 29654

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.

Upvotes: 0

Related Questions