View RSS Feed

LianjaDev

HOWTO Integrate Python and LianjaScript

Rate this Entry
LianjaScript and Python integration

Python is now the most popular programming language in the world.

LianjaScript is a data-centric scripting language specifically used for developing database applications. It is a modern day superset of Microsoft Visual FoxPRO which was widely praised for its speed and flexibility.

Lianja has complete two-way integration with Python. You can execute Python code directly from LianjaScript and execute LianjaScript code directly from Python.

If you have Python expertise and that’s your language of Choice then you can build apps in Lianja in Python!

If you are an ISV and you have limited VFP expertise in-house you can assign Lianja development tasks to Python developers.

Both LianjaScript and Python are cross platform and can be used on Windows, Linux and MacOS.ere are still many legacy VFP applications out there and Lianja provides a fully supported path to modernize those applications and make them available on the Web and Mobile devices.

Why choose Lianja for Python business applications development?

The Lianja platform includes the Lianja App Builder which is a modern visual development IDE for building Desktop, Web and Mobile apps.

Python has an extensive set of open source packages that can be used in your Lianja business apps.

https://pypi.org/search/?c=Programmi...ython+%3A%3A+3

Installing Python packages in Lianja

Python is embedded inside Lianja. You do not need to install it separately. If you have an existing Python installation the embedded Python inside Lianja will have no effect on it.

PIP is the package manager for Python packages.

To install Python packages that can be used with Python in Lianja you can use pip in the Lianja Python console.

Click image for larger version. 

Name:	Screen Shot 2023-09-25 at 10.26.45 AM.jpg 
Views:	1575 
Size:	55.0 KB 
ID:	3065

If you are notified that pip is out of date your can update it from a console command window which on my windows development system is:

Code:
c:\lianja\cloudserver\scriptinglanguages\python3\python.exe -m pip install --upgrade pip
Alternatively you can execute it from the operating system command line.

Windows

cd c:\lianja\bin\
.\pip list
.\pip install packagename

Click image for larger version. 

Name:	Screen Shot 2023-09-25 at 10.24.17 AM.jpg 
Views:	1494 
Size:	25.3 KB 
ID:	3066

LianjaScript Essentials

https://www.lianja.com/doc/index.php...ing_Essentials

Python Tutorial

https://www.w3schools.com/python/default.asp

Lianja includes an extensive array of built-in classes that are both cross platform and cross device. These classes can be used with both LianjaScript and Python.

https://www.lianja.com/doc/index.php...mework_Classes

Integrating Python with LianjaScript

In LianjaScript you can execute any Python code using the built-in execPython() function.

Code:
File: barrytest.py
import Lianja
import sys

def myfunc(theArg):
    Lianja.writeOutput("myfunc was called with " + theArg)
    return Lianja.evaluate("now()")

if len(sys.argv) > 0:
    global returnvalue
    returnvalue = myfunc( sys.argv[1] )
Now from LianjaScript (added in Lianja 9.3.2) you can dynamically load this library (once) and call it in one statement.

Code:
result = execPython("barrytest::myfunc('hello world')")

Inside your Python code you can execute any LianjaScript code using Lianja.evaluate() and Lianja.execute()

Code:
now = Lianja.evaluate("now()")

The argument to Lianja.evaluate() can contain user defined functions and procedure calls. This provides full two way integration between LianjaScript and Python.

Code:
Lianja.evaluate(“yourLianjaFunction(‘hello’, ‘world’)”)
Marshaling arrays and lists between LianjaScript and Python

You can pass arrays and lists back and forward between LianjaScript and Python like this:

In LianjaScript:

Code:
// dynamic arrays
myarray = array(1,2,3,4,5)
result = execPython(format("mypythonlib::myFunc('{0}')", implode(",", myarray))
myarray = explode(“,”, result)

// static arrays
declare arr[2]
arr[1] = "hello"
arr[2] = "world"
result = execPython(format("mypythonlib::myFunc('{0}')", implode(",", arr))
astore(arr, result, “,”)
In Python the function that is being called creates a Python list and returns it as a comma separated list of items.

Code:
# file: mypythonlib.py
def myfunc(cList):
    mylist = list(cList.split(","))
    return “,”.join(mylist)
Marshaling objects between LianjaScript and Python

You can pass objects back and forward between LianjaScript and Python like this:

In LianjaScript:

Code:
// object
myobj = object()
myobj.name = "Barry"

result = execPython(format("mypythonlib::myFunc('{0}')", base64_encode(json_encode(myobj))))
myobj = json_decode(result)
In Python the function that is being called creates a Python dictionary and returns it as a JSON encoded string.

Code:
# file: mypythonlib.py
import json
import base64

def myfunc(cJson):
   cJson = base64.b64decode(cJson)
   myobj =  json.loads(cJson)
   myobj["name"]  = "Barry Mavin"
   return json.dumps(myobj)

Submit "HOWTO Integrate Python and LianjaScript" to Google Submit "HOWTO Integrate Python and LianjaScript" to Facebook Submit "HOWTO Integrate Python and LianjaScript" to Twitter

Updated 2024-03-04 at 01:50 by barrymavin

Tags: None Add / Edit Tags
Categories
LianjaDev , LowCode , ProCode , HOWTO

Comments

Journey into the Cloud
Join us