Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/barneygale/JSRPC
For your web ui: access javascript variables and functions in native python
https://github.com/barneygale/JSRPC
Last synced: 13 days ago
JSON representation
For your web ui: access javascript variables and functions in native python
- Host: GitHub
- URL: https://github.com/barneygale/JSRPC
- Owner: barneygale
- License: mit
- Created: 2011-10-07T20:27:43.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2011-10-12T14:11:42.000Z (about 13 years ago)
- Last Synced: 2024-08-01T22:57:32.184Z (3 months ago)
- Language: Python
- Homepage:
- Size: 178 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# JSRPC
JSRPC _(javascript remote procedure call)_ is a small library for those wishing to write python
applications with web interfaces. the JSRPC class creates a small HTTP server, through which AJAX
requests can be exchanged.The project consists of two parts:
1. A python library providing a JSRPC() object, through which javascript calls can be made
2. A small jQuery plugin to handle these calls## Usage
in python:import jsrpc
js = jsrpc.JSRPC()
#For example:
print "window.location.href: %s" % js.sync.window.location.href
print "window.prompt(): %s" % js.sync.window.prompt('What is your name?')in javascript:
$(document).ready(function() {
$.jsrpc_start();
});
## Sync and Async
synchronous calls wait for javascript to return a value. They take the form:js.sync.window.prompt('Why are you here?')
asynchronous calls won't block, and will call a specified callback function with the value.
def callback(text):
print text
js.async(callback).window.prompt('Why are you here?')if you don't care about return values, you can cast into the void:
js.async.window.alert('To see with eyes unclouded by hate.')
## Limitations
### Data format
You can only pass around data that can be encoded as JSON. numbers, arrays, dicts, strings, etc
are all fine, but you won't get anything back from js.sync.document.getElementById('blah'), and
you can forget about most objects.### Variable access
When accessing variables in async mode, you will _always_ needs to call .flush(). For example:def callback(location):
print location
js.async(callback).window.location.href.flush()This is also _sometimes_ true for variables in sync mode. Most of the time it will work, but you
may run into situation when it won't.## Messages
Sometimes you want to alert your python program of something from javascript, outside of a return
value. For this, you can use messages. First assign a message handler in python:def get_message(message):
print "got a message:", message
js.message_handler = get_messageYou can then send messages (with an arbitary data format) to python from javascript:
...
$(document).ready(function() {
$.jsrpc_start();
$('#send').click(function() {
$.jsrpc_message($('#text').val());
});
});## Webserver options
JSRPC packages a basic webserver based on BaseHTTPServer.HTTPServer. You can pass options to JSRPC():* `server` specifies a server object. It is passed the `jsrpc.io` method, and any kargs, in its constructor
* if `server` isn't specified, you can configure the default server:
* `interface`: the interface to listen on (defaults to '')
* `port`: defaults to 8080
* `http_root`: directory to serve files from
* `request_handler`: subclass of BaseHTTPRequestHandler. Defaults to webserver.JSRPCRequestHandler.Subclassing JSRPCRequestHandler is useful for handling GET and POST yourself
class MyRequestHandler(webserver.JSRPCRequestHandler):
def _do_GET(self):
...
def _do_POST(self):
...js = jsrpc.JSRPC(request_handler = MyRequestHandler)
Note that the jsrpc.RequestHandler is almost identical to BaseHTTPRequestHandler, so
base your extension on that. You get passed every HTTP request that isn't part of the RPC stuff.