🏠 back to Observable

Custom representation for object in the output cell?

Hi,

As an example, I am creating a simple Vector prototype to do some geometric computation :

u = new Vector( [2, 4] )

the folded output is:

Vector { coords: Array(2) }

In this situation, the folded output gives no information about the actual object. It would be great to have the possibility to define a custom output format for an object. I think about the __repr__() method in python. Maybe there is already a way around this I don’t know ? I have tried to change the toString() method…

Similarly, sometimes the definition is more valuable than the output.

For example, here I want to plot the function y = f(x).

I would prefer to keep visible the definition only and not the output, which is a large unreadable array and provides me no more information than the graph.

Thanks for the work!! It is already a hard choice between Jupyter notebook&Python and Observable…

in Manual Mutable Values, @jashkenas defines a function that shows how to provide a value and a view for an object. That might be a way to solve your problems.

If you put a wrapper wrapper function around your Vector method, you can display whatever you like :slight_smile:

To share some of our plans here:

  • There’ll be plenty of ‘userland’ solutions possible already and in the future, like viewof cells

In the works is an implementation of devtoolsFormatters, a semi-obscure but hella cool way to provide custom formatters for object types, and the situation you propose will be perfect for them. See the Immutable devtools formatter for example: we plan to support the same API but to make it available outside of your browser’s devtools and on the page, and in all browsers rather than just Chrome.

2 Likes