Fixes to the display system #370

Merged
merged 4 commits into from May 17, 2011

5 participants

@ellisonbg
IPython member

The IPython formatters use special methods to compute the format
of objects. These special methods have names like __html__, but
with this commit these have been changed to _repr_html_.

I have also added a Javascript formatter and fixed a bug in
pylab tools in getfigs.

Some remaining questions:

  • We are still using the old logic in pretty.py and the special name __pretty__. Do we want to update pretty.py as well and use _repr_pretty_?
  • Are we happy with the special names in this branch?
  • Should _repr_png_ return the raw png data or base64 encoded data (required for sending over json). Currently we base64 encode.
  • Should the latex representation include the wrapping $ or $$?
  • Should the javascript repr output the raw javascript or include it in a <script> tag pair.
@mdboom

To answer your question: "Should the latex representation include the wrapping $ or $$?", meaning (I think), the result of repr_latex should include the dollar signs, I would advocate for "yes". The matplotlib mathtext parser supports text outside of math mode, eg.:

r"Result of expression: $\sqrt{x}$"

Also, if one ever wanted to use a "call out to the real LaTeX" approach to this, there's all kinds of text layout features in LaTeX outside of math mode that one might want to use.

@mdboom

Quick sanity check also: Is the LaTeX inline display not yet working? I see how I can get it to display LaTeX for Sympy objects, but what about an arbitrary object with a repr_latex method?

@ellisonbg
IPython member

The repr_latex method does work, but the qtconsole does not have native latex rendering. The htmlnotebook we are working on does have native latex support, but it is not all wired up in the Javascript yet. Not sure how to get qt to handle latex (without rendering to png like we do for sympy currently).

@mdboom
@ellisonbg
IPython member
@mdboom

Yeah -- the mathtext stuff in matplotlib is still a subset of what a standard LaTeX distribution has. It's reasonably complete wrt to TeX, on which it is based, but it's missing most of the macros that LaTeX layers on top.

You could use dvipng and real LaTeX to generate the equation images if LaTeX is available. It adds a really heavy dependency, but it's the only thing I'm aware of that is as complete as LaTeX (because it is!). The Sphinx mathpng extension is a good concise example of using dvipng to get a LaTeX image. There is dvi parsing code in matplotlib, but it is complicated by the fact that it deals with scalable fonts etc. -- something you wouldn't necessary want to do for an interactive console.

@mdboom

I've added a function to matplotlib master that converts a math expression alone to any of the image formats that matplotlib supports. See this pull request:

matplotlib/matplotlib#84

Since that function won't exist in a released matplotlib for a while, you may want to copy it into IPython and use it when it isn't available in matplotlib -- it uses things that have been stable for quite some time.

@fperez
IPython member
@minrk
IPython member
  • Should the javascript repr output the raw javascript or include it in a <script> tag pair.

It should definitely exclude the script tag, because the tag is HTML, and it would no longer be a valid javascript representation if you include it.

The use case is javascript-based frontends. If the frontend wants to do any handling on the data, it would have to strip the tags before parsing. It's more trivial to add tags in frontends that just want to echo into HTML than it is to strip tags for frontends that want to look at the data.

@ellisonbg
IPython member

OK, I am going to make $/$$ required for latex output, the png/etc formats will not encode (zmq layer will do that) and the javascript will includes the <script> tags. Still waiting for @rkern to give us his thoughts on the pretty.py issues. Also, we will copy the new function @mdbloom wrote for generating svg output from latex and incorporate that into a default latex formatter.

@rkern

Sure.

@minrk
IPython member

Please don't include <script> tags, for reasons described in my previous comment.

@ellisonbg
IPython member

@minrk: OK, I understand your logic here and I think I agree with you.

ellisonbg added some commits Apr 11, 2011
@ellisonbg ellisonbg Renaming the special methods of the formatters.
The IPython formatters use special methods to compute the format
of objects. These special methods have names like "__html__", but
with this commit these have been changed to "_repr_html_".

I have also added a Javascript formatter and fixed a bug in
pylab tools in getfigs.
43a27cb
@ellisonbg ellisonbg Renamed __pretty__ to _repr_pretty_ and changed updated pretty.py
* Throughout the codebase, __pretty__ has been changed to
  _repr_pretty_ to match general convention for special method
  names.
* The logic in pretty.py now matches that in formatters.py in that
  formatters that are callables are tried first and then special
  methods are used.
4241df5
@ellisonbg ellisonbg Misc updates the display system.
* PNG base64 encoding is now done at the ZMQ level, not in the
  display formatter itself.
* All formatters are documented as to the exact nature of their
  return value. PNGs are not base64 encoded, LaTeX must include
  $/$$, Javascript should not have the <script> tags, etc.
* Updated the Circle display example in docs/examples/core.
* Renamed the sympy printing extension to sympyprinting.py.
e7df0ac
@ellisonbg ellisonbg Added mdboom's math_to_image from matplotlib. 290c0f4
@ellisonbg ellisonbg merged commit 290c0f4 into ipython:master May 17, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment