B4J Question [Solved]How to code William's PyBridge example

Theera

Expert
Licensed User
Longtime User
Refer to this ,I 've coded as following William's example which is attached file. I have still error codea after clicked the button.
My the lastest I have edited,but I have still error code.
B4X:
Dim opt As PyOptions = Py.CreateOptions(File.Combine(File.DirApp, "C:\Python/python310/python.exe"))
 

Attachments

  • Project.zip
    3.5 KB · Views: 38
Last edited:
Solution
Use this code:
B4X:
Private Sub Fibonacci(n As Int) As PyWrapper
    Dim Code As String = $"
#python code here

#Fibonacci numbers module
def Fibonacci(n):
    result=[]
    a,b=0,1
    while a<n:
        result.append(a)
        a,b=b,a+b    
    return result
"$
    Return Py.RunCode("Fibonacci", Array(n), Code)
End Sub

I've reset the indentation and set it again.

teddybear

Well-Known Member
Licensed User
You should create a PyBridge with Python project instead of b4xpages

1739719504057.png
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Server is listening on port: 61177
Python executable not found!
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
Failed to start Python process.
org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "C:\_2_B4J~1\PYFIBO~1\B4J\Objects\Python\python310\python.exe" (in directory "."): CreateProcess error=2, The system cannot find the file specified)
Process completed. ExitCode: -559038737
b4xset._contains (java line: 61)
java.lang.NullPointerException: Cannot invoke "b4j.example.b4xorderedmap._containskey(Object)" because "this._map" is null
at b4j.example.b4xset._contains(b4xset.java:61)
at b4j.example.pybridge._registermember(pybridge.java:515)
at b4j.example.pybridge._runcode(pybridge.java:527)
at b4j.example.b4xmainpage._fibonacci(b4xmainpage.java:183)
at b4j.example.b4xmainpage$ResumableSub_Button1_Click.resume(b4xmainpage.java:143)
at b4j.example.b4xmainpage._button1_click(b4xmainpage.java:119)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.BA$1.run(BA.java:236)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:1589)
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
It should be configured path to python,or not?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Dim opt As PyOptions = Py.CreateOptions(File.Combine(File.DirApp, "C:\Python/python310/python.exe"))

This makes no sense

Try
Dim opt As PyOptions = Py.CreateOptions(File.Combine("", "C:\Python/python310/python.exe"))
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Dim opt As PyOptions = Py.CreateOptions(File.Combine(File.DirApp, "C:\Python/python310/python.exe"))

This makes no sense

Try
Dim opt As PyOptions = Py.CreateOptions(File.Combine("", "C:\Python/python310/python.exe"))
It still is error.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Server is listening on port: 61427
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
connected
starting PyBridge v0.50
watchdog set to 30 seconds
Connecting to port: 61427
Error occurred on line: 61 (B4XMainPage)
(b4xmainpage._fibonacci) - Python Error (TabError) - Method: module.exec: inconsistent use of tabs and spaces in indentation (<string>, line 7)
pywrapper.raiseError (java line: 607)
java.lang.RuntimeException: (b4xmainpage._fibonacci) - Python Error (TabError) - Method: module.exec: inconsistent use of tabs and spaces in indentation (<string>, line 7)
at b4j.example.pywrapper.raiseError(pywrapper.java:607)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4j.example.pywrapper._getvalue(pywrapper.java:239)
at b4j.example.b4xmainpage$ResumableSub_Button1_Click.resume(b4xmainpage.java:152)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:156)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4a.keywords.Common$3.run(Common.java:1172)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:1589)
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Create a PY project, just run it, it should work, and then modify it for William's example
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
(b4xmainpage._fibonacci) - Python Error (TabError) - Method: module.exec: inconsistent use of tabs and spaces in indentation (<string>, line 7)
That is because you cannot mix tabs and spaces in python source code. Use one or the other.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Use this code:
B4X:
Private Sub Fibonacci(n As Int) As PyWrapper
    Dim Code As String = $"
#python code here

#Fibonacci numbers module
def Fibonacci(n):
    result=[]
    a,b=0,1
    while a<n:
        result.append(a)
        a,b=b,a+b    
    return result
"$
    Return Py.RunCode("Fibonacci", Array(n), Code)
End Sub

I've reset the indentation and set it again.
 
Upvote 0
Solution

Theera

Expert
Licensed User
Longtime User
What happened about Python code?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
What exactly are you asking?

 
Upvote 0

Theera

Expert
Licensed User
Longtime User
What exactly are you asking?
After I remarked my Python code, and use your python code, It is Ok.
P.s. It is about configured path to Python directory, too.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
My problem is about your done" I've reset the indentation and set it again."
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The IDE will use tabs if you add a line of code. Python code expects either tabs or spaces but not both. You can ensure it only uses spaces by doing this sort of thing.
B4X:
    Dim Code As String = $"
#python code here

#Fibonacci numbers module
def Fibonacci(n):
    result=[]
    a,b=0,1
    while a<n:
        result.append(a)
        a,b=b,a+b    
    return result
"$.Replace(TAB,"    ") ' replace tabs with 4 spaces.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Ok, I know all of my problems
1. It is about configured path to Python directory
(My Python is C:\Python\python310\python.exe, but others may be different from mine)
B4X:
Dim opt As PyOptions = Py.CreateOptions(File.Combine("", "C:\Python\python310\python.exe"))
2. It is about the indentation each line which is different from other languages programming.
 
Upvote 0
Top