forked from has2k1/gnuplot_kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgnuplot_magic.py
More file actions
149 lines (120 loc) · 4.01 KB
/
gnuplot_magic.py
File metadata and controls
149 lines (120 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from IPython.core.magic import (register_line_magic,
register_cell_magic)
from metakernel import Magic
class GnuplotMagic(Magic):
def __init__(self, kernel):
"""
GnuplotMagic
Parameters
----------
kernel : GnuplotKernel
Kernel used to execute the magic code
"""
super(GnuplotMagic, self).__init__(kernel)
def eval(self, code):
"""
Evaluate code useing the gnuplot kernel
"""
return self.kernel.do_execute_direct(code)
def print(self, text):
"""
Print text if it is not empty
"""
if text and text.output.strip():
self.kernel.Display(text)
def line_gnuplot(self, *args):
"""
%gnuplot CODE - evaluate code as gnuplot
This line magic will evaluate the CODE to setup or
unset inline plots. This magic should be used instead
of the plot magic
Examples:
%gnuplot inline pngcairo enhanced transparent size 560,420
%gnuplot inline svg enhanced size 560,420 fixed
%gnuplot inline jpeg enhanced nointerlace
%gnuplot qt
"""
backend, terminal, termspec = _parse_args(args)
terminal = terminal or 'pngcairo'
inline_terminals = {'pngcairo': 'png',
'png': 'png',
'jpeg': 'jpg',
'svg': 'svg'}
format = inline_terminals.get(terminal, 'png')
if backend == 'inline':
if terminal not in inline_terminals:
msg = ("For inline plots, the terminal must be "
"one of pngcairo, jpeg, svg or png")
raise ValueError(msg)
self.kernel.plot_settings['backend'] = backend
self.kernel.plot_settings['termspec'] = termspec
self.kernel.plot_settings['format'] = format
self.kernel.handle_plot_settings()
def cell_gnuplot(self):
"""
%%gnuplot - Run gnuplot commands
Example:
%%gnuplot
unset border
unset xtics
g(x) = cos(2*x)/sin(x)
Note: this is a persistent connection to a gnuplot shell.
The working directory is synchronized to that of the notebook
before and after each call.
"""
result = self.eval(self.code)
self.print(result)
def register_magics(kernel):
"""
Make the gnuplot magic available for the GnuplotKernel
"""
kernel.register_magics(GnuplotMagic)
def register_ipython_magics():
"""
Register magics for the running kernel
The magics themselve use a special kernel that
understands gnuplot.
"""
from ..kernel import GnuplotKernel
# Kernel to run the both the line magic (%gnuplot)
# and cell magic (%%gnuplot) statements
# See: GnuplotKernel for a full notebook kernel
kernel = GnuplotKernel()
magic = GnuplotMagic(kernel)
# This kernel that is used by the magics is
# not the main kernel and it may not have access
# to some functionality. This connects it to the
# main kernel.
from IPython import get_ipython
ip = get_ipython()
kernel.makeSubkernel(ip.parent)
# Make magics callable:
kernel.line_magics['gnuplot'] = magic
kernel.cell_magics['gnuplot'] = magic
@register_line_magic
def gnuplot(line):
magic.line_gnuplot(line)
del gnuplot
@register_cell_magic
def gnuplot(line, cell):
magic.code = cell
magic.cell_gnuplot()
def _parse_args(args):
"""
Process the gnuplot line magic arguments
"""
if len(args) > 1:
raise TypeError()
sargs = args[0].split()
backend = sargs[0]
if backend == 'inline':
try:
termspec = ' '.join(sargs[1:])
terminal = sargs[1]
except IndexError:
termspec = None
terminal = None
else:
termspec = args[0]
terminal = sargs[0]
return backend, terminal, termspec