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:

wonder

Expert
Licensed User
Longtime User
No issues here, I successfully imported the Regex module and displayed its contents.
Tested on two different devices (PC and Tablet).

Memu PC Emulator (x86):
upload_2017-10-31_21-2-59.png

NVIDIA Shield Tablet K1 (ARM):
upload_2017-10-31_21-6-8.png

Do you have the same issue with the Demo project?
 

Husam

Member
Licensed User
No issues here, I successfully imported the Regex module and displayed its contents.
Tested on two different devices (PC and Tablet).

now its work, i just changed the compilation mode from debug to release :D

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
[NinjaPython] ARM CPU detected
[NinjaPython] Python 2.7.9 was successfuly installed in: /data/user/0/b4a.example/files
[NinjaPython] Initialized
[NinjaPython] In [1]:
SayHello('world')
[NinjaPython] Out[1]:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'SayHello' is not defined
[NinjaPython] Terminated
** Activity (main) Resume **
 

wonder

Expert
Licensed User
Longtime User
Glad to hear that! :)

Regarding your Python code, note that you are calling a function that was not previously declared. You need to define SayHello().
B4X:
def SayHello(name):
    print "Hello, " + name + "!"

SayHello("world")
 

wonder

Expert
Licensed User
Longtime User
Here's an idea:
B4X:
'You can keep your functions on a separate file, without executable code
Dim myPythonFunctions = File.ReadString(File.DirAssets, "myFunctions.py") As String

'Start the interpreter
Py.Initialize

'Load your functions into memory
Py.Exec(myPythonFunctions)
...
...
'Call a specific function whenever you want
Py.Exec("SayHello('World')")
Py.Exec("SayHello('Moon')")
Py.Exec("SayHello('Mars')")

'Finalize destroys the interpreter object, meaning
'your functions are no longer in memory
Py.Finalize
 

Q Candiani

Member
Licensed User
Longtime User
Great work! I interest to use de psycopg2 library to connect to a postgresql database from my app. Is there any way to use it? I think it would be very powerful tool. Thank you very much
 

wonder

Expert
Licensed User
Longtime User
Hi!! I'm back from the dead! :D

Is this library only for B4A, or will it work in B4J?

Nope, it's B4A only. On the PC you could just use regular Python, right?
 

Cableguy

Expert
Licensed User
Longtime User
Hi!! I'm back from the dead! :D

You never die while someone still remembers you!


Nope, it's B4A only. On the PC you could just use regular Python, right?

Just out of curiosity, how could python be integrated in a b4j project?
 

DonManfred

Expert
Licensed User
Longtime User
Just out of curiosity, how could python be integrated in a b4j project?
B4X:
Sub StartTest
    Dim shl As Shell
    shl.Initialize("shl","python",Array("/.../.../test.py"))
    shl.RunWithOutputEvents(-1)
End Sub

Sub shl_StdOut (Buffer() As Byte, Length As Int)
    Dim Output As String = BytesToString(Buffer,0,Length,"UTF-8")
    Log(Output)
End Sub

Sub shl_StdErr (Buffer() As Byte, Length As Int)
    Dim Output As String = BytesToString(Buffer,0,Length,"UTF-8")
    Log(Output)
End Sub
 

wonder

Expert
Licensed User
Longtime User
Hey Ilan! I'm doing alright, thanks!! How about you?
Well, I barely have time to dedicate myself to B4X nowadays, due to my current schedule.

I'm still working on some of my stuff, here and there. :)
 

ilan

Expert
Licensed User
Longtime User
I'm still working on some of my stuff, here and there.

Please dont abandon your 2d knight game. You must finish it. It is a master piece.

How about you

I am fine thanx. I also have very little time for coding. I switch job 1 month ago and i have lot of work in my new job so less time for my b4x projects but right now i am working on a b4a app that will make me rich. :)

(If i would get 1$ for everytime i thought this will be the one i really would be already rich) :p
 

sorex

Expert
Licensed User
Longtime User
are you still into CNC? or did you enter the world of IT as profession?
 

wonder

Expert
Licensed User
Longtime User
Please dont abandon your 2d knight game. You must finish it. It is a master piece.
I won't! :) I've been learning GLSL and experimenting with shaders!

i am working on a b4a app that will make me rich
Ahahah!!! I really hope that day will come!!! ;)


On a side note, this thread is about NinjaPython, so better if we can get back on topic before Erel gets mad. :p
 
Top