Difference between revisions of "Integrating Python with LianjaScript"

From Lianjapedia
Jump to: navigation, search
(Lianja Framework Classes)
(Understanding Custom Attributes and Delegates)
Line 60: Line 60:
 
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".
 
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".
  
You adjust the appearance and behavior of each UI Element in the Attributes Tab of the [[Category:App_Inspector_v5|App Inspector]].
+
You adjust the appearance and behavior of each UI Element in the Attributes Tab of the [[:Category:App_Inspector_v5|App Inspector]].
  
 
Delegates are scripting language independent event handlers for specific actions that may occur related to a UI Element.
 
Delegates are scripting language independent event handlers for specific actions that may occur related to a UI Element.

Revision as of 01:02, 16 April 2024

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".

You adjust the appearance and behavior of each UI Element in the Attributes Tab of the App Inspector.

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