开发者

Python: Networked IDLE/Redo IDLE front-end while using the same back-end?

Is there any existing web app that lets multiple users work with an interactive IDLE type session at once?

Something like:

IDLE 2.6.4
Morgan: >>> letters = list("abcdefg")
Morgan: >>> # now, how would you iterate over letters?
Jack: >>> for char in letters:
    pri开发者_C百科nt "char %s" % char


char a
char b
char c
char d
char e
char f
char g
Morgan: >>> # nice nice

If not, I would like to create one. Is there some module I can use that simulates an interactive session? I'd want an interface like this:

def class InteractiveSession():
    ''' An interactive Python session '''

    def putLine(line):
        ''' Evaluates line '''
        pass

    def outputLines():
        ''' A list of all lines that have been output by the session '''
        pass

    def currentVars():
        ''' A dictionary of currently defined variables and their values '''
        pass

(Although that last function would be more of an extra feature.)

To formulate my problem another way: I'd like to create a new front end for IDLE. How can I do this?

UPDATE: Or maybe I can simulate IDLE through eval()?

UPDATE 2: What if I did something like this:

  • I already have a simple GAE Python chat app set up, that allows users to sign in, make chat rooms, and chat with each other.

  • Instead of just saving incoming messages to the datastore, I could do something like this:


def putLine(line, user, chat_room):
''' Evaluates line for the session used by chat_room '''

# get the interactive session for this chat room
curr_vars = InteractiveSession.objects.where("chatRoom = %s" % chat_room).get()

result = eval(prepared_line, curr_vars.state, {})

curr_vars.state = curr_globals

curr_vars.lines.append((user, line))
if result:
    curr_vars.lines.append(('SELF', result.__str__()))

curr_vars.put()

The InteractiveSession model:

def class InteractiveSession(db.Model):


 # a dictionary mapping variables to values
    # it looks like GAE doesn't actually have a dictionary field, so what would be best to use here?
    state = db.DictionaryProperty()

    # a transcript of the session
    #
    # a list of tuples of the form (user, line_entered)
    #
    # looks something like:
    #
    # [('Morgan', '# hello'),
    #  ('Jack', 'x = []'),
    #  ('Morgan', 'x.append(1)'),
    #  ('Jack', 'x'),
    #  ('SELF', '[1]')]
    lines = db.ListProperty()

Could this work, or am I way off/this approach is infeasible/I'm duplicating work when I should use something already built?

UPDATE 3: Also, assuming I get everything else working, I'd like syntax highlighting. Ideally, I'd have some API or service I could use that would parse the code and style it appropriately.

for c in "characters":

would become:

<span class="keyword">for</span> <span class="var">c</span> <span class="keyword">in</span> <span class="string>"characters"</span><span class="punctuation">:</span>

Is there a good existing Python tool to do this?


I could implement something like this pretty quickly in Nevow. Obviously, access would need to be pretty restricted since doing something like this involves allowing access to a Python console to someone via HTTP.

What I'd do is create an Athena widget for the console, that used an instance of a custom subclass of code.InteractiveInterpreter that is common to all users logged in.

UPDATE: Okay, so you have something chat-like in GAE. If you just submit lines to a code.InteractiveInterpreter subclass that looks like this, it should work for you. Note that the interface is pretty similar to the InteractiveSession class you describe:

class SharedConsole(code.InteractiveInterpreter):
    def __init__(self):
        self.users = []

    def write(self, data):
        # broadcast output to connected clients here
        for user in self.users:
            user.addOutput(data)

class ConnectedUser(object):
    def __init__(self, sharedConsole):
        self.sharedConsole = sharedConsole
        sharedConsole.users.append(self) # reference look, should use weak refs

    def addOutput(self, data):
        pass # do GAE magic to send data to connected client

    # this is a hook for submitted code lines; call it from GAE when a user submits code
    def gotCommand(self, command):
        needsMore = self.sharedConsole.runsource(command)
        if needsMore:
            pass # tell the client to change the command line to a textarea
                 # or otherwise add more lines of code to complete the statement


The closest Python interpreter I know of to what you are looking for, in terms of interface, is DreamPie. It has separate input and output areas, much like a chat interface. Also, DreamPie runs all of the code in a subprocess. DreamPie also does completion and syntax coloring, much like IDLE, which means it doesn't just pipe input and output to/from the subprocess -- it has implemented the abstractions which you are looking for.

If you wish to develop a desktop application (not a web-app), I recommend basing your work on DreamPie and just adding the multiple-frontend functionality.

Update: For syntax highlighting (including HTML) see the Pygments project. But that is a completely different question; please ask one question at a time here.


As a proof of concept, you may be able to put something together using sockets and a command-line session.


this is likely possible with the upcoming implimentation of IPython using a 0MQ backend.


I would use ipython and screen. With this method, you would have to create a shared login, but you could both connect to the shared screen session. One downside would be that you would both appear as the same user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