Skip to content
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

Support ipython style HTML / SVG display with _repr_html_ #191

Open
tobiasgrosser opened this issue May 29, 2016 · 5 comments
Open

Support ipython style HTML / SVG display with _repr_html_ #191

tobiasgrosser opened this issue May 29, 2016 · 5 comments

Comments

@tobiasgrosser
Copy link
Contributor

Similar to ipython pypyjs is working in a web browser, so it is simple to display rich HTML / SVG output. Ipython makes this functionality available by allowing for additional repr*_ methods [1] that are used to print objects in HTML/SVG.

Using sys.displayhook (https://docs.python.org/2/library/sys.html#sys.displayhook) and jqconsole.Append() this could probably be implemented without too much effort.

[1] http://ipython.readthedocs.io/en/stable/config/integrating.html

@tobiasgrosser
Copy link
Contributor Author

Pasting the following piece of code in the console allows formatted output.

import js

class Test2:
  def __init__(self):
    return
  def __repr__(self):
    return "Test-Object"
  def __repr_html__(self):
    return "<div style=\"color:red; background-color: white\">Test-Object</div>"



def formatted_displayhook(x):
  if '__repr_html__' in dir(x):
    js.globals.jqconsole.Write(x.__repr_html__(), "", 0)
    print("")
    return
  print(x)
  print("")



t = Test2()
print("With old output")
t

import sys
sys.displayhook = formatted_displayhook
print("With new hook")
t

@rfk
Copy link
Contributor

rfk commented May 30, 2016

Thanks @TobiG, this is pretty neat! Are you interested in working on this yourself? I'd be happy to provide a few pointers as to how it could fit into the existing code and API.

@tobiasgrosser
Copy link
Contributor Author

On 05/30/2016 03:12 AM, Ryan Kelly wrote:

Thanks @TobiG https://github.com/tobig, this is pretty neat! Are you
interested in working on this yourself? I'd be happy to provide a few
pointers as to how it could fit into the existing code and API.

No promises, unfortunately. However, if you could point out how you
imagine this to work I might give it a shot at some quiet moment.

Tobias

@rfk
Copy link
Contributor

rfk commented Jun 3, 2016

I've been trying to think of a good way to incorporate this. The code for making an interactive shell with jqconsole lives in a separate repo here:

https://github.com/pypyjs/pypyjs.github.io/blob/master/index.html

So one option could be to just have that code execute something like your displayhook setup as part of its initialization procedure. But that's not very re-usable.

A more general solution might add support for this into the underlying pypyjs.repl method, e.g. by adding a second "displayhook" argument. But I'm not sure how to make that nice as an API.

@tobiasgrosser
Copy link
Contributor Author

What about doing the following:

  1. Allowing to (optionall) set vm.stdout_html, which by default aliases vm.stdout, but can be set to a
    special display function (e.g., the raw output function we used above).
    We could make this functionality available from the python side. I am unsure what the right interface
    would be here?

  2. Add a convenience function to 'vm' that loads a formatted display_hook as presented above and uses
    this HTML output interface of 1). As a result, we can just set these two options in index.html and
    editor.html and both should benefit from optional HTML output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants