Skip to content

MEP27 Decouple pyplot from backends (refactoring Gcf out of backend code) #4143

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

Closed
wants to merge 55 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
24caf2b
Refactor pass 1. Refactoring Gcf out of specific backend (backend_gt…
OceanWolf Feb 21, 2015
2b05d38
Refactor Pass 2. Refactored Gcf out of all backend code.
OceanWolf Feb 22, 2015
f4fc354
Quick fix to figure for safe unpickling.
OceanWolf Feb 25, 2015
0b31e3a
GTK3Agg
OceanWolf Feb 26, 2015
6fb452e
Refactored making `FigureManager` a *figure* manager, plus added miss…
OceanWolf Feb 27, 2015
0a868a2
keyword
OceanWolf Feb 27, 2015
61ba2b4
Make add_element more general, and make sure the code complies with it.
OceanWolf Feb 27, 2015
f0eb84c
Better destroy order.
OceanWolf Feb 27, 2015
8fe9cd7
GTK simplifications
OceanWolf Feb 28, 2015
494e5f1
Added doc and cleaned backend_managers, don't want our new file dirty.
OceanWolf Mar 3, 2015
ed16178
Improve layout!
OceanWolf Apr 6, 2015
21b8f58
Move knowledge of the backend to the manager.
OceanWolf Apr 7, 2015
cf42e3b
Incorporate MEP22 into MEP27
OceanWolf Apr 12, 2015
f027b16
Improved new toolbar and updated tool_manager example accoridingly.
OceanWolf Apr 12, 2015
24b7b73
fullscreen
OceanWolf Apr 13, 2015
bc99129
MEP update
OceanWolf Apr 14, 2015
7f7f05e
Finish MEP22 conversion
OceanWolf Apr 14, 2015
713abcb
rename window method
OceanWolf Apr 17, 2015
80adaaf
Add backend anme to widgets
OceanWolf Apr 17, 2015
4e5f69d
Handle FigureManager destroy internaly without pyplot.
OceanWolf Jun 4, 2015
160ef57
Make functionality more consistant for embedded applications
OceanWolf Jun 7, 2015
b6d6acc
Backend getter method for FigureManager
OceanWolf Jun 13, 2015
ecd5038
Improve example after new method
OceanWolf Jun 13, 2015
f8e83fe
Clean up the code a bit
OceanWolf Jun 13, 2015
c53b79a
Remove old code from backend_managers
OceanWolf Jun 15, 2015
34c6b12
Cleanup
OceanWolf Jun 22, 2015
8a4268a
Explicity get set manager as None if appropiate.
OceanWolf Jun 22, 2015
44df199
figure attribute and canvas property
fariza Jun 22, 2015
860a8ed
Fix FigureCanvasBase
OceanWolf Jun 23, 2015
c44e744
super
OceanWolf Jun 25, 2015
2fe9215
figure setter
fariza Jun 25, 2015
3b434ef
Improve MEP22 Tool Searching Structure
OceanWolf Jun 25, 2015
490629f
adding example file
fariza Jun 25, 2015
8eb987b
super dooper
OceanWolf Jun 26, 2015
224a4f3
Revert old example and fix new one.
OceanWolf Jun 26, 2015
cdbd51b
Improve MEP22 tool-searching method.
OceanWolf Jun 27, 2015
50e3719
MEP22 Save Figure Tool
OceanWolf Jun 27, 2015
85be519
pep8
OceanWolf Jun 27, 2015
7edaf5a
Make ToolConfigureSubplots a generic tool
OceanWolf Jun 28, 2015
8e6e252
Improve flow handling and make it a lot more generic
OceanWolf Jun 28, 2015
72575cb
Missing resize method
OceanWolf Jun 28, 2015
4a78246
Convert to new structure for finding tools
OceanWolf Jun 28, 2015
e300707
doc
OceanWolf Jun 29, 2015
ee76451
Add ExpandableBase
OceanWolf Jun 29, 2015
6f0c7ab
Template Backend plus fix FigManager for non-GUI backends and add gen…
OceanWolf Jun 29, 2015
ae9bf5b
rcParam and Travis
OceanWolf Jul 19, 2015
fb004e0
test
OceanWolf Jul 20, 2015
1d2095b
test always MEP27
OceanWolf Jul 20, 2015
24e43b3
Fix FigureManager to allow pyplot to work for non GUI backends
OceanWolf Jul 27, 2015
208c3be
Fix Gcf.show_all()
OceanWolf Jul 27, 2015
a44ebd9
doc
OceanWolf Jul 27, 2015
f8f9cf2
pep8
OceanWolf Jul 27, 2015
0e09a54
remove show_popup
OceanWolf Jul 27, 2015
a38b6d7
AttributeError
OceanWolf Sep 22, 2015
64f0c61
Fixes for MEP27
OceanWolf Aug 3, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MEP22 Save Figure Tool
  • Loading branch information
OceanWolf committed Aug 3, 2016
commit 50e3719892d0dfb32238f7c57f85c7dab2938371
39 changes: 39 additions & 0 deletions lib/matplotlib/backend_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import time
import warnings

import os


class Cursors(object):
"""Simple namespace for cursor reference"""
Expand Down Expand Up @@ -679,6 +681,43 @@ class SaveFigureBase(ToolBase):
default_keymap = rcParams['keymap.save']


class ToolSaveFigure(ToolBase):
Copy link
Member

Choose a reason for hiding this comment

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

Adding this one, SaveFigureBase should be removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree, I added it to the Todo list at the top of the PR under TkAgg. As the code stands at the moment, TkAgg still works in this PR for rcParams['toolbar'] == 'toolmanager'.

"""Saves the figure"""

description = 'Save the figure'
image = 'filesave.png'
default_keymap = rcParams['keymap.save']

def get_filechooser(self):
fc = self.figure.canvas.backend.FileChooserDialog(
Copy link
Member

Choose a reason for hiding this comment

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

the backend attribute is in figuremanager not in canvas

Copy link
Member Author

Choose a reason for hiding this comment

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

I added it to the canvas in the commit before this... the reason for this, we will always have a canvas we won't always have a FigureManager, thus it simplifies the code if canvas knows the backend it belongs to as well.

title='Save the figure',
parent=self.figure.canvas.manager.window,
path=os.path.expanduser(rcParams.get('savefig.directory', '')),
filetypes=self.figure.canvas.get_supported_filetypes(),
default_filetype=self.figure.canvas.get_default_filetype())
fc.set_current_name(self.figure.canvas.get_default_filename())
return fc

def trigger(self, *args, **kwargs):
chooser = self.get_filechooser()
fname, format_ = chooser.get_filename_from_user()
chooser.destroy()
if fname:
startpath = os.path.expanduser(
rcParams.get('savefig.directory', ''))
if startpath == '':
# explicitly missing key or empty str signals to use cwd
rcParams['savefig.directory'] = startpath
else:
# save dir for next time
rcParams['savefig.directory'] = os.path.dirname(
six.text_type(fname))
try:
self.figure.canvas.print_figure(fname, format=format_)
except Exception as e:
error_msg_gtk(str(e), parent=self)


class ZoomPanBase(ToolToggleBase):
"""Base class for `ToolZoom` and `ToolPan`"""
def __init__(self, *args):
Expand Down
33 changes: 0 additions & 33 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,38 +923,6 @@ def set_message(self, s):
self.push(self._context, s)


class SaveFigureGTK3(backend_tools.SaveFigureBase):

def get_filechooser(self):
fc = FileChooserDialog(
title='Save the figure',
parent=self.figure.canvas.manager.window,
path=os.path.expanduser(rcParams.get('savefig.directory', '')),
filetypes=self.figure.canvas.get_supported_filetypes(),
default_filetype=self.figure.canvas.get_default_filetype())
fc.set_current_name(self.figure.canvas.get_default_filename())
return fc

def trigger(self, *args, **kwargs):
chooser = self.get_filechooser()
fname, format_ = chooser.get_filename_from_user()
chooser.destroy()
if fname:
startpath = os.path.expanduser(
rcParams.get('savefig.directory', ''))
if startpath == '':
# explicitly missing key or empty str signals to use cwd
rcParams['savefig.directory'] = startpath
else:
# save dir for next time
rcParams['savefig.directory'] = os.path.dirname(
six.text_type(fname))
try:
self.figure.canvas.print_figure(fname, format=format_)
except Exception as e:
error_msg_gtk(str(e), parent=self)


class SetCursorGTK3(backend_tools.SetCursorBase):
Copy link
Member

Choose a reason for hiding this comment

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

Why did this + ConfigureSubplotsGTK3 get removed?

Copy link
Member

Choose a reason for hiding this comment

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

There is a new generic Tool that does exactly that without having to implement it for each backend.

Copy link
Member

Choose a reason for hiding this comment

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

🐑 Sorry for asking silly questions.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hehe, no worries, this actually triggered this MEP, looking through the new ConfigureSubplotsGTK3 tool and seeing how it duplicated much of the FigureManagerGTK3 code, I started off just hacking that FigMan code and getting more and more frustrated by the ugliness of the hacking and how the specific backends were handling high-ish level matplotlib code, and saw that we needed to split them into two, a backend specific generic window class, and a single backend-independent FigureManager class.

The aim of this MEP became removing all matplotlib specific code from the backends, allowing the backends to become flexible and reusable for any matplotlib task.

def set_cursor(self, cursor):
self.figure.canvas.get_property("window").set_cursor(cursord[cursor])
Expand Down Expand Up @@ -1039,7 +1007,6 @@ def error_msg_gtk(msg, parent=None):
dialog.destroy()


backend_tools.ToolSaveFigure = SaveFigureGTK3
backend_tools.ToolConfigureSubplots = ConfigureSubplotsGTK3
backend_tools.ToolSetCursor = SetCursorGTK3
backend_tools.ToolRubberband = RubberbandGTK3
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/backends/backend_gtk3agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def new_figure_manager_given_figure(num, figure):
Window = backend_gtk3.WindowGTK3
Toolbar = backend_gtk3.ToolbarGTK3
Statusbar = backend_gtk3.StatusbarGTK3
FileChooserDialog = backend_gtk3.FileChooserDialog

Toolbar2 = backend_gtk3.NavigationToolbar2GTK3
MainLoop = backend_gtk3.MainLoopGTK3
show = backend_gtk3.show
2 changes: 2 additions & 0 deletions lib/matplotlib/backends/backend_gtk3cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def new_figure_manager_given_figure(num, figure):
Window = backend_gtk3.WindowGTK3
Toolbar = backend_gtk3.ToolbarGTK3
Statusbar = backend_gtk3.StatusbarGTK3
FileChooserDialog = backend_gtk3.FileChooserDialog

Toolbar2 = backend_gtk3.NavigationToolbar2GTK3
MainLoop = backend_gtk3.MainLoopGTK3
show = backend_gtk3.show