8000 added support for external backends · matplotlib/matplotlib@032cc0c · GitHub
[go: up one dir, main page]

Skip to content

Commit 032cc0c

Browse files
committed
added support for external backends
svn path=/trunk/matplotlib/; revision=5754
1 parent 77e497a commit 032cc0c

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2008-07-12 Added support for external backends with the
2+
"module://my_backend" syntax - JDH
3+
14
2008-07-11 Fix memory leak related to shared axes. Grouper should
25
store weak references. - MGD
36

lib/matplotlib/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,11 @@ def use(arg, warn=True):
801801
if warn: warnings.warn(_use_error_msg)
802802
return
803803
arg = arg.lower()
804-
be_parts = arg.split('.')
805-
name = validate_backend(be_parts[0])
804+
if arg.startswith('module://'):
805+
name = arg
806+
else:
807+
be_parts = arg.split('.')
808+
name = validate_backend(be_parts[0])
806809
rcParams['backend'] = name
807810
if name == 'cairo' and len(be_parts) > 1:
808811
rcParams['cairo.format'] = validate_cairo_format(be_parts[1])

lib/matplotlib/backends/__init__.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11

22
import matplotlib
3-from matplotlib.rcsetup import interactive_bk
4-
from matplotlib.rcsetup import non_interactive_bk
5-
from matplotlib.rcsetup import all_backends
6-
from matplotlib.rcsetup import validate_backend
3+
74

85
__all__ = ['backend','show','draw_if_interactive',
96
'new_figure_manager', 'backend_version']
@@ -14,27 +11,23 @@ def pylab_setup():
1411
'return new_figure_manager, draw_if_interactive and show for pylab'
1512
# Import the requested backend into a generic module object
1613

17-
backend_name = 'backend_'+backend
18-
backend_name = backend_name.lower() # until we banish mixed case
19-
backend_mod = __import__('matplotlib.backends.'+backend_name,
14+
if backend.startswith('module://'):
15+
backend_name = backend[9:]
16+
else:
17+
backend_name = 'backend_'+backend
18+
backend_name = backend_name.lower() # until we banish mixed case
19+
backend_name = 'matplotlib.backends.%s'%backend_name.lower()
20+
backend_mod = __import__(backend_name,
2021
globals(),locals(),[backend_name])
2122

2223
# Things we pull in from all backends
2324
new_figure_manager = backend_mod.new_figure_manager
2425

25-
if hasattr(backend_mod,'backend_version'):
26-
backend_version = getattr(backend_mod,'backend_version')
27-
else: backend_version = 'unknown'
28-
29-
3026

31-
# Now define the public API according to the kind of backend in use
32-
if backend in interactive_bk:
33-
show = backend_mod.show
34-
draw_if_interactive = backend_mod.draw_if_interactive
35-
else: # non-interactive backends
36-
def draw_if_interactive(): pass
37-
def show(): pass
27+
def do_nothing(*args, **kwargs): pass
28+
backend_version = getattr(backend_mod,'backend_version', 'unknown')
29+
show = getattr(backend_mod, 'show', do_nothing)
30+
draw_if_interactive = getattr(backend_mod, 'draw_if_interactive', do_nothing)
3831

3932
# Additional imports which only happen for certain backends. This section
4033
# should probably disappear once all backends are uniform.

lib/matplotlib/backends/backend_template.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,25 @@
1717
with 'xxx'. Then implement the class methods and functions below, and
1818
add 'xxx' to the switchyard in matplotlib/backends/__init__.py and
1919
'xxx' to the backends list in the validate_backend methon in
20-
matplotlib/__init__.py and you're off. You can use your backend with
20+
matplotlib/__init__.py and you're off. You can use your backend with::
2121
2222
import matplotlib
2323
matplotlib.use('xxx')
2424
from pylab import *
2525
plot([1,2,3])
2626
show()
2727
28+
matplotlib also supports external backends, so you can place you can
29+
use any module in your PYTHONPATH with the syntax::
30+
31+
import matplotlib
32+
matplotlib.use('module://my_backend')
33+
34+
where my_backend.py is your module name. Thus syntax is also
35+
recognized in the rc file and in the -d argument in pylab, eg::
36+
37+
python simple_plot.py -dmodule://my_backend
38+
2839
The files that are most relevant to backend_writers are
2940
3041
matplotlib/backends/backend_your_backend.py

matplotlibrc.template

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
# such as 0.75 - a legal html color name, eg red, blue, darkslategray
2020

2121
#### CONFIGURATION BEGINS HERE
22+
2223
# the default backend; one of GTK GTKAgg GTKCairo CocoaAgg FltkAgg
23-
# QtAgg Qt4Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG Template
24+
# QtAgg Qt4Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG Template You
25+
# can also deploy your own backend outside of matplotlib by referring
26+
# to the module name (which must be in the PYTHONPATH) as
27+
# 'module://my_backend'
2428
backend : %(backend)s
2529
numerix : %(numerix)s # numpy, Numeric or numarray
2630
#maskedarray : False # True to use external maskedarray module

0 commit comments

Comments
 (0)
0