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: 45
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
 
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
 
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