Skip to content

Use cssdecl package for resolving CSS #16170

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 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
1 change: 1 addition & 0 deletions ci/requirements-2.7_WIN.pip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cssdecl
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add to requirements-2.7 as well

1 change: 1 addition & 0 deletions ci/requirements-3.5_DOC.pip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cssdecl
Copy link
Contributor

Choose a reason for hiding this comment

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

add to ci/requirements-3.5.pip & requirements-2.7.pip as well (what you added here barely tests anything and only on windows).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh. Strange. I think I must have misperformed a patch commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd presume 3.6, 3.5_DOC and 2.7_WIN sufficient, though.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes that is fine. note that you might want to create a conda-forge package for this at some point (otherwise you can't include it directly in a dependecy tree as they don't support pip install directly).

1 change: 1 addition & 0 deletions ci/requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ python-dateutil
pytz
openpyxl
xlsxwriter
cssdecl
Copy link
Contributor

Choose a reason for hiding this comment

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

this is for developement and not necessary to have this, pls remove.

xlrd
xlwt
html5lib
Expand Down
1 change: 1 addition & 0 deletions doc/source/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ Optional Dependencies
* `openpyxl <http://packages.python.org/openpyxl/>`__: openpyxl version 1.6.1
or higher (but lower than 2.0.0), or version 2.2 or higher, for writing .xlsx files (xlrd >= 0.9.0)
* `XlsxWriter <https://pypi.python.org/pypi/XlsxWriter>`__: Alternative Excel writer
* `cssdecl <https://pypi.python.org/pypi/cssdecl>`__: along with one of openpyxl or XlsxWriter for exporting styled DataFrames
Copy link
Contributor

Choose a reason for hiding this comment

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

if you have a min version requirement we can put it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've decided I can probably keep the interface backwards compatible. There may, in the future, be min versions for certain features, e.g. gradient fills used for bar charts, but there will also be a min version of openpyxl for that (and I don't think xlsxwriter supports)

Copy link
Contributor Author

@jnothman jnothman May 8, 2017

Choose a reason for hiding this comment

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

xlsxwriter support mentioned here depends largely on #16149 being merged


* `Jinja2 <http://jinja.pocoo.org/>`__: Template engine for conditional HTML formatting.
* `s3fs <http://s3fs.readthedocs.io/>`__: necessary for Amazon S3 access (s3fs >= 0.0.7).
Expand Down
248 changes: 0 additions & 248 deletions pandas/io/formats/css.py

This file was deleted.

13 changes: 9 additions & 4 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import numpy as np

from pandas.compat import reduce, string_types
from pandas.io.formats.css import CSSResolver, CSSWarning
from pandas.io.formats.printing import pprint_thing
from pandas.core.dtypes.common import is_float
import pandas._libs.lib as lib
Expand Down Expand Up @@ -61,14 +60,18 @@ class CSSToExcelConverter(object):
# without monkey-patching.

def __init__(self, inherited=None):
try:
from cssdecl import CSS22Resolver
except ImportError:
raise ImportError("cssdecl is not installed and is requied to "
"convert styles to Excel")
self.compute_css = CSS22Resolver().resolve_string
if inherited is not None:
inherited = self.compute_css(inherited,
self.compute_css.INITIAL_STYLE)

self.inherited = inherited

compute_css = CSSResolver()

def __call__(self, declarations_str):
"""Convert CSS declarations to ExcelWriter style

Expand Down Expand Up @@ -302,6 +305,7 @@ def color_to_excel(self, val):
try:
return self.NAMED_COLORS[val]
except KeyError:
from cssdecl import CSSWarning
warnings.warn('Unhandled colour format: %r' % val, CSSWarning)


Expand Down Expand Up @@ -333,7 +337,8 @@ class ExcelFormatter(object):
A `'-'` sign will be added in front of -inf.
style_converter : callable, optional
This translates Styler styles (CSS) into ExcelWriter styles.
Defaults to ``CSSToExcelConverter()``.
Defaults to ``CSSToExcelConverter()`` which requires the ``cssdecl``
package installed.
It should have signature css_declarations string -> excel style.
This is only called for body cells.
"""
Expand Down
Loading