### Python Script by B4J

This is an example of running a Python script located on a Raspberry Pi via B4J Console Application.
The Python script outputs the UTC time which is logged by the B4J application.
'Non-UI application (console / server application)
'B4JHowto running a Python script on the Raspberry Pi.
'The Python script is given as argument for the B4J non-ui application.
'#Python Script gettime.py
'#Purpose: Get & print the utc time
'#20160319
'from DateTime import DateTime
'utc_time = DateTime.utcnow()
'print(utc_time)
'#Output: 2016-03-19 09:22:27.483388
'
'print(utc_time.strftime("%a, %d %b %Y %H:%M:%S"))
'#Output: Sat, 19 Mar 2016 09:30:15
'External Libraries used: jShell
#Region Project Attributes
'Set the command line argurment which is the Python script located in the dirapps folder.
#CommandLineArgs: gettime.py
#MergeLibraries: True
#End Region
`Sub Process_Globals
'Non defined
End Sub`
`Sub AppStart (Args() As String)
Log($"Application started with Python Script as argument: ${Args(0)}"$)
'Start the python script
RunPythonScript(Args(0))
'Do not forget to start the message loop as this is a B4J non-ui application!!!
StartMessageLoop
End Sub`
'Run the python script from the raspberry pi folder /home/pi/python
`Sub RunPythonScript(script As String)
Dim shl As Shell
shl.Initialize("shl", "python", Array As String(script))
shl.WorkingDirectory = "/home/pi/python"
shl.Run(-1)
End Sub`
'Handle process completion
`Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success And ExitCode = 0 Then
Log("Python Script successfully executed. Output:")
Log(StdOut)
Else
Log("Python Script executed with Errors. Output:")
Log(StdErr)
End If
ExitApplication
End Sub`

Changed: , Category: Raspberry Pi Sample Projects, File: T6C238M1742.htm, ID: 1742