Friday, June 6, 2014

How to create activities with sugar web and help them communicate with python

In the middle of my posts related to the sugar cordova project, I would also like to share a simple and straight process to develop activities on sugar web and help them communicate with native python.

This post is to help all the developers out there who might as well want to develop some cool activities for sugar using sugar web and might need to communicate with the native python back end using the javascript-python bridge.

So here go the steps which you must follow :

1. Setup up the sugar development environment :

2. Create a sample activity through command line - follow the steps mentioned here :

3.Once you are done with the initial set up of your activity then you can jump into more details, here are a few pointers on how to set up a link between native side and the javascript side using sugar web - 
  • Compare this file with the file given here
  • A sample call : look here in the first file goes to this line in the file
  • So basically if we want to implement something on native front and call it in javascript, so code up your class in python in sugar and import it in and then code up a function in activity class of which wraps up the calls to your class or simply you can put your logic in the function itself if its not huge ( for getting started dont go for building a seperate class and importing, code up your logic in a function there itself to test whether it works).
  • The function responsible for calling to the native side isbus.sendmessage.It has the following parameters :
    •  the class and function to which call is made : like activity.show_object_chooser
    • the parameters which you want to pass are defined in [] like [parameter1,parameter2,....]
    • the callback function which has two parameters for the success and failure - when there is success the error object would be null else it shall contain the error
  • The call back from native to javascript is made by the send_resultor send_error functions of the API Client class of file - you can see a sample usage of these functions in activity class functions of
  • So as per your code logic you either call send_error or send_result function - incase of error call send_error or else return the object through send_result
  • On the javascript side you check through the callback(3rd parameter of bus.sendmessage) that whether the error is null and invoke errorhandler or successhandler as per the result
  • Inside the function , you can access the parameters passed by : request['params'][0] , request['params'][1] and so on ...

I hope the interested developers are benefitted from the post.

A special thanks to Manusheel for motivating me to write this on blog ;-)

No comments:

Post a Comment