-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JS Plugins and JSON Handlers #2518
Conversation
Looks fine to me. Do you wan't all the plugin to be on one repository ? |
I don't think we should stop Notebook development until testing is done. On Wed, Oct 31, 2012 at 1:48 AM, Bussonnier Matthias <
Brian E. Granger |
It would be great if there were a way for the javascript to send messages back to IPython other than constructing a string and sending it in an execute request call. For example, it would be great if we could: (1) be able to register handlers on the python side for messages with a specific type (or some other metadata), and (2) send such custom messages from the javascript code. |
Yes, I have been thinking about this exact thing. I agree that right now On Tue, Nov 13, 2012 at 11:24 AM, Jason Grout notifications@github.comwrote:
Brian E. Granger |
Writing all code and data in strings is exactly what the IPython display messages eliminates (i.e., we have richer communication than just stdout strings). It makes sense to extend this capability also to messages back to python code. |
At pycon we wrote a simple plugin. About the only issue we had was that the interpreter tended to eat all of the exceptions that were raised by the serialiser (the code that turned a python object into json). To fix this problem, we wrapped the serialisation code in an exception handler and sent the traceback up to the javascript, which alerted the exception; this feature should probably be built into the framework. We actually spent most of our time fighting with javascript. I suspect javascript package management will become an issue as more complex javascript visualisers are written. |
Could you post a link to the exception handling code on this PR, so I can have a look. |
We took down the handling code in the master branch once we had the serialiser functioning. I've rewritten a simple example on this branch of our project: https://github.com/RishiRamraj/seepymol/tree/feature/exceptions |
I see what you mean.
On js side we can make sure that only if |
Although I haven't given it too much thought, I think the structure could following something along the lines of the parameters defined for the exit function in PEP 343:
I'm not sure if a json handler would ever be interested in the error codes returned by the python serialiser. |
Here are the main things we need to work our for this PR:
|
After working for a bit on implementing interactive javascript controls, I'd like the ability to register handlers for specific mime types, rather than an all-encompassing json type. That takes care of the namespace issues as well, since I can just invent my own mime type, like "application/sage-interact" or something. This addresses your second point---the handler is different than the content. |
Sounds a bit like the wild west. We would have to modify the entire On Sat, Dec 29, 2012 at 3:01 PM, Jason Grout notifications@github.comwrote:
Brian E. Granger |
Well, then you're back to 'how do you specify the handler?' The mime type is a great way (in fact, the current way) to change between handlers. I guess this is the same as the custom message discussion earlier, but one level of wrapping down from what we were discussing earlier. Do we want to wrap custom messages (your solution of using a json mime type) or give users a way to change the top-level msg type (my solution of allowing custom mime types). You're right that the current system really isn't using mime types correctly, since there isn't a way to ship purely binary data over json (e.g., the image/png mime type really isn't binary image data...). So your argument about the data not actually being the right mime type actually extends to the current system (at least if you wanted to be strictly pure). What happens now is that you indicate the intent/purpose of the data with the mime type, rather than the actual format, and it's up to the client to understand how you encoded the mime type to fit JSON. I agree that specifying the handler at the same level as the content is not very elegant. The handler should be specified above the content level. Another natural place to specify the handler would be the metadata for the entire message. What do you think about using the top-level metadata dict for specifying a handler? Or should that metadata dict be strictly preserved for the user? |
{'handler': handler_name, 'content': {actual data goes here}}
Brian E. Granger |
Are you proposing a message format of: Really, the mimetypes really are about selecting a handler on the client side, and the handler knows how to interpret the value of the key. You could also do a special mimetype of If you're proposing a handler attribute at the top level, then would it also be possible to specify a handler for messages other than display_data messages? Would it be possible to specify a handler for display_data messages that only had, say, a text/plain mimetype? |
I am thinking that the main content dict would have two subfields: handler I also don't want to get into allowing custom handlers for arbitrary data On Mon, Dec 31, 2012 at 7:58 AM, Jason Grout notifications@github.comwrote:
Brian E. Granger |
A couple of clarifying comments:
The difference between our two approaches seems to be just that I want to specify the intent of the content using the mime type, whereas you want to specify the intent using the mime type and a handler key. Is that a fair characterization? I think my approach plugs into the existing system very well and is simpler (one way of registering handlers for the message data). An implementation detail can say that the standard mimetypes are not available to be overridden. I think making the handler request disconnected from the data (e.g., having the handler specified separately from the content) is not as elegant as making the handler an integral part of the data (e.g., having the handler specified as a key with the content as the data). Also, if the handler is specified using the mimetype system, you can set up multiple handlers for the same message. What if, for example, I have a python library that can send data back and use one of three different custom handlers. Having the handler being specified by a key in the data dictionary means I can consolidate the display data in one message, and the client can decide which (or all) of the handlers called. Under your proposal, I'd have to send several different display messages, and all would be acted on. |
No, I mean that the "data" field on content would have the handler On Mon, Jan 7, 2013 at 1:16 PM, Jason Grout notifications@github.comwrote:
Brian E. Granger |
I'm still a little confused. Can you post an example message that specifies a handler? In particular, are other mimetypes allowed in the data dictionary? |
I'm on my phone so it is a little bit difficult to type a Json structure. The display data message type has a data attribute this data attribute would contain the Json data for the message being sent to the browser. I propose creating a handler sub attribute within the data structure that would contain the handler for that data. The actual Json data would sit alongside the handler attribute in the data sub structure. I can describe this more when I get home if you want. This is also how it implemented it in my JS plug-ins branch. Who Ray for voice recognition! Sent from my iPhone On Jan 7, 2013, at 2:18 PM, Jason Grout notifications@github.com wrote:
|
So like this?
|
No I am sorry I forgot one detail. Here is how it is now:
IOW, the handler attribute is part of the actual JSON payload. |
I am thinking about closing this PR. There are a number of larger design issues that we really need to work out first and those discussions are going beyond the scope of this PR. How do people feel about this (I would open an issue to track the larger issues). |
+1 to opening up the broader discussion |
I am closing this issue as there are larger design questions that have to be worked out. Follow that discussion at #2802. |
This add two new things to the IPython Notebook:
publish_json
and declare a JS function that will be the handler for that message.See:
https://github.com/ipython/jsplugins
For details and an Online Python Tutor example.