HOWTO Integrate Python and LianjaScript
by
, 2023-09-26 at 08:52 (24183 Views)
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.
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:
Alternatively you can execute it from the operating system command line.Code:c:\lianja\cloudserver\scriptinglanguages\python3\python.exe -m pip install --upgrade pip
Windows
cd c:\lianja\bin\
.\pip list
.\pip install packagename
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.
Now from LianjaScript (added in Lianja 9.3.2) you can dynamically load this library (once) and call it in one statement.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] )
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.
Marshaling arrays and lists between LianjaScript and PythonCode:Lianja.evaluate(“yourLianjaFunction(‘hello’, ‘world’)”)
You can pass arrays and lists back and forward between LianjaScript and Python like this:
In LianjaScript:
In Python the function that is being called creates a Python list and returns it as a comma separated list of items.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, “,”)
Marshaling objects between LianjaScript and PythonCode:# file: mypythonlib.py def myfunc(cList): mylist = list(cList.split(",")) return “,”.join(mylist)
You can pass objects back and forward between LianjaScript and Python like this:
In LianjaScript:
In Python the function that is being called creates a Python dictionary and returns it as a JSON encoded string.Code:// object myobj = object() myobj.name = "Barry" result = execPython(format("mypythonlib::myFunc('{0}')", base64_encode(json_encode(myobj)))) myobj = json_decode(result)
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)