I am searching for a way to call a python function of my Bokeh Server App from arbitrary Javascript. I want to write custom widgets using HTML/Javascript only and use them to interact with the Bokeh App. I know that one can extend bokeh. Is this is the only way to go? For fast prototyping I think it would be nice to just use the websocket connection to send requests to the app, something along this line:
Bokeh.call('my_python_func', arg1, arg2)
As of Bokeh 0.13.0 this capability does not yet exist directly in the library. You can follow the GitHub issue #5983 Events with feedback / Simple RPC to get updates about any ongoing development.
Otherwise, any alternatives are somewhat involved. If the Python you would like to call is available behind some REST API you could certainly invoke that from JS using standard means.
Related
I'm thinking of switching from PHP to using Node.js for developing my website. However, after researching Node.js for a little while, I can't seem to find exactly how to write a webpage with Node. I see that you use response.write() in Node to write html to your webpage, but that seems like a tedious thing to do, having your entire webpage as a string literal in your node file. How does web development work in Node as opposed to PHP's method of embedding the script into the HTML file?
You don't necessarily need to use response.write for each line of the view, you can use template engines as well. Search for "node.js template engines". At first impression it could seem tedious, but a similar approach prevents you from writing bad code.
PHP is a scripting language, node is a platform built on javascript.
To start web development using node.js, at first you have to understand what makes node different. Node gives you a way to make async calls to your database (which is a very simplified explanation), which you can then wrap in nice html and send (route) it to the browser. Alternatively, you can use something like angular.js in the frontend and use node.js to make db requests and response which is picked up by angular.js which updates the front html. If you like the idea of Single page app with async calls to fetch data, use node with angular. The tutorial that I like is https://scotch.io/tutorials/creating-a-single-page-todo-app-with-node-and-angular Hope this helps!
As others have answered, there exist templating engines for Node. With the current trends in web development, most modern web frameworks encourage the separation of code from the view (or the HTML you deliver to the client). For instance, Ruby's ERB templates, Jinja2 in Python, Handlebars/Jade for Node, and now a lot of modern PHP frameworks support this as well (Zend/Slim).
Another main difference is in how they work and how the languages are designed. PHP is an object oriented language supporting classes, inheritance, member visibility, interfaces, etc. Node.js is Javascript, so using prototypical inheritance.
The communities and ecosystems are different as well. Modern PHP tends to embrace the use of the Composer package manager, and that came after PEAR. However, npm is the official node package manager and it is deeply integrated with the platform. It is trivial to search for new packages and then use them in your projects.
The main architectural difference is that Node is also asynchronous by design, meaning it runs in a single thread and can potentially handle much more connections than PHP on systems with limited memory. When a request comes in to a PHP application, all the services/controllers and everything you defined have to be reinstatiated, you define PHP files and let Apache/Nginx process them. In Node you have a node process which you can proxy outside requests to.
Node.js Provides so many modules to do these things there is framework called express for node.js http://expressjs.com/ You can use a templating engine and create views. some examples are like ejs or jade. It doesnt have to be a string.
PHP is very strongly orientated towards creating web pages from a template, while Node.js is lower-level and broader in scope. A very rough overview of the differences between PHP and Node.js:
In PHP, you'd start a web server (almost certainly Apache), and then put a PHP file in a directory where you want to serve something from. You might use some fancy .htaccess directives to make URLs nicer, etc.
In Node.js you create a script, in which you use the http module to start a web server, and then you supply a callback for whenever a request is made to your server. Deciding which page to respond to the request with, etc, is all your work to do.
In PHP, things like routing a request to a particular PHP file, compression, decoding POST and GET variables, are all done by using Apache - your PHP files are sort of just like templates which Apache runs whenever a request is received. In Node.js, everything, from starting the server to sending HTML, is all done within your Node.js script - you have to do everything.
HTML isn't the first class citizen in Node.js that it is PHP. Generally, in Node.js you are just sending strings to the client. There are plenty of third party templating tools for Node.js - but they will be dependencies, not builtin functions.
Am new to ExtJS Direct and DirectJNgine. The relation between them confuses me. I cannot get the overview picture.
What I know is:
ExtJS is a JavaScript framework focusing on UI design. It supplies plethora APIs for programmers. I can call these APIs without bitter.
From the official site:
Ext Direct is a platform and language agnostic technology to remote server-side methods to the client-side. Ext Direct allows for seamless communication between the client-side of an Ext JS application and all popular server platforms.
My think is: ExtJS Direct is also a subset of the APIs supplied the ExtJS team for us to use. These APIs can facilitate our work on calling server side method. But I want to dig into a little bit.
Server side method can be any language method, like JAVA C++ PHP etc. Direct APIs handle all possibilities to support different server side language methods?
From the official site:
DirectJNgine (or DJN, for short), is a Java based implementation of the Ext Direct API for ExtJs.
ExtJs is probably one of the most powerful and attractive UIs for web-based applications, as can be seen here, as well as in many other examples. DirectJNgine makes it much easier to use the full power of Java business classes with such a powerful front-end, making Java methods directly callable from the client.
Since ExtJS Direct is already implemented the APIs for seamless remote method invoking. Why DirectJNgine need to implement these ExtJS Direct APIs? What does "a java based implementation of the Ext Direct API for ExtJs" mean?
I'd add server-side to the definition DirectJNgine:
DirectJNgine is an Open Source library that provides a Java-based implementation of the server-side Ext Direct API.
Ext Direct is a javascript framework and resides on client and let's you call any server API. The call is made via HTTP - that's why it's not dependent on the server language. You formulate a HTTP request, not a Java/PHP/any other request.
Server API handles the HTTP request and can be implemented in Java, C++, PHP, .Net etc. One of the Java implementations is DirectJNgine. It runs on server and responses to Ext Direct calls.
In your project you can use both Ext Direct and DirectJNgine for communication between client and server. They don't overlap, they cooperate with each other on the opposite sides of the network.
And you'd use some framework for the presentation layer (ExtJS maybe) and some framework for server business/storage/other layers (to make use of DirectJNgine it would be a Java framework).
Extjs needs to know the URL, the methods and the parameter count for the possible actions and this is provided by for example DirectJNgine.
The API also takes care of messaging between ExtJS and JAVA , i.e. converting incoming JSON objects to JAVA methods (Json with method names and parameters is passed to JAVA methods, their can be multiple actions in one call). And passing back the result from the JAVA methods in JSON format.
I did a bit of digging into extdirectspring when I realised some strange stuff was going on and wanted to know why. Effectively what extdirect does is provide a description of the server side methods to the client in the form of json (+ possibly through other mediums).
On the client side you end up with objects built from this json that know how to call the server code. In the java (extdirectspring) implementation this creates a static variable in your program's namespace for each of the java classes decorated with the 'ExtDirectMethod' attribute. This variable seems to always be the class name with the first letter lowercased. I had a bit of a dig and this appears to be by coincidence - the name comes from the bean name in spring and is not defined by extdirectspring.
In extdirectspring the setup works through the ether (through the servlet + spring) (have yet to bottom out exactly how this works) and it is predefined to respond to requests for api.js (among other things). This then returns the configuration object which is assigned to a static variable in the ext namespace. By default this variable is called REMOTING_API.
To get the client side stuff to know about what the server has to offer you need to put in a call like:
Ext.direct.Manager.addProvider(Ext.app.REMOTING_API);
after extjs has fired up.
I heard some facts about ZeroMQ, and I think it's very powerful thing. But now I try to imagine how it can be applied in web application.
Could you make an example of using ZeroMQ in web applications?
So, the first that strikes me - simple chat application. So, we need frontend and backend. I prefer using python+Tornado as backend. There is python lib for using ZeroMQ. It's clear. So, the next thing is frontend. In frontend I will use some javascript to interact with backend.
So, to do this I should use ajax calls, right? Are there some other ways to do it?
TIA!
The easiest way to do this is to map WebSockets to ZeroMQ sockets, which is quite simple with tornado and PyZMQ's ZMQStream objects. An example of such an app is the IPython Notebook. This approach has the downside of requiring websockets, which puts a limit on what browsers you can support. Of course, you could also map ajax calls with jQuery, etc. and handle the relay with async handlers in tornado.
A more sophisticated web:ZeroMQ app is the mongrel2 webserver.
The right choice for you is just going to depend on your communication patterns.
I'm writing a CouchDB sample. My intention is to write a web app using AJAX and deploy it in the tomcat environment. As I use the CouchDB I would like to know the way to interact with the CouchDB server. However there were few examples but I have few concerns over that.
1) As my application is deployed in a web server is it possible to connect with a CouchDB outside? Many examples uses apps which are deployed in CouchApp or Couch environment itself.
2) If so will it end up with cross domain issues. I found this in here
Connection AJAX, CouchDB and JavaScript
However will this be a problem? Is this the correct way to achieve this?
3) To omit above issues is it possible to use some server side javascript implementation as an example Rhino?
http://www.mozilla.org/rhino/doc.html
4) If I use Rhino above it wont allow many java script libraries to use. How do I interact with CouchDB then? Do I have to invoke native Javascript? Cant I use something like JQuery?
5) What are the best libraries for client side to achieve my goal?
Your help is appreciated.
Thank you
Based on your need to use tomcat, I would just recommend using ektorp (http://ektorp.org/). Just think of ektorp like a jdbc driver. No need to do ajax from the user interface, or no need to use javascript from java in your server.
Ektorp also lets you annotate your java classes with the javascript couchdb needs to make views. see the example here: http://ektorp.org/reference_documentation.html#d100e753
Hope that helps.
Instead of creating every websocket and defining the entire structure by hand, is there a library that will let me run a function on the Node.js server, that can call a related function on all clients connected to the server simultaneously? Likewise, can I securely call a server function FROM the client browser? I feel like every time I have to construct a command to send over the web socket, I'm working on the transmission layer instead of the application layer, and I want to be thinking at the higher layer the entire time.
I wouldn't mind building something like this myself if it doesn't already exist, but I have a hard time believing this isn't solved on node already.
What you are really looking for is an node.js RPC solution. Here are a couple of node.js RPC options:
DNode - shows some good examples.
BERT-RPC
nowjs
I have not personally used them, but they look like they have good potential.
Try to look at now.js.
Try msg-rpc, it just provides the rpc support you need, also no particular requirement for the websocket library of server/client you already have. Tell how to send out a message and forward the messages received, that's all.