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

allow formatters to specify metadata #3190

Merged
merged 10 commits into from Apr 29, 2013
Merged

Conversation

minrk
Copy link
Member

@minrk minrk commented Apr 16, 2013

Part of IPEP 13.

A formatter can now return (data, metadata), in addition to just data.

A display_data message may now have a key in the content's existing metadata field for each format type.
These keys are not necessarily defined.

The main initial use of this code is to allow images to specify display size,
which should be the missing piece before implementing 2x figures (#2234).

Pinging @piti118, who plans to do that implementation on top of this.

"""shortcut for returning metadata with shape information, if defined"""
md = {}
if self.width:
md['width'] = self.width
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the units of width and height here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Integer pixels make the most sense, but technically anything HTML allows will work ("2em", etc.)

@ellisonbg
Copy link
Member

Previously the display publisher had the ability to publish a metadata dict, but we never used it. As I understand this PR, this does a couple of things:

  • It starts to use that metadata dict.
  • It standardizes the top-level keys of the metadata dict (mime types).
  • It allows formatters to pass in data to that metadata dict by returning a (format, md) tuple.

If this is correct we could use this to allow JS code to declare the name of a browser side handler in the metadata?

@minrk
Copy link
Member Author

minrk commented Apr 25, 2013

That seems correct. The metadata dict is currently a black box. This simply defines that mime-type keys are themselves black boxes that will be passed to the append_mimetype handlers. So you can still add any further arbitrary interpretations of the metadata dict.

@ellisonbg
Copy link
Member

Let's update the docstrings of the various classes/methods this PR touches to reflect that many return values are now a tuple of data, metadata, etc. Things like DisplayFormatter.format and friends.

@ellisonbg
Copy link
Member

DisplayPublisher.publish docstring should describe how the metadata dict now has top level keys that are the format types.

@ellisonbg
Copy link
Member

Same with docstring of publish_display_data.

@ellisonbg
Copy link
Member

I am wondering if we should change how the metadata arg to functions like publish_html and friends should be interpreted. Right now, that metadata arg is just passed through to the underlying publish function. Another option would be to assume that the metadata passed is for that particular format type and automatically add it to the global metadata dict with the right top-level mime-type key. I think that makes more sense.

@ellisonbg
Copy link
Member

The docstring and code of DisplayHook.compute_format_data is confusing. It looks and sounds like it will just return the raw data, but the method it calls returns the data, metadata tuple. The __call__ method that calls this one does the right thing, but it is still confusing.

@ellisonbg
Copy link
Member

DisplayHook.write_format_data needs an updated docstring.

@ellisonbg
Copy link
Member

Right now we use OutputArea.prototype._dblclick_to_reset_size to reset a resized image to its original size. Do we instead want to use the metadata provided sizes if they were passed?

@minrk
Copy link
Member Author

minrk commented Apr 26, 2013

I'm not fond of the publish_mime methods - they don't actually do what they describe, since they also publish plaintext for everything.

@minrk
Copy link
Member Author

minrk commented Apr 26, 2013

docstrings updated

@minrk
Copy link
Member Author

minrk commented Apr 26, 2013

Right now we use OutputArea.prototype._dblclick_to_reset_size to reset a resized image to its original size. Do we instead want to use the metadata provided sizes if they were passed?

We could, though the way we have it now will work, since it just uses the measured initial size, which will always be the specified size given with metadata, so it would be a bit redundant.

@ellisonbg
Copy link
Member

Are you sure? From looking at the code, I think the publish_foo methods only publish their own mine type.

@minrk
Copy link
Member Author

minrk commented Apr 26, 2013

Oh, that's even worse - I was thinking of display, which always includes plaintext. publish doesn't for some reason. Why do the publish_foo methods exist?

@ellisonbg
Copy link
Member

Originally, you couldn't publish metadata by calling display for using the
real formatters, so if you wanted to publish with metadata you had to use
these publish_foo methods. But now that we can publish data with
metadata, I think we don't need these. But we should add a metadata arg to
the main display classes (Image, HTML, Javascript, etc.), to make it easy
to publish data with metadata. Let's add that to this PR as it is a simple
change that will make the API more uniform. Then, I am fine removing the
publish methods if you want as well. Simplify, simplify, simplify...

On Thu, Apr 25, 2013 at 9:47 PM, Min RK notifications@github.com wrote:

Oh, that's even worse - I was thinking of display, which always
includes plaintext. publish doesn't for some reason. Why do the publish_foo
methods exist?


Reply to this email directly or view it on GitHubhttps://github.com//pull/3190#issuecomment-17055508
.

Brian E. Granger
Cal Poly State University, San Luis Obispo
bgranger@calpoly.edu and ellisonbg@gmail.com

@ellisonbg
Copy link
Member

IOW, display and the top level display objects should be our official
public display protocol API, not these low-level publish ones...

On Thu, Apr 25, 2013 at 9:51 PM, Brian Granger ellisonbg@gmail.com wrote:

Originally, you couldn't publish metadata by calling display for using the
real formatters, so if you wanted to publish with metadata you had to use
these publish_foo methods. But now that we can publish data with
metadata, I think we don't need these. But we should add a metadata arg to
the main display classes (Image, HTML, Javascript, etc.), to make it easy
to publish data with metadata. Let's add that to this PR as it is a simple
change that will make the API more uniform. Then, I am fine removing the
publish methods if you want as well. Simplify, simplify, simplify...

On Thu, Apr 25, 2013 at 9:47 PM, Min RK notifications@github.com wrote:

Oh, that's even worse - I was thinking of display, which always
includes plaintext. publish doesn't for some reason. Why do the publish_foo
methods exist?


Reply to this email directly or view it on GitHubhttps://github.com//pull/3190#issuecomment-17055508
.

Brian E. Granger
Cal Poly State University, San Luis Obispo
bgranger@calpoly.edu and ellisonbg@gmail.com

Brian E. Granger
Cal Poly State University, San Luis Obispo
bgranger@calpoly.edu and ellisonbg@gmail.com

@minrk
Copy link
Member Author

minrk commented Apr 26, 2013

we want a metadata dict kwarg on all display types? What should happen if someone specifies width/height as kwargs and via metadata? Or do we want to pass any **kwargs through as metadata?

@ellisonbg
Copy link
Member

Because we need to tightly monitor all metadata attributes, lets set the
metadata via specific keywords args like width/height. That way we can do
a better job checking their formats. For example, if we introduce a
handler metadata attr for the JSON mime-type, users would pass that in as a
handler kwargs to the display object.

On Thu, Apr 25, 2013 at 9:56 PM, Min RK notifications@github.com wrote:

we want a metadata dict kwarg on all display types? What should happen if
someone specifies width/height as kwargs and via metadata? Or do we
want to pass any **kwargs through as metadata?


Reply to this email directly or view it on GitHubhttps://github.com//pull/3190#issuecomment-17055696
.

Brian E. Granger
Cal Poly State University, San Luis Obispo
bgranger@calpoly.edu and ellisonbg@gmail.com

@ellisonbg
Copy link
Member

Important question: is this metadata saved in the notebook format? Seems like it should be otherwise when a notebook is reloaded, the metadata will be lost. If it is part of the notebook format, is that not a notebook format change?

@minrk
Copy link
Member Author

minrk commented Apr 26, 2013

yes, the metadata is saved in the notebook

formatters can now return either data or (data, metadata)

the resulting published dict has both content.data keyed by mime-types,a
and content.metadata keyed by mime-types.

metadata keys are not necessarily defined, and are generally undefined when they would have no content.

This fits fully within the existing message spec.
also consolidate a bunch of duplicate code
adds display(raw=True), which maps directly to publish_display_data on each arg.
@ellisonbg
Copy link
Member

So I think this code is ready to merge. The only thing we talked about was updating the message spec/nbformat documentation with information about the metadata. Do you want to do that in a separate PR, or include it here?

@minrk
Copy link
Member Author

minrk commented Apr 29, 2013

Ah, I updated the IPEP, but forgot to update the actual docs. I'll do that now. I will also add an example or two to the example notebooks.

@ellisonbg
Copy link
Member

OK great!

@minrk
Copy link
Member Author

minrk commented Apr 29, 2013

docs should be updated

@ellisonbg
Copy link
Member

Merging, great work!

ellisonbg added a commit that referenced this pull request Apr 29, 2013
allow formatters to specify metadata
@ellisonbg ellisonbg merged commit cf5a12a into ipython:master Apr 29, 2013
@minrk minrk deleted the image_size branch March 31, 2014 23:36
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
allow formatters to specify metadata
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

Successfully merging this pull request may close these issues.

None yet

2 participants