-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathheat.py
67 lines (51 loc) · 1.82 KB
/
heat.py
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
"""
heat
~~~~
Module implementing IPython cell magic to use pyheat.
Usage of heat:
To have heatmap displayed within ipython notebook.
>>> %%heat
To have heatmap exported as a image file.
>>> %%heat -o heat.png
>>> %%heat --out heat.png
:copyright: (c) 2017 by Vishwas B Sharma.
:license: MIT, see LICENSE for more details.
"""
__title__ = 'heat'
__version__ = '0.0.2'
__author__ = 'Vishwas B Sharma'
__author_email__ = '[email protected]'
__license__ = 'MIT'
__copyright__ = 'Copyright 2017 Vishwas B Sharma'
import os
from tempfile import mkstemp
from IPython.core import magic_arguments
from IPython.core.magic import Magics, cell_magic, magics_class
from pyheat import PyHeat
@magics_class
class PyHeatMagic(Magics):
"""Class with IPython magic commands to effectively use py-heat profiling
within IPython."""
@magic_arguments.magic_arguments()
@magic_arguments.argument('-o', '--out', default=None, help='Save the heatmap to given file')
@cell_magic
def heat(self, line, cell):
"""Method to profile the python code in the ipython cell and display it
as a heatmap using py-heat package.
:param line: Line value for the ipython line this magic is called from.
:param cell: Cell value for the ipython cell this magic is called from.
"""
args = magic_arguments.parse_argstring(self.heat, line)
filename = args.out
if filename is not None:
filename = os.path.expanduser(args.out)
_, tmp_file = mkstemp()
with open(tmp_file, 'wb') as f:
f.write(cell.encode())
pyheat = PyHeat(tmp_file)
pyheat.create_heatmap()
pyheat.show_heatmap(output_file=filename)
pyheat.close_heatmap()
os.remove(tmp_file)
def load_ipython_extension(ipython):
ipython.register_magics(PyHeatMagic)