Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to interactively program a Python curses application?

Tags:

python

curses

Is there a way to create a second terminal so that all calls to curses functions operate on that, rather than in the existing terminal? I work much faster when I can try things out interactively, so I'd like to be able to run an interactive python interpreter in one terminal and see the curses output in another.

As it is, calling initscr() in an interactive window either fails (PyDev) or permanently takes away window refresh from the host (Spyder) or causes weird behavior in the console (IPython).

Is it possible to take over a different terminal using setupterm()? If so, where do I get a different TERM string to call it with?

like image 558
dan Avatar asked Aug 26 '11 18:08

dan


2 Answers

You could use code.InteractiveConsole and SocketServer to attach a python interactive shell to a socket and do your development through that. A simple example looks like:

import sys
import SocketServer
from code import InteractiveConsole

class InteractiveServer(SocketServer.BaseRequestHandler):
   def handle(self):
        file = self.request.makefile(mode='rw')
        shell = Shell(file)
        try:
           shell.interact()
        except SystemExit:
           pass


class Shell(InteractiveConsole):
    def __init__(self, file):
        self.file = sys.stdout = file
        InteractiveConsole.__init__(self)
        return

    def write(self, data):
       self.file.write(data)
       self.file.flush()

    def raw_input(self, prompt=""):
       self.write(prompt)
       return self.file.readline()

if __name__ == '__main__':
   HOST, PORT = "127.0.0.1", 9999

   server = SocketServer.TCPServer((HOST, PORT), InteractiveServer)
   server.serve_forever()

Once you've got that up and running you can connect to port 9999 from another terminal and do your thing. You can see this working in this screenshot (PNG)

The basics for using the InteractiveConsole were taken from this post. I modified it to work with the SocketServer for another project I was working on.

like image 192
cnelson Avatar answered Oct 22 '22 22:10

cnelson


I don't believe so as the curses module is mostly (totally?) implemented at the C level. It's unlikely that it would provide such hooks, although if you are familiar with the language it might be worth looking thru the source.

However while reading your question I thought of another technique which I use in other contexts. You can save a script via another terminal/editor and use a technique similar to the dnotify command (or even simple polling) to load it into your running program.

Another idea would be to use sockets to send commands over and execute them. Of course this is dangerous security-wise so take the necessary precautions.

You'll have to build some infrastructure, but it would likely be much easier than adding multiple device support to curses.

like image 37
Gringo Suave Avatar answered Oct 22 '22 23:10

Gringo Suave