Integrating Python with LianjaScript
Contents
- 1 LianjaScript and Python integration
- 2 Why choose Lianja for Python business applications development?
- 3 Installing Python packages in Lianja
- 4 LianjaScript Essentials
- 5 Python Tutorial
- 6 Lianja Framework Classes
- 7 Python built-in LianjaScript functions
- 8 Working with data in Python
- 9 Using Lianja Key-Value Store in Python
- 10 Building Standalone Executables in Python
- 11 Integrating Python with LianjaScript
- 12 Marshaling arrays and lists between LianjaScript and Python
- 13 Marshaling objects between LianjaScript and Python
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. There 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.
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:
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
LianjaScript Essentials
Python Tutorial
Lianja Framework Classes
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.
Python built-in LianjaScript functions
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.
Many of the LianjaScript functions are built into Lianja Python.
Python built-in LianjaScript functions
Working with data in Python
Accessing data in Lianja databases from Python is simple, but nevertheless powerful, exposing much of the power of the Lianja database to Python developers.
See Working with data in Python for details.
Using Lianja Key-Value Store in Python
Lianja/KVS (Key-Value store) is fully integrated in with Python.
See Lianja/KVS for details.
Building Standalone Executables in Python
See Building Standalone Executables for details.
Integrating Python with LianjaScript
In LianjaScript you can execute any Python code using the built-in execPython() function.
# 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 (from v9.3.2) you can dynamically load this library (once) and call it in one statement.
result = execPython("barrytest::myfunc('hello world')")
Inside your Python code you can execute any LianjaScript code using Lianja.evaluate() and Lianja.execute().
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.
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:
// 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.
# 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:
// 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.
# 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)