Skip to content

Move show() to somewhere naturally inheritable / document what pyplot expects from a backend. #23101

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

Merged
merged 1 commit into from
Nov 11, 2022

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented May 22, 2022

PR Summary

Goes on top of #23090 (see #23090 (comment)).

Move show() to somewhere naturally inheritable.

It's actually not clear whether to move it to FigureCanvas or
FigureManager. FigureCanvas already has start_event_loop (cf.
start_main_loop), but pyplot_show/start_main_loop is more a
global/pyplot-only concept, so perhaps it belongs to FigureManager
instead? OTOH, being on canvas_class makes it easier for switch_backend
to access it (it doesn't need to go through
canvas_class.manager_class.pyplot_show, which is relevant considering
that some designs of inheritable backends didn't even have a
manager_class attribute).

Document what pyplot expects from a backend.

Finally, a document stating what's needed in a backend!
(only the pyplot interaction part, not the rendering part.)
Edit: now moved to #24218.

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

@anntzer anntzer changed the title Derive new_figure_manager from FigureCanvas.new_manager. Move show() to somewhere naturally inheritable / document what pyplot expects from a backend. May 23, 2022
@anntzer anntzer force-pushed the inheritable-backend-show branch from ee82d35 to da12a3e Compare May 23, 2022 06:21
@anntzer anntzer force-pushed the inheritable-backend-show branch from da12a3e to 6670d0d Compare May 23, 2022 06:23
@anntzer anntzer added this to the v3.6.0 milestone May 27, 2022
@jklymak
Copy link
Member

jklymak commented Jun 30, 2022

Failures looks real, and has picked up rebase issues... This looks like a reasonable simplification, thought I'm not an expert

@jklymak jklymak marked this pull request as draft June 30, 2022 10:54
@jklymak jklymak marked this pull request as draft June 30, 2022 10:54
@jklymak jklymak marked this pull request as draft June 30, 2022 10:54
@anntzer anntzer force-pushed the inheritable-backend-show branch 2 times, most recently from 1aebeb8 to 86e5d5b Compare July 3, 2022 18:17
@anntzer
Copy link
Contributor Author

anntzer commented Jul 3, 2022

Thanks for pointing this out, this should be hopefully fixed now. I would also like to request the reviewer's thoughts (perhaps @timhoffm?) as to whether pyplot_show should be a canvas or a manager method (as explained in the commit message/top post).

@anntzer anntzer force-pushed the inheritable-backend-show branch from 86e5d5b to 340d579 Compare July 5, 2022 06:14
@anntzer anntzer force-pushed the inheritable-backend-show branch 2 times, most recently from 5118228 to e181216 Compare July 5, 2022 09:05
@anntzer anntzer marked this pull request as ready for review July 5, 2022 11:05
@anntzer anntzer added the status: needs comment/discussion needs consensus on next step label Jul 5, 2022
@anntzer anntzer force-pushed the inheritable-backend-show branch 2 times, most recently from 4a99249 to 7e5f7db Compare July 20, 2022 08:15
@anntzer
Copy link
Contributor Author

anntzer commented Jul 21, 2022

Discussed on the call: I'll move pyplot_show to the manager class instead, keeping that name; I'll also document explicitly that's it's only public for the purpose of being overridden by third-party backends, not for being called directly by end users.

@anntzer anntzer removed the status: needs comment/discussion needs consensus on next step label Jul 21, 2022
@anntzer anntzer force-pushed the inheritable-backend-show branch from 7e5f7db to 4c299b6 Compare July 21, 2022 21:27
@anntzer
Copy link
Contributor Author

anntzer commented Jul 21, 2022

The discussed modifications have been implemented.

@QuLogic QuLogic added this to the v3.7.0 milestone Aug 24, 2022
@anntzer anntzer force-pushed the inheritable-backend-show branch 3 times, most recently from 0986e93 to 4500f83 Compare October 17, 2022 08:29
@anntzer anntzer force-pushed the inheritable-backend-show branch from 4500f83 to d30b372 Compare October 18, 2022 09:32
@anntzer anntzer force-pushed the inheritable-backend-show branch from d30b372 to d6ebf93 Compare October 18, 2022 10:21
@anntzer
Copy link
Contributor Author

anntzer commented Oct 19, 2022

I have addressed your comments (thanks for the review) but moved the docs part of the PR to #24218, to make the pure implementation part easier to merge.

manager.show() # Emits a warning for non-interactive backend.
except NonGuiException as exc:
_api.warn_external(str(exc))
if block is None:
Copy link
Member

Choose a reason for hiding this comment

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

Do we also want to check if pypplot is already imported?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure.

@anntzer anntzer force-pushed the inheritable-backend-show branch from 5f1d623 to 2fca527 Compare October 21, 2022 08:49
@anntzer anntzer force-pushed the inheritable-backend-show branch 2 times, most recently from 30dd65f to e32ae61 Compare October 21, 2022 09:41
Comment on lines 3592 to +3602
# Hack: Are we in IPython's %pylab mode? In pylab mode, IPython
# (>= 0.10) tacks a _needmain attribute onto pyplot.show (always
# set to False).
from matplotlib import pyplot
ipython_pylab = hasattr(pyplot.show, "_needmain")
ipython_pylab = hasattr(
getattr(sys.modules.get("pyplot"), "show", None), "_needmain")
Copy link
Member

Choose a reason for hiding this comment

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

Move this to a helper function in cbook?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The duplication is only temporary, because the version in _Backend.show will go away as I plan to get rid of the _Backend class, which was an implementation detail anyways. (I'm not doing it immediately because the ipympl backend has started relying on it, but I plan to help them to get rid of that use, either going back to the old-style fully manual API if they want a long backcompat, or going to the new class-based API.)

It's actually not clear whether to move it to FigureCanvas or
FigureManager.  FigureCanvas already has start_event_loop (cf.
start_main_loop), but pyplot_show/start_main_loop is more a
global/pyplot-only concept, so perhaps it belongs to FigureManager
instead?  OTOH, being on canvas_class makes it easier for switch_backend
to access it (it doesn't need to go through
`canvas_class.manager_class.pyplot_show`, which is relevant considering
that some designs of inheritable backends didn't even have a
manager_class attribute).
@anntzer anntzer force-pushed the inheritable-backend-show branch from e32ae61 to 86f26a0 Compare November 6, 2022 22:42
@timhoffm timhoffm merged commit cd185ab into matplotlib:main Nov 11, 2022
@anntzer anntzer deleted the inheritable-backend-show branch November 12, 2022 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants