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

Define Python API hook for third party libraries to control how they display in the variables pane #5573

Open
wesm opened this issue Dec 2, 2024 · 2 comments
Labels
area: variables Issues related to Variables category. lang: python

Comments

@wesm
Copy link
Contributor

wesm commented Dec 2, 2024

Motivated by #5499 -- we can add custom display code in the Positron Python kernel on a case by case basis when we find that __repr__ renders badly or some other reason (performance or unwanted computation). But: dealing with display rendering for third party libraries within Positron has a number of drawbacks:

  • We are responsible for maintaining this code, and handling a new third party library requires making changes to Positron and waiting until a new version is released to get the improvement.
  • If there is a breaking change to a third party library that causes the display rendering code in Positron to stop working, then the current and all previous versions of Positron will all be broken with the new library version going forward.

Clearly, this is not ideal! IPython / Jupyter in the past had provided a _repr_html_ display hook for third party libraries to provide an HTML rendering for the Jupyter notebook. We could define a similar _repr_positron_ (open to ideas on the name) or similar hook to allow libraries to define how they display in the variables pane. If this method isn't present, we can fall back onto __repr__.

@wesm wesm added this to the Release Candidate milestone Dec 2, 2024
@juliasilge
Copy link
Contributor

Just for visibility, we have already invested in extending the Variables pane for R in posit-dev/ark#560 and related PRs. I'm not sure if we learned much that is transferable, however, other than that we needed to do this?

@wesm
Copy link
Contributor Author

wesm commented Dec 3, 2024

Thanks for cross-linking! Based on what was built there, a collection of different APIs for each class:

Ark currently supports six methods with the following signatures:

    ark_variable_display_value(x, ..., width = getOption("width"))
    ark_variable_display_type(x, ..., include_length = TRUE)
    ark_variable_kind(x, ...)
    ark_variable_has_children(x, ...)
    ark_variable_get_children(x, ...)
    ark_variable_get_child_at(x, ..., index, name)

It would make sense instead of a simplistic _repr_positron_ method, instead to have a duck-typed variables inspection API for third party libraries, where they can provide implementations of all of these methods. I don't think there is urgency, but at some point we can draft and review the abstract API to adopt in the Python kernel.

@wesm wesm added the area: variables Issues related to Variables category. label Dec 3, 2024
wesm added a commit that referenced this issue Dec 5, 2024
… is True (#5625)

Addresses #5499 by adding a custom inspector for Ibis expressions. This
is very basic, and per #5573 should perhaps live eventually in Ibis
itself.

Ibis is a bit unusual in that its interactive mode causes computation to
be executed when running the `__repr__` method, for nice interactivity
in the console and in Jupyter notebooks. So here we avoid running the
`__repr__` method so we don't accidentally fire off a BigQuery,
Snowflake, or other query which might have unwanted costs or side
effects.

There is a unit test -- Ibis with DuckDB is a minor dependency to pull
in relative to the rest of our test dependencies so I do not think this
is too onerous.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: variables Issues related to Variables category. lang: python
Projects
None yet
Development

No branches or pull requests

2 participants