Integrating Python with LianjaScript

From Lianjapedia
Jump to: navigation, search

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.

Python Package Index (PYPI)

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.

pip list


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

Commannd Window: pip list

LianjaScript Essentials

Lianja Scripting Essentials provides a primer for developers who are new to the LianjaScript scripting language.

Python Tutorial

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.

See Lianja Framework Classes for details.

Understanding Custom Attributes and Delegates

An App consists of pages. Pages are made up of Sections. Form sections are made up of FormItems. We call these collectively "UI Elements" or "Visual Elements".

The Page Builder Assistant can assist you in designing the UI and coding delegates in Pages. It provides speedy access to key Page, Section, Formitem and GridColumn attributes and the ability to navigate quickly between the UI elements. Delegates can be edited in a floating editor directly in the Pages workspace.

See the Page Builder Assistant for details.

Delegates are scripting language independent event handlers for specific actions that may occur related to a UI Element.

see Understanding Custom Delegates for details.

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.

See Python built-in LianjaScript functions for details.

Working with data in Python

Accessing data in Lianja and other third party 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.

Working with Python Server Pages

Python Server Pages provide the ability to write both desktop and server-side code in Python. If you know Python and HTML these are for you.

See Python Server Pages for details.

Developing Custom Sections in Python

See Developing Custom Sections in Python for details.

Developing Custom Gadgets in Python

See Developing Custom Gadgets in Python for details.

Developing Custom Webviews in Python

See Developing Custom WebViews in Python for details.

Using the 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)

Embedding .NET into Python

You can embed .NET into Python using the Python.NET package.

Python.NET is a package that gives Python programmers nearly seamless integration with the .NET Common Language Runtime (CLR) and provides a powerful application scripting tool for .NET developers. It allows Python code to interact with the CLR, and may also be used to embed Python into a .NET application.

Installing from PyPI can be done using:

pip install pythonnet

You can then integrate LianjaScript and .NET by creating wrapper functions between LianjaScript and Python as described above.

See Embedding .NET into Python for details.

If .NET Core is installed in a default location or the dotnet CLI tool is on the PATH, loading it instead of the default (Mono/.NET Framework) runtime just requires setting either the environment variable PYTHONNET_RUNTIME=coreclr or calling pythonnet.load explicitly:

from pythonnet import load
load("coreclr")
 
import clr

Make sure that the directory containing the .net DLL is in your Python path. The easiest way to do this is by modifying sys.path.

assembly_path = r"C:\Users\Administrator\Desktop\test\CalcTest\CalcTest\bin\Debug"
 
import sys
sys.path.append(assembly_path)

Import the assembly. Note that you don't have to append .dll to the end of the assembly name, Python for .Net does that for you automatically.

import clr
clr.AddReference("CalcTest")

Use the namespace as a module to import classes and other DLL resources.

from CalcTestNS import calculate

Use the imported class. .

ct = calculate()
print(ct.Add(1,1))
 
params = [1,2]
print(ct.Sub(*params))

Here is the complete code.

import clr
import sys
 
assembly_path = r"C:\Users\Administrator\Desktop\test\CalcTest\CalcTest\bin\Debug"
sys.path.append(assembly_path)
 
clr.AddReference("CalcTest")
from CalcTestNS import calculate
 
ct = calculate()
print(ct.Add(1,1))
 
params = [1,2]
print(ct.Sub(*params))