Webware / WebKit / Examples /

# JSON-RPC demo client contributed by Christoph Zwerschke

    import simplejson
except ImportError:
        import json as simplejson
    except ImportError:
        simplejson = None

from ExamplePage import ExamplePage

class JSONRPCClient(ExamplePage):
    """Demo client for using the JSON-RPC example."""

    def writeJavaScript(self):
        if not simplejson:
            <script type="text/javascript" src="jsonrpc.js"></script>
            <script type="text/javascript">
            jsonrpc = new JSONRpcClient("JSONRPCExample");
            methods = jsonrpc.system.listMethods();
            function dojsonrpc() {
                p = document.getElementById("parameter").value;
                i = document.getElementById("method").selectedIndex;
                r = jsonrpc[methods[i]](p);
                document.getElementById("result").value = r;

    def writeContent(self):
            <h3>JSON-RPC Example</h3>
            <p>This example shows how you can call methods
            of a <a href="">JSON-RPC</a> servlet
            built with Webware for Python from your web browser
            via JavaScript (which has to be activated to run this demo).
            <noscript><span style="color:red">
            Unfortunately, JavaScript is not activated.
            <p>The example uses a JSON-RPC JavaScript client
            based on Jan-Klaas' "JavaScript o lait" library
            (<a href="">jsolait</a>).
            You can also use other JavaScript libraries and toolkits
            such as <a href="">dojo</a> or
            <a href="">pyjamas</a>
            for that purpose.</p>
            <p>In order to run the JSON-RPC servlet,
            <a href="">simplejson</a>
            must be installed on your server.''')
        if not simplejson:
            self.write(''' <span style="color:red">
            Unfortunately, simplejson is not installed.</span>''')
            <p>Type in any example text to be used as input parameter,
            choose one of the available methods to be invoked by the
            example servlet and press the button to display the result.</p>
            <tr align="center">
            <th>Input parameter</th>
            <th>Remote method</th>
            </tr><tr align="center">
            <input id="parameter" type="text" size="20" value="Hello, World!">
            <select id="method" size="1">
            <script type="text/javascript">
            for (m in methods)
                document.writeln('<option>' + methods[m] + '</option>');
            <input type="text" size="20" id="result">
            </tr><tr align="center">
            <td colspan="3">
            <input type="button" value="Invoke remote method"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.