How to interactively communicate with Python script

  1. 5 years ago

    Beatrix W

    20 Apr 2014 Pre-Release Testers Europe (Germany)

    Hi,

    how do I interactively communicate with a Python script?

    In Xojo I have a simple shell as property:

     PythonShell = New Shell
      PythonShell.Mode = 2
      
      PythonShell.execute "python /Users/beatrixwillius/PycharmProjects/test/test.py"
      PythonShell.Write "login" + Chr(13)

    which I read with a timer for now

     If PythonShell <> Nil Then
        dim theResult as String = PythonShell.ReadAll
        if theResult <> "" then
          TextArea1.Text = TextArea1.Text + theResult
        end if
      End If

    The Python script waits for input:

    print 'start'
    do_loop = True
    while do_loop:
        theAction = raw_input()
        if theAction == 'login':
            print 'login'
        else:
            do_loop == False
            break
        #end if
    #wend

    I get the result "start" in Xojo, but no "login". What am I doing wrong here?

    Mac OS 10.9, Xojo 2013r4.

    DONT use Chr(13) - its the wrong line ending on OS X

    Here's what I did with your code
    1) test.py is your python code unaltered

    2) placed a button on a window with this in the action event

    PythonShell.Mode = 2 PythonShell.execute "python /Users/npalardy/test.py"

    3) placed a shell object on a window and named it PythonShell.Write

    implemented the DataAavilable event as
    dim s as string = trim(me.ReadAll)

      select case s
        
      case "start"
        PythonShell.Write "login" + EndOfLine.UNIX
        
      case "login"
        PythonShell.Write "quit" + EndOfLine.UNIX
        
      end
      
  2. Eli O

    is not verified 20 Apr 2014 Europe (Berlin, Germany)

    The python code is waiting for user input on the line theAction = raw_input(). How will this input happen? You have a shell invisible to the user.

  3. Beatrix W

    20 Apr 2014 Pre-Release Testers Europe (Germany)

    @Eli: I'm a total beginner with Python. What else can I use?

  4. Lee P

    is not verified 20 Apr 2014 Hertfordshire, UK

    I'm a total beginner with Python. What else can I use?

    Can you post the Python script it may help to see what you are trying to accomplish?

  5. Rick A

    20 Apr 2014 (Brazil. GMT-3:00)

    If you intend to do some processing in Python, why don't you write a small server and talk to it connecting to it's port?

    Here is a start point: https://wiki.python.org/moin/DocXmlRpcServer

  6. Lee P

    is not verified 20 Apr 2014 Hertfordshire, UK
    Edited 5 years ago

    :)

    Here is a start point: https://wiki.python.org/moin/DocXmlRpcServer

    I am using this too with pyinstaller works perfectly

    Also thinking about using the Cgi implementation for Xojo Web Apps

    https://wiki.python.org/moin/AutoXmlRpcCgi

  7. Beatrix W

    20 Apr 2014 Pre-Release Testers Europe (Germany)

    @Lee: thanks, but this looks like way beyond what I know about Python at the moment. How would I use this in Xojo?

  8. Eli O

    is not verified 20 Apr 2014 Europe (Berlin, Germany)

    @Beatrix W I'm a total beginner with Python. What else can I use?

    The issue here is not about Python, it is about two independent programs running on OS X, your application and the Python console.

    You could run the Python script in Terminal. And then communicate between Xojo and Python over sockets.
    Or switch to AppleScript. Might work since Xojo supports AppleScript ("might" because I never used it myself).

  9. Norman P

    20 Apr 2014 Xojo Inc Answer North of 50
    Edited 5 years ago

    DONT use Chr(13) - its the wrong line ending on OS X

    Here's what I did with your code
    1) test.py is your python code unaltered

    2) placed a button on a window with this in the action event

    PythonShell.Mode = 2 PythonShell.execute "python /Users/npalardy/test.py"

    3) placed a shell object on a window and named it PythonShell.Write

    implemented the DataAavilable event as
    dim s as string = trim(me.ReadAll)

      select case s
        
      case "start"
        PythonShell.Write "login" + EndOfLine.UNIX
        
      case "login"
        PythonShell.Write "quit" + EndOfLine.UNIX
        
      end
      
  10. Beatrix W

    20 Apr 2014 Pre-Release Testers Europe (Germany)

    Thanks, Normal! I'll test that later.

  11. Beatrix W

    21 Apr 2014 Pre-Release Testers Europe (Germany)

    Works great. I got the chr(13) from the example Interactive Shell and always forget which EndOfLine is which.

or Sign Up to reply!