Web Services - Sample 06: Using the JavaScript API

view page source quit pymol (FireFox only: view page source)

The PyMOL JavaScript API re-exposes PyMOL's Python programming interface directly to the JavaScript layer in an object-oriented way.

var pymol = new PyMOL(); // create PyMOL object instance for server of this page

var cmd = pymol.cmd; // assign a global symbol for the PyMOL cmd API
cmd.load('$PYMOL_PATH/test/dat/pept.pdb)

cmd.zoom('pept///1/',0,0,0,1)

cmd.zoom('all',0,0,0,2)

This eliminates the need for encoding your PyMOL commands as URL query strings and makes it possible to write JavaScript programs in your web page which control PyMOL in much the same way Python programs do.

Python (inside PyMOL)   JavaScript (inside Browser)
cmd.reinitialize()
cmd.load("$PYMOL_PATH/test/dat/1tii.pdb")
cmd.show_as("cartoon")
list = cmd.get_chains("polymer")
for chain in list:
    cmd.color("auto","chain "+chain)
 
cmd.reinitialize();
cmd.load("$PYMOL_PATH/test/dat/1tii.pdb");
cmd.show_as("cartoon");
list = cmd.get_chains("polymer");
for(var i=0;i<list.length;i++) {
    cmd.color("auto","chain "+list[i]);
}
Run this program
throw exception