Skip to content

WIP: df rendering using templates + conditional formatting for HTML #5763

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 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 pandas/io/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
from pandas.io.stata import read_stata
from pandas.io.pickle import read_pickle, to_pickle
from pandas.io.packers import read_msgpack, to_msgpack
from pandas.io.templating import HTMLStyler
114 changes: 114 additions & 0 deletions pandas/io/templating/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
from abc import abstractmethod
import os
import uuid


ROW_HEADING_CLASS = "pandas_row_heading"
COL_HEADING_CLASS = "pandas_col_heading"
DATA_CLASS = "pandas_data"
DATA_CELL_TYPE = "data"
ROW_CLASS = "pandas_row"
COLUMN_CLASS = "pandas_col"
HEADING_CELL_TYPE = "heading"
BLANK_CLASS = "pandas_blank"
BLANK_VALUE = ""
LEVEL_CLASS = "pandas_level"

TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"templates")

# TODO, cleaner handling of cell_context (extra_contaxt
class Styler(object):

@abstractmethod
def render(self, f):
pass

def __init__(self, df, template, engine_instance=None,*args, **kwds):
super(Styler, self).__init__(*args, **kwds)
self.df = df
self.template = template
self.style = []
self.cell_context = {}

from pandas.io.templating.engines import Jinja2Engine
self.engine_instance = engine_instance or Jinja2Engine()
self.engine_instance.load(self.template)

def std_context(self, *args, **kwds):
df = self.df

n_rlvls = df.index.nlevels
n_clvls = df.columns.nlevels
rlabels = df.index.tolist()
clabels = df.columns.tolist()
if n_rlvls == 1:
rlabels = [[x] for x in rlabels]
if n_clvls == 1:
clabels = [[x] for x in clabels]
clabels = zip(*clabels)
head = []
for r in range(n_clvls):
row_es = [{"type": HEADING_CELL_TYPE, "value": BLANK_VALUE,
"class": " ".join([BLANK_CLASS])}] * n_rlvls
for c in range(len(clabels[0])):
cs = [COL_HEADING_CLASS, "%s%s" % (LEVEL_CLASS, r),
"%s%s" % (COLUMN_CLASS, c)]
cs.extend(
self.cell_context.get("col_headings", {}).get(r, {}).get(c,
[]))
row_es.append(
{"type": HEADING_CELL_TYPE, "value": clabels[r][c],
"class": " ".join(cs)})
head.append(row_es)
body = []
for r in range(len(df)):
cs = [ROW_HEADING_CLASS, "%s%s" % (LEVEL_CLASS, c),
"%s%s" % (ROW_CLASS, r)]
cs.extend(
self.cell_context.get("row_headings", {}).get(r, {}).get(c, []))
row_es = [
{"type": HEADING_CELL_TYPE, "value": rlabels[r][c],
"class": " ".join(cs)}
for c in range(len(rlabels[r]))]
for c in range(len(df.columns)):
cs = [DATA_CLASS, "%s%s" % (ROW_CLASS, r),
"%s%s" % (COLUMN_CLASS, c)]
cs.extend(
self.cell_context.get("data", {}).get(r, {}).get(c, []))
row_es.append(
{"type": DATA_CELL_TYPE, "value": df.iloc[r][c],
"class": " ".join(cs)})
body.append(row_es)

# uuid required to isolate table styling from other tables
# on the page in ipnb
u = str(uuid.uuid1()).replace("-", "_")
return dict(head=head, body=body, uuid=u, style=self.style)

def render(self, f=None, **kwds):
encoding = kwds.pop('encoding', "utf8")
s = self.engine_instance.render(self.std_context())
if f:
with codecs.open(f, "wb", encoding) as f:
f.write(s)
else:
return s

class ITemplateEngine(object):
"""Interface for supporting multiple template engines

We'll support only a single engine, but help users help themselves.
"""


@abstractmethod
def render(self, f=None,**kwds):
pass

@abstractmethod
def load(self, *args, **kwds):
pass


from html import HTMLStyler
10 changes: 10 additions & 0 deletions pandas/io/templating/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function

import sys, os, re, json, codecs

# integrate some colorbrewer package
red_scale = ["#fff7ec", "#fee8c8", "#fdd49e",
"#fdbb84", "#fc8d59", "#ef6548",
"#d7301f", "#b30000", "#7f0000"]
19 changes: 19 additions & 0 deletions pandas/io/templating/engines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pandas.io.templating import ITemplateEngine


class Jinja2Engine(ITemplateEngine):
def __init__(self):
from jinja2 import Template

def load(self, s):
from jinja2 import Template

self.t = Template(s)

def render(self, ctx):
if self.t:
return self.t.render(**ctx)
else:
raise AssertionError("Template not initialized, cannot render")
64 changes: 64 additions & 0 deletions pandas/io/templating/html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import uuid
import os
import functools
from os.path import abspath, dirname

from pandas.io.templating import *


# TODO: caption

class HTMLStyler(Styler):
def clone(self):
"""We always return a modified copy when extra styling is added

`clone()` provides a copy to apply changes to
"""
import copy

c = HTMLStyler(self.df,
template=self.template,
engine_instance=self.engine_instance)
c.style = copy.deepcopy(self.style)
c.cell_context = copy.deepcopy(self.cell_context)
return c

def __init__(self, *args, **kwds):

