How to interactively communicate with Python script

Hi,

how do I interactively communicate with a Python script?

In Xojo I have a simple shell as property:

[code] PythonShell = New Shell
PythonShell.Mode = 2

PythonShell.execute “python /Users/beatrixwillius/PycharmProjects/test/test.py”
PythonShell.Write “login” + Chr(13)
[/code]

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.

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.

@Eli: 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?

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

:slight_smile:

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

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

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).

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"

  1. 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
  

Thanks, Normal! I’ll test that later.

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