Skip to content

Commit

Permalink
IPython support.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed May 19, 2016
1 parent 76f5c6a commit 3594efc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
9 changes: 5 additions & 4 deletions leather/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from leather.shapes import Bars, Columns, Dots, Lines
import leather.svg as svg
from leather import theme
from leather.utils import X, Y, DIMENSIONS, Box
from leather.utils import X, Y, DIMENSIONS, Box, IPythonSVG


class Chart(object):
Expand Down Expand Up @@ -243,8 +243,9 @@ def to_svg(self, path=None, width=None, height=None):
as though they were pixels.
:param path:
Filepath or file-like object to write to. If not specified then
the SVG will be returned as a string.
Filepath or file-like object to write to. If omitted then the SVG
will be returned as a string. If running within IPython, then this
will return a SVG object to be displayed.
:param width:
The output width, in SVG user units.
:param height:
Expand Down Expand Up @@ -285,4 +286,4 @@ def to_svg(self, path=None, width=None, height=None):
if close and f is not None:
f.close()
else:
return svg_text
return IPythonSVG(svg_text)
3 changes: 2 additions & 1 deletion leather/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import leather.svg as svg
from leather import theme
from leather.utils import IPythonSVG


class Grid(object):
Expand Down Expand Up @@ -83,4 +84,4 @@ def to_svg(self, path=None, width=None, height=None):
if close and f is not None:
f.close()
else:
return svg_text
return IPythonSVG(svg_text)
7 changes: 7 additions & 0 deletions leather/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

from collections import namedtuple

try:
__IPYTHON__
from IPython.display import SVG as IPythonSVG
except (NameError, ImportError):
IPythonSVG = lambda x: x


#: X data dimension index
X = 0

Expand Down
2 changes: 2 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import unittest

from lxml import etree
import six


class XMLTest(unittest.TestCase):
Expand All @@ -17,6 +18,7 @@ def render_chart(self, chart):
Verify the column names in the given table match what is expected.
"""
text = chart.to_svg()
print(dir(text))
text = text.replace(' xmlns="http://www.w3.org/2000/svg"', '')

return etree.fromstring(text)
Expand Down

0 comments on commit 3594efc

Please sign in to comment.