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)
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
The Python script waits for input:
do_loop = True
theAction = raw_input()
if theAction == 'login':
do_loop == False
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
I am using this too with pyinstaller works perfectly
Also thinking about using the Cgi implementation for Xojo Web Apps
@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
test.py is your python code unaltered
placed a button on a window with this in the action event
PythonShell.Mode = 2
PythonShell.execute "python /Users/npalardy/test.py"
- 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
PythonShell.Write "login" + EndOfLine.UNIX
PythonShell.Write "quit" + EndOfLine.UNIX
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.