Other [PyBridge] B4J v10.2 BETA is available for download

Erel

B4X founder
Staff member
Licensed User
Longtime User
This update includes a new library named PyBridge, that allows using Python libraries from inside the B4J app. Python is leading the way in fields such as AI, LLM, image recognition and many others. Using PyBridge is not always trivial and it won't be the right tool for every task, however there are many things that weren't possible before and are now possible. And that is a good thing.

1739718245749.png


Other improvements:
  • Many internal libraries were updated since last release.
  • New internal keywords: Initialized and NotInitialized:
    B4X:
    If Map1 <> Null And Map1.IsInitialized Then ... 'boring
    If Initialized(Map1) Then ... 'less boring
  • #CustomBuildAction arguments are expanded with environment variables and other variables (%PROJECT%, %B4X%, %JAVABIN%, %PROJECT_NAME% and %ADDITIONAL%): https://www.b4x.com/android/forum/threads/b4x-comment-links.119897/#content
  • LogColors withs in b4xlibs
  • Fix for logs encoding issue in Java 19+. Note that the following attribute should be added for the same fix with standalone packages (if the terminal output is important):
    B4X:
    #PackagerProperty: VMArgs = -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8
  • B4XCollections: new helper methods: EmptyList, EmptyMap, MergeMaps, MergeLists, CreateList, CopyOnWriteMap and CopyOnWriteList.
  • Panel.LoadLayout no longer deletes the previous tag.
Download link: https://www.b4x.com/b4j/files/beta.exe

PyBridge is a big thing. I will write tutorials that explain how it works and how Python libraries can be wrapped to be accessed in a simple manner.
To run a simple program:

1739957471817.png



Please start a new thread for questions or issues. Use [pybridge] in the thread title if related.
 

Attachments

  • 1739719504057.png
    1739719504057.png
    15.4 KB · Views: 807
Last edited:

LucaMs

Expert
Licensed User
Longtime User
I don't know if I should report a bug here or in a new thread. Maybe better here.

I opened the latest project (v. 10) I'm working on with this new version.
I made some changes: I added a field to a custom type and set this field in 4 or 5 lines of the project. The IDE reports errors that are not reported by version 10, since there are no errors, and in fact clicking on them does not move the cursor in the editor on the error line.

1739824000175.png