if not kwds.get('template'):
tmpl_filename = os.path.join(TEMPLATE_DIR, "html")
with open(tmpl_filename) as f:
kwds['template'] = f.read()

super(HTMLStyler, self).__init__(*args, **kwds)

import html_helpers
import types

for n in dir(html_helpers):
cand = getattr(html_helpers, n)
if callable(cand):
def f(cand):
@functools.wraps(cand)
def f(self, *args, **kwds):
self_copy = self.clone()
cand(self_copy, *args, **kwds)
# self.style.extend(sd.style)
# TODO, need to merge-with
# self.cell_context = sd.cell_context
return self_copy

return f

setattr(self, n, types.MethodType(f(cand), self))

def _repr_html_(self):
return self.render()

# def __dir__(self):
# import html_helpers
# return ["render","to_context"] + dir(html_helpers)

# def __getattr__(self, key):
# import html_helpers
# return getattr(html_helpers,key)
138 changes: 138 additions & 0 deletions pandas/io/templating/html_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
from __future__ import print_function

import sys, os, re, json, codecs
import uuid

from pandas.io.templating import *

__all__ = ["zebra", "hlrow", "hlcol"]

from collections import namedtuple


def zebra(s, color1, color2):
style = [dict(selector="td.%s:nth-child(2n)" % DATA_CLASS,
props=[("background-color", color1)]),
dict(selector="td.%s:nth-child(2n+1)" % DATA_CLASS,
props=[("background-color", color2)])]
s.style.extend(style)


def hlcell(s, r, c, color="#aaa", with_headings=False):
selector = "td.%s%d.%s%d" % (ROW_CLASS, r, COLUMN_CLASS, c)
if not with_headings:
selector += ".%s" % DATA_CLASS
style = [dict(selector=selector,
props=[("background-color", color)])]
s.style.extend(style)


def hlcol(s, n, color="#aaa", with_headings=False):
selector = "td.%s%d" % (COLUMN_CLASS, n)
if not with_headings:
selector += ".%s" % DATA_CLASS
style = [dict(selector=selector,
props=[("background-color", color)])]
s.style.extend(style)


def hlrow(s, n, color="#ccc", with_headings=False):
selector = "td.%s%d" % (ROW_CLASS, n)
if not with_headings:
selector += ".%s" % DATA_CLASS
style = [dict(selector=selector,
props=[("background-color", color)])]
s.style.extend(style)


def round_corners(s, radius):
props_bl = [
("-moz-border-radius-bottomleft", "%dpx" % radius ),
("-webkit-border-bottom-left-radius", "%dpx" % radius ),
("border-bottom-left-radius", "%dpx" % radius )
]
props_br = [
("-moz-border-radius-bottomright", "%dpx" % radius ),
("-webkit-border-bottom-right-radius", "%dpx" % radius ),
("border-bottom-right-radius", "%dpx" % radius )
]
props_tl = [
("-moz-border-radius-topleft", "%dpx" % radius ),
("-webkit-border-top-left-radius", "%dpx" % radius ),
("border-top-left-radius", "%dpx" % radius )
]
props_tr = [
("-moz-border-radius-topright", "%dpx" % radius ),
("-webkit-border-top-right-radius", "%dpx" % radius ),
("border-top-right-radius", "%dpx" % radius )
]

style = [
dict(selector="",
props=[("border-collapse", "separate")]),
dict(selector="td",
props=[("border-width", "0px")]),
dict(selector="th",
props=[("border-width", "0px")]),
dict(selector="td",
props=[("border-left-width", "1px")]),
dict(selector="td",
props=[("border-top-width", "1px")]),

dict(selector="tbody tr:last-child th",
props=[("border-bottom-width", "1px")]),
dict(selector="tr:last-child td",
props=[("border-bottom-width", "1px")]),

dict(selector="tr td:last-child",
props=[("border-right-width", "1px")]),
dict(selector="tr th:last-child",
props=[("border-right-width", "1px")]),
dict(selector="th",
props=[("border-left-width", "1px")]),
dict(selector="th",
props=[("border-top-width", "1px")]),
dict(selector="th td:last-child",
props=[("border-right-width", "1px")]),


dict(selector="tr:last-child th:first-child",
props=props_bl),
dict(selector="tr:last-child td:last-child",
props=props_br),
dict(selector="tr:first-child th.%s0" % COLUMN_CLASS,
props=props_tl),
dict(selector="tr:first-child th.%s0:first-child" % ROW_CLASS,
props=props_tl),
dict(selector="tr:first-child th:last-child",
props=props_tr),
]

s.style.extend(style)

#
# def rank_heatmap(s, df, row=None, col=None):
# def color_class(cls, color):
# return [dict(selector="td.%s" % cls,
# props=[("background-color", color)])]
#
# def rank_col(n, ranking, u):
# data = {i: {n: ["%s-%s" % (u, ranking[i])]}
# for i in range(len(ranking))}
# return {"data": data}
#
#
# # u = "U" + str(uuid.uuid1()).replace("-", "_")
# # df = mkdf(9, 5, data_gen_f=lambda r, c: np.random.random())
#
# ranking = df.iloc[:, 1].argsort().tolist()
# cell_context = rank_col(1, ranking, u)
#
# from .colors import red_scale
#
# style = [color_class("%s-%s" % (u, intensity),
# red_scale[intensity])
# for intensity in range(9)]
#
# s.style.extend(style)
# # s.cell_context.extend(s) # TODO
Loading