Skip to content

Commit ff1fa48

Browse files
committed
2 parents 7967704 + 5349ffb commit ff1fa48

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

dataframe_image/_matplotlib_table.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class TableMaker:
1616
def __init__(
17-
self, fontsize=14, encode_base64=True, limit_crop=True, for_document=True
17+
self, fontsize=14, encode_base64=True, limit_crop=True, for_document=True, savefig_dpi=None
1818
):
1919
self.original_fontsize = fontsize
2020
self.encode_base64 = encode_base64
@@ -28,6 +28,7 @@ def __init__(
2828
self.figheight = 4
2929
self.wrap_length = 10
3030
self.dpi = 100
31+
self.savefig_dpi = savefig_dpi
3132

3233
def parse_html(self, html):
3334
html = html.replace("<br>", "\n")
@@ -249,7 +250,7 @@ def print_table(self):
249250
end = self.figwidth - start
250251
bbox = Bbox([[start - 0.1, y * h], [end + 0.1, h]])
251252
buffer = io.BytesIO()
252-
self.fig.savefig(buffer, bbox_inches=bbox)
253+
self.fig.savefig(buffer, bbox_inches=bbox, dpi=self.savefig_dpi)
253254
img_str = buffer.getvalue()
254255
if self.encode_base64:
255256
img_str = base64.b64encode(img_str).decode()

dataframe_image/_pandas_accessor.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def export(
2121
max_cols=None,
2222
table_conversion="chrome",
2323
chrome_path=None,
24+
dpi=None
2425
):
2526
return _export(
2627
self._df,
@@ -30,6 +31,7 @@ def export(
3031
max_cols,
3132
table_conversion,
3233
chrome_path,
34+
dpi
3335
)
3436

3537

@@ -41,13 +43,14 @@ def export(
4143
max_cols=None,
4244
table_conversion="chrome",
4345
chrome_path=None,
46+
dpi=None
4447
):
4548
return _export(
46-
obj, filename, fontsize, max_rows, max_cols, table_conversion, chrome_path
47-
)
49+
obj, filename, fontsize, max_rows, max_cols, table_conversion, chrome_path, dpi
50+
)
4851

4952

50-
def _export(obj, filename, fontsize, max_rows, max_cols, table_conversion, chrome_path):
53+
def _export(obj, filename, fontsize, max_rows, max_cols, table_conversion, chrome_path, dpi):
5154
is_styler = isinstance(obj, Styler)
5255
df = obj.data if is_styler else obj
5356

@@ -59,12 +62,13 @@ def _export(obj, filename, fontsize, max_rows, max_cols, table_conversion, chrom
5962
fontsize=fontsize,
6063
encode_base64=False,
6164
limit_crop=False,
65+
device_scale_factor=(1 if dpi == None else dpi/100.0)
6266
).run
6367
else:
6468
from ._matplotlib_table import TableMaker
6569

6670
converter = TableMaker(
67-
fontsize=fontsize, encode_base64=False, for_document=False
71+
fontsize=fontsize, encode_base64=False, for_document=False, savefig_dpi=dpi
6872
).run
6973

7074
if df.shape[0] > MAX_ROWS and max_rows is None:
@@ -166,6 +170,15 @@ def _export(obj, filename, fontsize, max_rows, max_cols, table_conversion, chrom
166170
Path to your machine's chrome executable. When `None`, it is
167171
automatically found. Use this when chrome is not automatically found.
168172
173+
dpi : int, default `None`
174+
Dots per inch - Use this to change the resolution ("quality") of the image.
175+
176+
If `table_conversion`=`matplotlib`, this value is directly passed to
177+
the "savefig" method. When `None`, the figure's DPI is used.
178+
179+
If `table_conversion`=`chrome`, the dpi value is converted to a
180+
"device scale factor" but should provide the same effect. When `None`,
181+
the "device scale factor" is 1.
169182
"""
170183

171184
export_intro = """

dataframe_image/_screenshot.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def __init__(
8383
fontsize=18,
8484
encode_base64=True,
8585
limit_crop=True,
86+
device_scale_factor=1
8687
):
8788
self.center_df = center_df
8889
self.max_rows = max_rows
@@ -93,6 +94,7 @@ def __init__(
9394
self.css = self.get_css(fontsize)
9495
self.encode_base64 = encode_base64
9596
self.limit_crop = limit_crop
97+
self.device_scale_factor = device_scale_factor
9698

9799
def get_css(self, fontsize):
98100
mod_dir = Path(__file__).resolve().parent
@@ -117,6 +119,7 @@ def take_screenshot(self):
117119
"--headless",
118120
"--no-sandbox",
119121
"--crash-dumps-dir=/tmp",
122+
f"--force-device-scale-factor={self.device_scale_factor}",
120123
]
121124

122125
if self.ss_width and self.ss_height:

tests/dfi-dpi-change-check.xlsx

66.7 KB
Binary file not shown.

tests/test_df_image.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,34 @@
77
"tests/notebooks/data/covid19.csv", parse_dates=["date"], index_col="date"
88
)
99

10+
test_dpi_values = [50, 400, 600, 800]
1011

1112
class TestImage:
1213
def test_df(self):
1314
df.tail(10).dfi.export("tests/test_output/covid19.png")
1415

16+
@pytest.mark.parametrize('dpi', test_dpi_values)
17+
def test_chrome_changed_dpi(self, dpi):
18+
df.tail(10).dfi.export(f"tests/test_output/covid19_dpi_{dpi}.png", dpi=dpi)
19+
1520
def test_styled(self):
1621
df.tail(10).style.background_gradient().export_png(
1722
"tests/test_output/covid19_styled.png"
1823
)
1924

25+
@pytest.mark.parametrize('dpi', test_dpi_values)
26+
def test_styled_changed_dpi(self, dpi):
27+
df.tail(10).style.background_gradient().export_png(
28+
f"tests/test_output/covid19_styled_dpi_{dpi}.png", dpi=dpi
29+
)
30+
2031
def test_mpl(self):
2132
df.tail(10).dfi.export(
2233
"tests/test_output/covid19_mpl.png", table_conversion="matplotlib"
2334
)
35+
36+
@pytest.mark.parametrize('dpi', test_dpi_values)
37+
def test_mpl_changed_dpi(self, dpi):
38+
df.tail(10).dfi.export(
39+
f"tests/test_output/covid19_mpl_dpi_{dpi}.png",
40+
table_conversion="matplotlib", dpi=dpi)

0 commit comments

Comments
 (0)