Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: pygtk - the output of bash commands on a TextView widget

  1. #1
    Join Date
    May 2005
    Location
    Lecce, Italy
    Beans
    6,168
    Distro
    Ubuntu

    Question pygtk - the output of bash commands on a TextView widget

    Hi, I would like to know how I can redirect the output of bash commands (e.g. apt-get install vim-gnome) to a TextView widget using pygtk.

    For example if I press a button (see the attached code) I would like to call a few commands in bash (os.popen doesn't seem to handle well commands such as "apt-get install vim-gnome" ) and display their output on the TextView widget.


    If this is not possible with a TextView widget I would appreciate any suggestion about an alternative. An explanation about how I could use python-vte in this case would be very welcome.

    I have attached the file project.tar.gz which contains the file written in Python (myapp.py) and the .glade file

    Thanks in advance.
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2004
    Location
    Clemson, SC
    Beans
    271

    Re: pygtk - the output of bash commands on a TextView widget

    Quote Originally Posted by tseliot View Post
    An explanation about how I could use python-vte in this case would be very welcome.
    easy. here's a snippet from a program i wrote a while back.
    Code:
             v = vte.Terminal()
             v.fork_command('bash')
             v.feed_child('ssh %s tail -f %s.o%s \n' % (host,jname,id))
             v.show()
             notebook.append_page(v, tab_label=gtk.Label(nodeid))

  3. #3
    Join Date
    May 2005
    Location
    Lecce, Italy
    Beans
    6,168
    Distro
    Ubuntu

    Re: pygtk - the output of bash commands on a TextView widget

    Thanks for your reply.

    VTE works great now

  4. #4
    Join Date
    Jan 2005
    Location
    Toronto, Ontario, Canada
    Beans
    2,204

    Re: pygtk - the output of bash commands on a TextView widget

    any way to get rid of the prompt?

    eg
    Code:
    v = vte.Terminal()
    v.fork_command('bash')
    v.feed_child('whoami\n')
    v.feed_child('echo test\n')
    v.show()
    outputs
    whoami
    echotest
    earobinson@minusone:/media/data/dev/propensity/src/sandbox$ whoami
    earobinson
    earobinson@minusone:/media/data/dev/propensity/src/sandbox$ echo test
    test
    but I want
    earobinson
    test
    Edward A Robinson -- www.earobinson.org

  5. #5
    Join Date
    Jan 2005
    Location
    Toronto, Ontario, Canada
    Beans
    2,204

    Re: pygtk - the output of bash commands on a TextView widget

    Ok so I have found another problem

    Code:
    #!/usr/bin/env python
    
    import os
    import gtk
    import vte
    import time
    from subprocess import Popen, PIPE
    
    def show_callback(terminal):
        terminal.feed_child('cd /\n')
        terminal.feed_child('whoami\n')
        terminal.feed_child('echo test\n')
        for ii in range (10):
            terminal.feed_child('echo ' + str(ii + 1)  + '\n')
            time.sleep(1)
    
    def write(terminal, text):
        x, y = terminal.get_cursor_position()
        terminal.feed(text + '\n', len(text) + 1)
    
    window = gtk.Window()
    window.connect('destroy', lambda w: gtk.main_quit())
    
    terminal = vte.Terminal()
    terminal.connect("show", show_callback)
    
    child_pid = terminal.fork_command()
    
    #write(terminal, '1234567890')
    
    window.add(terminal)
    window.show_all()
    gtk.main()
    dose not count down, instead it waits 10 seconds then prints everything, any way to make it happen in real time?
    Last edited by earobinson; September 4th, 2007 at 08:17 PM.
    Edward A Robinson -- www.earobinson.org

  6. #6
    Join Date
    May 2005
    Location
    Lecce, Italy
    Beans
    6,168
    Distro
    Ubuntu

    Re: pygtk - the output of bash commands on a TextView widget

    Here is the solution to your problem.

    create a file named "ear1" and make it look like this:
    Code:
    #!/bin/bash
    
    NUM="0"
    
    cd /
    whoami
    echo test
    
    while [ $NUM -lt 10 ]; do
        echo $[$NUM+1]
        NUM=$[$NUM + 1]
        sleep 1
    done
    type:
    Code:
    chmod +x ear1
    then make your python file look like the following:
    Code:
    #!/usr/bin/env python
    
    import os
    import gtk
    import vte
    import time
    from subprocess import Popen, PIPE
    
        
    def show_callback(terminal):
        terminal.feed_child('./ear1')
    
    def write(terminal, text):
        x, y = terminal.get_cursor_position()
        terminal.feed(text + '\n', len(text) + 1)
    
    window = gtk.Window()
    window.connect('destroy', lambda w: gtk.main_quit())
    
    terminal = vte.Terminal()
    terminal.fork_command()
    terminal.connect("show", show_callback)
    
    
    
    #write(terminal, '1234567890')
    
    window.add(terminal)
    window.show_all()
    gtk.main()
    in other words you will have to make it run a bash script

  7. #7
    Join Date
    Jan 2005
    Location
    Toronto, Ontario, Canada
    Beans
    2,204

    Re: pygtk - the output of bash commands on a TextView widget

    hum, so there is no way I could have a user inputting commands in one terminal and the results coming out on the other?

    UPDATE:
    The problem is then I need to know all the commands that I want to run when I launch the terminal, there seems to be no way to make it interactactive(sp) or monitor the success of each command I issue to the terminal is there?

    Ex like a gtk.ProgressBar that monitored each command like synaptic does.

    Thanks for the feedback
    Last edited by earobinson; September 4th, 2007 at 11:00 PM.
    Edward A Robinson -- www.earobinson.org

  8. #8
    Join Date
    May 2005
    Location
    Lecce, Italy
    Beans
    6,168
    Distro
    Ubuntu

    Re: pygtk - the output of bash commands on a TextView widget

    Quote Originally Posted by earobinson View Post
    hum, so there is no way I could have a user inputting commands in one terminal and the results coming out on the other?

    UPDATE:
    The problem is then I need to know all the commands that I want to run when I launch the terminal, there seems to be no way to make it interactactive(sp) or monitor the success of each command I issue to the terminal is there?
    you can monitor the success of each command by keeping a log and parsing each line you append to the log on the fly.

    I'm not sure as to what you mean by interactive.

  9. #9
    Join Date
    Jan 2005
    Location
    Toronto, Ontario, Canada
    Beans
    2,204

    Re: pygtk - the output of bash commands on a TextView widget

    I guess the idea in my head was two guis, one that you can type commands into and the other (the terminal) that displays the commands.

    maybe this isent possible?

    But it seems like I should be able to issue commands to the terminal and get a response from them.

    I have made a simple example., What I would like is to push the press me button and then not be able to interact any more until the command has ran to completion.

    eventually I would add a gtk.entry and be able to run any command.

    I guess what your saying is I should send but the program I want to run and then a output log file to signify that the program is done?
    Attached Files Attached Files
    Edward A Robinson -- www.earobinson.org

  10. #10
    Join Date
    May 2005
    Location
    Lecce, Italy
    Beans
    6,168
    Distro
    Ubuntu

    Re: pygtk - the output of bash commands on a TextView widget

    Quote Originally Posted by earobinson View Post
    I have made a simple example., What I would like is to push the press me button and then not be able to interact any more until the command has ran to completion.

    eventually I would add a gtk.entry and be able to run any command.

    I guess what your saying is I should send but the program I want to run and then a output log file to signify that the program is done?
    You can gray out the text are when the user can type the command, pass the command to VTE as you know and then look for a certain line (which you will set) in the log.

    here is an example from Envy:
    Code:
    def log_output(self,term):
            column,row = self.term.get_cursor_position()
            if self.r != row:
                off = row-self.r
                text = self.term.get_text_range(row-off,0,row-1,-1,self.capture_text)
                self.r=row
                text = text.strip()
                if "\n" not in text or text[-1] != "\n":
                    text += "\n"
                ##self.logged += text
                self.completetxt.append(text)
                a = self.logged
                error = re.compile('.*ENVY ERROR.*\n')
                success = re.compile('.*ENVY:.*Operation.*Completed.*')
                for line in self.completetxt:#self.logged:
                    m1 = error.match(line)
                    m2 = success.match(line)
                if m1:
                    self.error = 'error'
                    sep = ''#write a logfile
                    logtext = sep.join(self.completetxt)
                    logfile = open(self.logtxtfile, 'w')
                    logfile.write(logtext)
                    logfile.close()
                    self.errorcheck()
                if m2:
                    self.error = 'noerror'
                    sep = ''#write a logfile
                    logtext = sep.join(self.completetxt)
                    logfile = open(self.logtxtfile, 'w')
                    logfile.write(logtext)
                    logfile.close()
                    self.errorcheck()
    in this example the method looks for strings such as "ENVY ERROR" or "ENVY:.*Operation.*Completed" and acts accordingly. For example if self.error == 'noerror' you can trigger a specific action.

    You might use strings such as "DONE", etc. so that if you find such a string you can allow the user to type commands again in the text input area.


    EDIT: in case you're wonder what self.term is: self.term = vte.Terminal()
    Last edited by tseliot; September 4th, 2007 at 11:47 PM.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •