How to use a python script in TideSDK: with an example
Today, we are going to see an example for how to use python as back-end in TideSDK. As we have already learnt to create a basic application in TideSDK in a previous post. We will be using the same application to continue here.
First we need to create an app. As soon as we create it, we have a directory called resources, in the directory where we created the app.
In that resources directory, we need following files, some of them will already be there:
Now you need to modify the index.html
In the index file, we are having two input boxes for username and password. And one login button.
When login is clicked, the Login function is called from the javascript.
Here, we have called another function log_in logged = window.log_in(username, password)
This is defined in the python script:
In the python script part, we have used import login
.
This is the way to include, a python script. The documentation stated way of using Titanium.include() does not work now, you can see the 1.3.1 beta release notes.
For using python script’s functions or classes, we need to declare them in the window object, as the Documentation says,
Any class, function or variable that you want to pass from Python back to Javascript must be declared in the window object.
The following line does this for us,
window.log_in = login.main
Here, main function from login.py is declared as window.log_in, that can be used anywhere in javascript.
For using python in Tide, we have three ways.
We are using the external script. so now you need to create a file named login.py
And we are done. Now the javascript can call our python function, and if login is correct according to script, we are prompted with an alert box.
window.alert("Welcome!");
Comments
Leave a comment