PythonCalling Python From Xbasic


Learn how to utilize Python in your sever-side scripts and events.


All server-side code in an UX and Grid is typically handled using Xbasic code. However, you are not restricted to only Xbasic to build server-side processes. Python scripts can be called everywhere Xbasic is used, including component server-side events, Ajax Callbacks, Xbasic modules, and more. In addition to being able to execute Python, your Python scripts have access to the same variables available to Xbasic scripts.

Python offers several advantages to new and existing developers when developing server-side scripts. Python integration with Alpha Anywhere offers developers the option to create server-side scripts using a language they already know. Python also has a vast library of modules you could leverage in your applications.

With every advantage, however, comes some drawbacks. At this time, there is no integrated debugger for Python in Alpha Anywhere. You will need to use an external tool to debug your Python scripts, or log variables and status information to a file from Python that you can later inspect.

Executing Python from Xbasic uses the following pattern:

  • Create an Xbasic code stub or function.
  • Call a Python function from Xbasic using helper::Python::Call().
  • Capture the return value from the Python function in an Xbasic variable.
  • Return the return value from the Python function (if desired).

The Python function called from Xbasic must be defined in a Python module. A Python module is a text file with a .py extension containing one or more Python function definitions. All Python modules in your project must be stored in a folder called Python in the Web Project.

To add a Python folder to your Web Project you can click the More... button on the Web Project Control Panel and then select the Python, Create a 'Python' folder in the Web Project command.

Calling Python in an Ajax Callback

Assume you have a button in your component that makes an Ajax Callback to an Xbasic function called myXbasicFunction. In this callback function, you would like to call myfunc, a Python function. myfunc, shown below, returns JavaScript that displays a popup with the message "Hello from Python".

Python function that returns JavaScript to display a popup message.
def myfunc(e,session,request):

    #Your code can read all of the properties in the e object
    #Your code can read and set session variables
    #Your code can read the request object

    #Create a variable with the value you want to return to the Xbasic stub code
    fn = "alert('Hello from Python');"
    #return the value
    return fn

This Python function is saved in a file called "" in the Python folder in the Web Project directory.

Here is how you would implement the myXbasicFunction function to call myfunc:

function myXbasicFunction as c (e as p)
    'Force a reload of the Python modules so that any changes you have
    'made to the Python code during development are picked up
    'Remove this line when you're ready to publish your app to production:

    dim returnValue as c

    'Call the myfunc function in the mypython module.
    'When you call helper::Python::Call(), you must pass in e, session, and request
    'This gives your Python function access to read and set session variables. 
    'All data in the e and request objects is also accessible in your Python code
    returnValue = helper::Python::Call("mypython","myfunc",e,session,request)

    'Return the value the Python function returned from the Xbasic function
    return returnValue

end function

Executing SQL Queries in Python

One of the significant benefits of writing your server-side code in Xbasic is Alpha Anywhere Data Access Objects (AlphaDAO), which makes it extremely easy to connect to a multitude of SQL databases and perform CRUD operations with Portable SQL. When you call Python code from within Alpha Anywhere, you have access to the AlphaDAO classes. This makes working with SQL databases in Python just as easy as it is in Xbasic.

You can certainly import specialized libraries into your Python functions to execute SQL commands, but you may find it easier to use the AlphaDAO objects that are built into Alpha Anywhere.

Below is some sample Python code that executes a SQL Select statement on the sample Northwind database.

#import the AApySQL library so that your Python code can access AlphaDAO object
import AApySQL

#create a connection object
cn = AApySQL.Connection()

#your Python code has access to your database connection strings
connectionString = "::Name::AADemo-Northwind"

#open a connection to the Database. The method name (.Open() is case-sensitive!)
flag = cn.Open(connectionString)

sql = "Select * from Customers"

#execute a SQL command
flag = cn.Execute(sql)
if flag:
    #get the data in JSON format
    json = cn.ResultSet.ToJSON()

Accessing Xbasic Functions in Python

Your Python code can execute any of the many functions available in Xbasic. In the example below the Python function executes the Xbasic api_uuidcreate() function to generate a GUID.

#import AAPy to that your Python code can reference Xbasic functions
import AAPy

guid = AApy.xbasic("api_uuidcreate()")

Accessing Xbasic Classes in Python

Alpha Anywhere exposes a large number of useful classes to your Xbasic code. These classes can all be used in your Python code.

In the example below the Python function uses the css::styledef class to generate a CSS style.

Notice that the import AAPY statement is needed to import the Alpha Anywhere library for calling Xbasic from Python. The AApy.xbasic() method is called to create a Python object from the Xbasic css::styledef class.

#import AAPy to that your Python code can reference Xbasic classes
import AAPy

ss = AApy.xbasic("new css::styledef()")
ss.background_color = "green"
string = ss.toString()

Debugging Python in an Alpha Anywhere App

There is no integrated debugger available to debug into your Python code (unlike Xbasic which has a very easy to use debugger). Currently, the only way to debug your Python code is by writing variable values to a file. Here is sample Python code that you can use to write to a file:

f = open("c:/debug/pythondebug.txt", "w")
f.write("write some text")


Calling Python Functions in Server-side Code

Typically, all server-side code in a UX or Grid is written using Xbasic. But if you are already familiar with Python, you might prefer to write server-side code using Python.

In this video, we show how you can write server-side code using Python.