B4J Question JShell and getting stdout from python

kostefar

Active Member
Licensed User
Longtime User
Dear All,

I´m running a .py script where the relevant part looks like this:

B4X:
def callback(update):
    print('I received', update)
    sys.stdout.write('I received stdout', update)
client.add_event_handler(callback)
client.idle()

It´s a script that every time a message is received will show it. That works fine from a DOS prompt. So I´m trying to access it from within b4j via jshell:

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    shl.Initialize("shl", "python", Array As String("C:\python\telethon\telethontest.py"))
shl.RunWithOutputEvents(-1)

End Sub

Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
Log (StdOut)
    Log (StdErr)
    Log (ExitCode)
    Log(Success)
End Sub

I was hoping that it´d just sit there and output whenever a message comes through (haven´t found any examples where you´ll have to use a loop in such a situation, so I suppose this is how it works) but all I get is "true" and then the exitcode 1. In other words, I don´t get anything from stdout. As you can see I even added sys.stdout.write - originally there was only print. I´m no python expert at all though, so I don´t know if there´s more in the script which needs to be setup first before it will work, or it´s something I need to amend in b4j.

Any ideas?

Thanks!

PS: I do know about the jpython library, but read that it´s got its limitations and upon trying the example provided resulting in a file not found error, pointed out by another user as well but said to be fixed in a later version, I should I should stick to the above which I know works.
 

DonManfred

Expert
Licensed User
Longtime User
From a app from me. t is a timer which is started after i started the shell command

B4X:
ub btnGO_Click
    Dim args As List
    args.Initialize
    'args.Add("-oF")
    'args.Add("GTIFF")
    args.Add("-o")
    args.Add(lblDest.Text)
    For Each item As String In lv.Items
        args.Add(item)
    Next
    shl.Initialize("shl", "gdal_merge.bat" , args)
    shl.WorkingDirectory = "C:\OSGeo4W64\"
    shl.Run(-1)  ' 
    running = True
    t.Enabled = True    ' Enable timer which gets the temporary output
End Sub
Sub Timer_Tick
    If running  Then
        memo.Text = shl.GetTempOut
    End If
End Sub
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
From a app from me. t is a timer which is started after i started the shell command

B4X:
ub btnGO_Click
    Dim args As List
    args.Initialize
    'args.Add("-oF")
    'args.Add("GTIFF")
    args.Add("-o")
    args.Add(lblDest.Text)
    For Each item As String In lv.Items
        args.Add(item)
    Next
    shl.Initialize("shl", "gdal_merge.bat" , args)
    shl.WorkingDirectory = "C:\OSGeo4W64\"
    shl.Run(-1)  '
    running = True
    t.Enabled = True    ' Enable timer which gets the temporary output
End Sub
Sub Timer_Tick
    If running  Then
        memo.Text = shl.GetTempOut
    End If
End Sub

Thanks, will see if this could come in handy!
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
I don´t think that this is possible. I don´t know if jshell is providing Console-fweatures. I would guess no.
I think it is:

https://www.b4x.com/android/forum/threads/question-about-jshell.85868/#post-547814

So the response I get is "Please enter your phone number:" which is normal, hence I do this:

B4X:
Sub Button1_MouseClicked (EventData As MouseEvent)

    Dim bc As ByteConverter
    Dim strng As String = "999999999"
     Dim data() As Byte = bc.StringToBytes(strng, "UTF-8")
    shl.WriteToInputStream(data)
End Sub

That aint my phone no. of course, but it should produce a response from the host whether if it´s right or wrong. I get nothing though.

I added shl.InputStreamEnabled = True in AppStart as well.

EDIT: Hah! I just had to add & CRLB after the phonenumber :)

B4X:
Sub Button1_MouseClicked (EventData As MouseEvent)

    Dim bc As ByteConverter
    Dim strng As String = "999999999" & CRLB
     Dim data() As Byte = bc.StringToBytes(strng, "UTF-8")
    shl.WriteToInputStream(data)
End Sub
 
Last edited:
Upvote 0
Top