1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
<html>
<head>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Expires" content="-1" />
<title>Web Services - Sample 08: Using the JavaScript API</title>
<style><!--
a{text-decoration:none;
padding: 1px;
background-color: #dddddd}
--></style>
<script src="json2.js"></script>
<script src="pymol.js"></script>
<script type="text/javascript">
var pymol = new PyMOL(null, null, 'on'); // create PyMOL object with buffering enabled
var cmd = pymol.cmd; // assign a global symbol for the PyMOL cmd API
function demoLoop() {
cmd.reinitialize();
cmd.load("$PYMOL_PATH/test/dat/1tii.pdb");
cmd.color("white")
cmd.count_atoms()
count = pymol.flush()
for(var i=1;i<=count;i++) {
cmd.color("auto","index "+i);
}
pymol.flush()
}
</script>
</head>
<body>
<h3>Web Services - Sample 08: Using the JavaScript API</h3>
<a href="javascript:void(0)" onclick="window.open('view-source:' + location.href)">view page source</a>
<a href="/apply/_quit">quit pymol</a>
(FireFox only: <a href="javascript:void(0)" onclick="window.open('view-source:' + location.href)">view page source</a>)
<p>As you saw in Sample 06, the PyMOL JavaScript API re-exposes
PyMOL's Python programming interface directly to the JavaScript layer
in an object-oriented way. However, there are some performance issues
(and in some cases, reliability issues) associated with sending
hundreds or perhaps thousands of HTTP requests in a very short period
of time.</p>
<p>We have implemented a buffering system into the PyMOL JavaScript
API which enables many requests to be combined into a single compound
request, provided of course, that only one return value (the final
result value) is required by the client.</p>
<p>The API is used as before, except that we enable the buffering option
by passing 'on' as the third argument of the constructor</p>
<pre>
var pymol = new PyMOL(null, null, 'on'); // create PyMOL object with buffering enabled
var cmd = pymol.cmd; // assign a global symbol for the PyMOL cmd API
</pre>
<p>The trick to using buffering in the same origin scenario is to remember two things:</p>
<ol>
<li>PyMOL API calls are not actually invoked until you call pymol.flush()</li>
<li>The value returned from the final invocation is the result from pymol.flush()</li>
</ol>
<table>
<tr>
<th align="left"><b>Python (inside PyMOL)</b></th>
<th width="50"> </th>
<th align="left"><b>JavaScript (inside Browser)</b></th>
</tr>
<tr>
<td valign="top"><pre>
cmd.reinitialize()
cmd.load("$PYMOL_PATH/test/dat/1tii.pdb")
cmd.color("white")
count = cmd.count_atoms()
for i in range(1,count+1):
cmd.color("auto","index "+str(i))
</td><td> </td>
<td valign="top"><pre>
cmd.reinitialize();
cmd.load("$PYMOL_PATH/test/dat/1tii.pdb");
cmd.color("white")
cmd.count_atoms()
count = pymol.flush()
for(var i=1;i<=count;i++) {
cmd.color("auto","index "+i);
}
pymol.flush()
</pre>
<a href="javascript:void(0)" onClick="demoLoop()">Run this program</a>
</td>
</tr>
</table>
<p>Note that you can turn buffering on or off at any time with the
setBufferMode method of the pymol object:</p>
<pre>pymol.setBufferMode('off') // disables buffering
list = cmd.get_chains('1tii') // immediate calls can now be made, with results returned
pymol.setBufferMode('on') // restores buffering (must call flush to force execution)
</pre>
</body>
</html>
|