B4A Library NinjaPython - Use Python Scripts in B4A

Work in progress - Work in progress - Work in progress - Work in progress - Work in progress
np.jpg

Work in progress - Work in progress - Work in progress - Work in progress - Work in progress

The NinjaPython B4A library brings you the ability of utilizing the Python language inside your B4A games and apps.
This can range from a few lines of code to entire scripts or object oriented modules, the choice is yours.

upload_2017-10-29_22-7-17.png

Quickstart:
- Switch 'Compilation Mode' to Release
- Download the library files and place them in your B4A external libraries folder.
- Option A: Download the resource files and place them in your project's Files folder.
- Option B: Download the test project below.
- See the 'usage' example below.​

Practice:

Features:
- CPython Interpreter 2.7.9
- Python Standard Library
- x86 and ARM compatible
- Ongoing development
Wishlist:
- Dedicated function calls
- Passing and returning B4A objects such as lists and maps
- Support for 3rd party libraries
Price (pay what you want):
Test project:

KanBan board:

Library files (1.03):

Resource files:

Usage:
B4X:
Dim Py As NinjaPython
Py.Initialize
Py.Exec($"
def SayHello(subject):
    print 'Hello, ' + subject + '!'
"$)

...
...
...
...

Py.Exec("SayHello('world')") 'Outputs and returns 'Hello, world!'
Py.Finalize

Learn:
/SPOILER]
Enjoy! :)
 
Last edited:

Michael Mejias

Member
Licensed User
Longtime User
Hello

Logger connected to: 192.168.209.101:5555
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
main_globals (java line: 417)
java.lang.ExceptionInInitializerError
at com.ninjadynamics.python.main._globals(main.java:417)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at com.ninjadynamics.python.main.initializeGlobals(main.java:317)
at com.ninjadynamics.python.main.afterFirstLayout(main.java:101)
at com.ninjadynamics.python.main.access$000(main.java:17)
at com.ninjadynamics.python.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsatisfiedLinkError: Cannot load library: load_library[1098]: Library '/system/lib/libhoudini.so' not found
at java.lang.Runtime.loadLibrary(Runtime.java:370)
at java.lang.System.loadLibrary(System.java:535)
at com.ninjadynamics.ninjapython.NinjaPython.<clinit>(NinjaPython.java:183)
... 17 more
 

walterf25

Expert
Licensed User
Longtime User
Awesome project, however i'm playing around with it and sending the following lines of code and i keep getting an error.

Any ideas why that is and how to get this to work, the reason i'm testing it with the argparse modile is because the scripts i have depend on the argparse module and was hoping i could port this project over to android.

the lines of code i'am sending are the following.

B4X:
Sub argpasrser As String
    Dim str As StringBuilder
    str.Initialize
    str.Append("import sys").Append(CRLF).Append("import argparse").Append(CRLF) _
    .Append("parser = argparse.ArgumentParser()").Append(CRLF) _
    .Append("parser.add_argument(" & """echo""" & ", help="&"""echo the string you use here"""&")").Append(CRLF) _
    .Append("args = parser.parse_args()").Append(CRLF)  _
    .Append("print args.echo")
    Log("command: " & str.ToString)
    Return str.ToString
End Sub

I'am calling this function which returns the formatted string to the
B4X:
Py.Execute(argpasrser)

but i keep getting the following error:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
[NinjaPython] ARM CPU detected
[NinjaPython] Python 2.7.9 installation found in: /data/user/0/com.ninjadynamics.python/files
[NinjaPython] Initialized
[NinjaPython] In [1]:
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo", help="echo the string you use here")
args = parser.parse_args()
print args.echo
[NinjaPython] Out[1]:
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/data/user/0/com.ninjadynamics.python/files/Lib/argparse.py", line 1586, in __init__
prog = _os.path.basename(_sys.argv[0])
AttributeError: 'module' object has no attribute 'argv'

Thanks, i'm hoping i can find the solution to this so i can proceed and hopefully port this project and load it onto a raspberry pi running Android Things.

Regards,
Walter
 

AmirMK82

Member
Licensed User
hi,
thanks for this lib
ttps://www.b4x.com/android/forum/threads/ninjapython-use-python-scripts-in-b4a.85220/post-574574
how can I imoprt library with zip format?
 

Anurag Jain

Member
Licensed User
Work in progress - Work in progress - Work in progress - Work in progress - Work in progress
View attachment 61077
Work in progress - Work in progress - Work in progress - Work in progress - Work in progress

The NinjaPython B4A library brings you the ability of utilizing the Python language inside your B4A games and apps.
This can range from a few lines of code to entire scripts or object oriented modules, the choice is yours.


Quickstart:
- Switch 'Compilation Mode' to Release
- Download the library files and place them in your B4A external libraries folder.​
- Option A: Download the resource files and place them in your project's Files folder.​
- Option B: Download the test project below.​
- See the 'usage' example below.​

Practice:

Features:
- x86 and ARM compatible​
- Ongoing development​
Wishlist:
- Dedicated function calls​
- Passing and returning B4A objects such as lists and maps​
- Support for 3rd party libraries​
Price (pay what you want):
- Free
- 2 beers
- 1 pizza
Test project:
-

KanBan board:

Library files (1.03):
-

Resource files:
-

Usage:
B4X:
Dim Py As NinjaPython[/COLOR][/INDENT]
[COLOR=#000000][INDENT]Py.Initialize[/INDENT]
[INDENT]Py.Exec($"[/INDENT]
[INDENT]def SayHello(subject):[/INDENT]
[INDENT]    print 'Hello, ' + subject + '!'[/INDENT]
[INDENT]"$)[/INDENT]
[INDENT][/INDENT]
[INDENT]...[/INDENT]
[INDENT]...[/INDENT]
[INDENT]...[/INDENT]
[INDENT]...[/INDENT]
[INDENT][/INDENT]
[INDENT]Py.Exec("SayHello('world')") 'Outputs and returns 'Hello, world!'[/INDENT]
[INDENT]Py.Finalize[/INDENT]
[INDENT]


Learn:
/SPOILER]
Enjoy! :)
Hi, I have been trying this, but it appears that after execution, its not able to find python library, which it is trying to look in some folder. Do we need to install python in PC to run the code, ............
 
Hello, the idea from this project is really amazing !!!

I would like do some tests, please could you approve my request to download the lib.

tks a lot !!!
 
Top