That ['as' expected "] is suspicious. The new custom type field is correctly declared, it has its own type (Int).

When opening the project in B4J V.10, those errors are not reported.

I try to perform the same steps in a test project... Done: no problems. Perhaps the steps taken were not in the same order.


I summarize the current situation.

The project is generated (updated) with version 10.1 Beta:

1739825478179.png


If I open it with B4J V. 10.1 Beta, the IDE highlights those errors but clicking on them does not "take you" to the wrong lines, the click has no effect.

If I open it with B4J V. 10, no errors and the project works "perfectly" (without crashes).
 
Last edited:
Upvote 0

Theera

Expert
Licensed User
Longtime User
Hello William,
Do you mind to post the completely examples for non-expert who need study them and isn't good at English?
After running my sourcecode it has showing mainform,although I 've deleted it,and I can't remark the part of code in appstart()
 

Attachments

  • PyPlotGraph.zip
    4.3 KB · Views: 35
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I opened the latest project (v. 10) I'm working on with this new version.
I made some changes: I added a field to a custom type and set this field in 4 or 5 lines of the project. The IDE reports errors that are not reported by version 10, since there are no errors, and in fact clicking on them does not move the cursor in the editor on the error line.
There are two new keywords: Initialized and NotInitialized. If you have a variable with the same name then you will need to rename it.
If this isn't the case then please send me the project to erel@basic4ppc.com
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
There are two new keywords: Initialized and NotInitialized. If you have a variable with the same name then you will need to rename it.
If this isn't the case then please send me the project to erel@basic4ppc.com
I removed almost everything from the project.
The error remains (and when launching the project, the error reported, different, is even stranger).

P.S.
Only now I thought to deselect some libraries. I removed only the DragAndDrop2 library, which is the only B4XLib, and the errors disappeared.
I think it is important to remind you that this library has always been present in the project and the errors appeared only when I added a field to an already existing custom type.

P.P.S.
I removed the b4xlib DragAndDrop2 and added its classes. One of them, TransferMode, has a variable called Initialized and that is obviously the "error".
 

Attachments

  • ToErel.zip
    3.4 KB · Views: 15
Last edited:
Upvote 0

walterf25

Expert
Licensed User
Longtime User
This update includes a new library named PyBridge, that allows using Python libraries from inside the B4J app. Python is leading the way in fields such as AI, LLM, image recognition and many others. Using PyBridge is not always trivial and it won't be the right tool for every task, however there are many things that weren't possible before and are now possible. And that is a good thing.

View attachment 161769

Other improvements:
  • Many internal libraries were updated since last release.
  • New internal keywords: Initialized and NotInitialized:
    B4X:
    If Map1 <> Null And Map1.IsInitialized Then ... 'boring
    If Initialized(Map1) Then ... 'less boring
  • #CustomBuildAction arguments are expanded with environment variables and other variables (%PROJECT%, %B4X%, %JAVABIN%, %PROJECT_NAME% and %ADDITIONAL%): https://www.b4x.com/android/forum/threads/b4x-comment-links.119897/#content
  • LogColors withs in b4xlibs
  • Fix for logs encoding issue in Java 19+. Note that the following attribute should be added for the same fix with standalone packages (if the terminal output is important):
    B4X:
    #PackagerProperty: VMArgs = -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8
  • B4XCollections: new helper methods: EmptyList, EmptyMap, MergeMaps, MergeLists, CreateList, CopyOnWriteMap and CopyOnWriteList.
  • Panel.LoadLayout no longer deletes the previous tag.
Download link: https://www.b4x.com/b4j/files/beta.exe

PyBridge is a big thing. I will write tutorials that explain how it works and how Python libraries can be wrapped to be accessed in a simple manner.
To run a simple program:

View attachment 161770

This will create a new project with a small Python runtime.

Please start a new thread for questions or issues. Use [pybridge] in the thread title if related.
Wow this can be a game changer, I use python at work and interface with B4J, but using python directly from within B4J, awesome. I hope this projects matures enough to do more complex things.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4J v10.2 BETA #2 is available for download.
Download link: https://www.b4x.com/b4j/files/beta.exe

The main change in this update is related to the way the Python runtime is located.
The new template is named PyBridge and it no longer includes a Python runtime. The Python package is installed under Program Files\Anywhere Software\B4J\Libraries\Python

There is now a local Python and global Python.
The path to the global Python can be configured with Tools - Configure Paths. If not configured then the internal Python package is considered to be the global Python.

At runtime - PyBridge looks for the local Python - the path passed to CreateOptions:
B4X:
Dim opt As PyOptions = Py.CreateOptions("Python/python/python.exe")
If it doesn't exist then the global Python will be used.

There is a comment link that will create a local Python by copying the internal one to the project folder:
B4X:
'Top of B4XMainPage.
'Create a local Python runtime:   ide://run?File=%WINDIR%\System32\Robocopy.exe&args=%B4X%\libraries\Python&args=Python&args=/E

My recommendation:

1. Start a new PyBridge project.
2. Create a local Python runtime and copy the Python folder (Objects\Python) to a different folder.
3. Configure this Python to be the global Python.
Use the global Python will all projects.
When you are ready to distribute a project then switch to a local Python with the relevant dependencies.

Note that the internal Python is stored in a read-only folder. If you try to install libraries they will be installed in C:\Users\<user>\AppData\Roaming\Python. This is a global folder for Python libraries and it can later be difficult to understand where the library is loaded from. Therefore, it is recommended to make a copy of the Python package and use that copy as the global Python.

1739967548883.png
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4J v10.2 BETA #2 is available for download.
Download link: https://www.b4x.com/b4j/files/beta.exe

The main change in this update is related to the way the Python runtime is located.
The new template is named PyBridge and it no longer includes a Python runtime. The Python package is installed under Program Files\Anywhere Software\B4J\Libraries\Python

There is now a local Python and global Python.
The path to the global Python can be configured with Tools - Configure Paths. If not configured then the internal Python package is considered to be the global Python.

At runtime - PyBridge looks for the local Python - the path passed to CreateOptions:
B4X:
Dim opt As PyOptions = Py.CreateOptions("Python/python/python.exe")
If it doesn't exist then the global Python will be used.

There is a comment link that will create a local Python by copying the internal one to the project folder:
B4X:
'Top of B4XMainPage.
'Create a local Python runtime:   ide://run?File=%WINDIR%\System32\Robocopy.exe&args=%B4X%\libraries\Python&args=Python&args=/E

My recommendation:

1. Start a new PyBridge project.
2. Create a local Python runtime and copy the Python folder (Objects\Python) to a different folder.
3. Configure this Python to be the global Python.
Use the global Python will all projects.
When you are ready to distribute a project then switch to a local Python with the relevant dependencies.

Note that the internal Python is stored in a read-only folder. If you try to install libraries they will be installed in C:\Users\<user>\AppData\Roaming\Python. This is a global folder for Python libraries and it can later be difficult to understand where the library is loaded from. Therefore, it is recommended to make a copy of the Python package and use that copy as the global Python.

View attachment 161897
When I installed the Win7 version, thanks to your suggestion, I gave permission to set the user PATH.
I tried and it works like this too:
B4X:
Dim opt As PyOptions = Py.CreateOptions("python")

P.S. This one:
1739968590153.png
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Great, Thanks Erel PYTHONPATH works as promised.

A couple of initial observations:
  • Start up time is improved by not copying Python on project creation - Better user experience
  • It would be useful to log whether global or Local Python is being used on startup. Just thinking about coming back to a project after a while. Found it.
  • It would be useful to allow an empty path in Py.CreateOptions("") to switch to the global python, it would make it immediately obvious. Although it does switch just by entering "Global".
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
When I installed the Win7 version, thanks to your suggestion, I gave permission to set the user PATH.
I tried and it works like this too:
It will work with any string (except of an empty string that currently has special meaning). It doesn't search the PATH. If the passed path isn't found then it will use the global Python.

It is recommended to keep the local path in case you later decide to switch to a local Python.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It will work with any string (except of an empty string that currently has special meaning). It doesn't search the PATH. If the passed path isn't found then it will use the global Python.
You are referring to this second Beta version, I am referring to the first; with the first, having that variable set in the system, the name of the sw is sufficient, even without the exe extension.

Obviously with the subsequent versions (this one included) I will follow your advice; mine was just a test, a curiosity: will that variable be used? Yes.
 
Upvote 0

AlfaizDev

Well-Known Member
Licensed User
I don't know why I don't understand the library, is it because I don't know Python, but I'm waiting for an explanation.
And what's so special about b4j?
 
Upvote 0

AlfaizDev

Well-Known Member
Licensed User
PyBridge is a big thing. I will write tutorials that explain how it works and how Python libraries can be wrapped to be accessed in a simple manner.
To run a simple program:
👍
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
When loading the attached program I get these messages in the Logs:

1740051880912.png

I do not see any line in my code where the error is.

Therefore it seems that the error is in a library.
List of the libraries:

1740052052888.png


The program works perfectly with B4J version 10.00
 

Attachments

  • b4xlib2XML.zip
    4.8 KB · Views: 26
Upvote 0

teddybear

Well-Known Member
Licensed User
When loading the attached program I get these messages in the Logs:

View attachment 161934
I do not see any line in my code where the error is.

Therefore it seems that the error is in a library.
List of the libraries:

View attachment 161935

The program works perfectly with B4J version 10.00
The solution is at post#24 and post#27
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Beta #3 is available for download: https://www.b4x.com/b4j/files/beta.exe

1. Adds a few missing methods to PyBridge.
2. B4JPackager improvements:
Add details to the generated executable:
B4X:
#PackagerProperty: AssemblyTitle = This is the title
#PackagerProperty: AssemblyDescription = Description
#PackagerProperty: AssemblyCompany = Cool company
#PackagerProperty: AssemblyProduct = Cool product
#PackagerProperty: AssemblyVersion = 1.2.3.0

Option to make the app request admin privileges on launch:
B4X:
#PackagerProperty: RequireAdministrator = True
Note that the useful run_debug.bat utility will not request these privileges.

3. B4J is signed with a new code signing key. You might see some warnings as this is a new key.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
B4X:
#PackagerProperty: AssemblyTitle = This is the title
It will be related to the title name of the app, or not?
B4X:
B4XPages.SetTitle(Me, "Title name")
 
Upvote 0
Top