This section will guide you step-by-step through a variety of ways to run Python scripts together with Plexus Connect application.
Following basic workflow, when using connect_api
in Python script is:
DataApi
Session
Session
The simple example script below is written for Demo Project Pubchem Grid View
:
# Create api : api = DataApi('username', 'password')
# warning: we don't recommend storing
# credentials directly in the script,
# but use different means in python how to obtain the password from the user.
api = DataApi('admin', 'admin')
# Open Session
session = api.openViewSession(
api.getViewByName('Pubchem grid view')
)
# empty query to load all data into session
session.query()
# get values for some of the columns
result = session.getData(['Mol weight', 'Structure', 'CdId', 'Acceptors'])
print(result)
Save the script above as example.py
and run it from command prompt. If the connect_API
is installed correctly, the script returns the expected results.
More examples can be found in the examples
directory, which is a part of the connect_API
content.
pip3 install jupyter
command.
For more details visit the Jupyter webpage here.Connect_API
functionalities used in Jupyter Notebook.For using connect_API
for scripting in Connect frontend, a subclass of ConnectScript
needs to be created first. This lets you react on user input and modify frontend state.
ConnectScript
is a Python script, which can be called by Connect frontend and can react on user actions like opening a view, clicking, querying or selection.
Example of a simple script:
class SimpleScript(ConnectScript):
def onLoad(self):
self.response.showMessage('Hello World')
SimpleScript().execute()
For usage from Connect frontend, this script needs to be saved in
~/.chemaxon/plexus-suite/scripts-data/{schemaId}/
~/chemaxon/plexus-suite/scripts-data/{schemaId}/
ViewId
in the scripts.conf
file in the same directory.Directories in the scripting folder are automatically created during startup of the Connect server and named according to the deployed schemas. Each deployed schema belongs to a folder with the same name {Schema_ID}
. Scripts are classified per schema and identified by Schema_ID
.
Content of the script.conf
file:
view id;javascriptfile.js;pythonfile.py
213A0E201AB2EB5DA54552018904C869;;hello_world.py
The ViewId
can be obtained from the address bar. When a view is opened in a browser, the Connect URL contains the ViewID
as it is shown in the following URL example:
http://your_server.com/grid/6C6F63616C6462_213A0E201AB2EB5DA54552018904C869/1
More examples can be found in the examples
directory, which is a part of the connect_API
content.
You can use DevelopmentConnectScriptContext
to make the development loop shorter by running a frontend script locally without copying it to the server.
The basic workflow looks like this:
There are three action events available, as are shown in the table below:
Script Action | Parameters | Description |
---|---|---|
onLoad | called when a view is loaded | |
onSelectionChange | {selection: <selection>} | called when a selection is changed |
onClick | {id: <buttonid>} | called after click on the button |
To copy a serialized frontend event you want to run locally:
DevelopmentConnectScriptContext
DevelopmentConnectScriptContext
DevelopmentConnectScriptContext
and copy the finished script to the serverExample script:
#script example which you need to develop
class SimpleScript(ConnectScript):
def onLoad(self):
self.response.showMessage('Hello World')
# connection part which returns #JSON for the one round of the loop
# this part will be removed, when script developing is finished
SimpleScript().execute(DevelopmentConnectScriptContext(
username="<USERNAME>",
password="<PASSWORD>",
connectUrl="http://your_server.com", # Connect backend location
sessionUrl="http://your_server.com/grid/6C6F63616C6462_213A0E201AB2EB5DA54552018904C869/6", # URL to parse Session out of
# Event to process - captured using Connect Frontend scripting tool dialogue
# Event type "onLoad", "onSelectionChange", "onClick"
event="""{
"viewId": "6C6F63616C6462_213A0E201AB2EB5DA54552018904C869",
"eventType": "onSelectionChange",
"params": {
"selection": [
4
]
}
}"""
))
Confirm that Python is properly installed. The simplest way to test for the existence of Python installation is to open the command prompt. Once the command prompt window opens, type python
and press Enter. If you have Python installed correctly, you should see output similar to the following:
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32.
Type "help", "copyright", "credits" or "license" for more information.
If you receive a message like:
Python is not recognized as an internal or external command, operable program or batch file.
Python is either not installed correctly or the system variable path
has not been set.
Connect_API
The Python library for Connect scripting can be downloaded from the Chemaxon download pages with the appropriate Connect version. It can be found in the Additional Downloadables section here.
The Connect_API
library of the same version as Connect is required to run Python scripts.
The Python library can be installed by using pip install connect-api-*.tar.gz
, where *
is the version number which must correspond to the Connect version.
Pip is the package installer for Python, pip itself is a package that someone might want to install, if it is not a part of Python.