Jump to: navigation, search

Difference between revisions of "Scout/Tutorial/3.8/Jython Integration"

< Scout‎ | Tutorial‎ | 3.8
(Add Logic for Jython Interpreter and Interaction with Scout Form)
(Add Logic for Jython Interpreter and Interaction with Scout Form)
Line 52: Line 52:
 
* Updating the ''sys.path'' should conceptually go to a place where it's calles once per client startup.  
 
* Updating the ''sys.path'' should conceptually go to a place where it's calles once per client startup.  
 
* There might be more elegant ways to ensure Jython is able to access the Python modules provided in the ''Lib'' subfolder of the ''jythonlib.jar''.
 
* There might be more elegant ways to ensure Jython is able to access the Python modules provided in the ''Lib'' subfolder of the ''jythonlib.jar''.
 +
 +
== Play around ==
 +
In the Scout SDK do the following
 +
# Select top level element ''org.eclipse.scout.jythontest'' in the Scout Explorer
 +
# Start the server by clicking on the corresponding icon in the server box of the section ''Product Launchers''
 +
# Start a client by clicking on acorresponding icon in a client box (Swing, SWT, or RAP)
 +
# The client starts and the desktop form is shown
 +
## Enter some text into the ''Line'' field
 +
## Enter a python script into the ''Python'' field
 +
## In field ''Input'' specify the Python variable representing the text in the ''Line'' field
 +
## In field ''Output'' specify the Python variable holding the output of the Python script
 +
## Click on ''Run'' to run your Python script, the output will be transferred into the field ''Result''

Revision as of 16:12, 1 June 2012

Introduction

With Release 3.8 the Scout SDK offers support to integrate external JAR files into a Scout application with a few clicks. In this tutorial we use this capability to demonstrate how Jython may be integrated in your Scout application.

According to the Wiki "Jython is a Java implementation of Python" and Python itself claims "You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs." In any case, we need an example library here and combining Java with a powerful scripting language can help to solve a significant variety of problems.

The result at the end of this tutorial will look similar to the screenshot below:

Jython integration.png

Building the Jythonlib.jar =

bla

Create New Scout Project and add the Library bundle

bla

Add and Configure the Form Fields on the Desktop Form

bla

Add Logic for Jython Interpreter and Interaction with Scout Form

  1. In the Scout Explorer select the RunButton element of the DesktopForm
  2. In the Scout Object Properties click on the green plus-icon next to the link Exec Click Action to add the corresponding method
  3. Replace the proposed implementation with the code provided below
 @Override
 protected void execClickAction() throws ProcessingException {
   getResultField().clearErrorStatus();
   try {
     // make sure Lib is visible to access python modules
     PySystemState sys = Py.getSystemState();
     PyString pyLibPath = new PyString("__pyclasspath__/Lib");
     
     if (!sys.path.contains(pyLibPath)) {
       sys.path.append(pyLibPath);
     }
     
     // get interpreter, read input variable from input field
     PythonInterpreter pi = new PythonInterpreter();
     pi.set(getInputField().getValue(), new PyString(getLineField().getValue()));
     
     // run script, transfer output to result field
     pi.exec(getPythonField().getValue());
     getResultField().setValue(pi.get(getOutputField().getValue()).asString());
   }
   catch (Exception e) {
     getResultField().setValue(null);
     getResultField().setErrorStatus(e.toString());
   }
 }

Remarks:

  • Updating the sys.path should conceptually go to a place where it's calles once per client startup.
  • There might be more elegant ways to ensure Jython is able to access the Python modules provided in the Lib subfolder of the jythonlib.jar.

Play around

In the Scout SDK do the following

  1. Select top level element org.eclipse.scout.jythontest in the Scout Explorer
  2. Start the server by clicking on the corresponding icon in the server box of the section Product Launchers
  3. Start a client by clicking on acorresponding icon in a client box (Swing, SWT, or RAP)
  4. The client starts and the desktop form is shown
    1. Enter some text into the Line field
    2. Enter a python script into the Python field
    3. In field Input specify the Python variable representing the text in the Line field
    4. In field Output specify the Python variable holding the output of the Python script
    5. Click on Run to run your Python script, the output will be transferred into the field Result