Skip to content

Commit bdfcbbf

Browse files
chore: Add more ruff rules
1 parent a7c00b6 commit bdfcbbf

File tree

9 files changed

+88
-84
lines changed

9 files changed

+88
-84
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# See https://pre-commit.com/hooks.html for more hooks
33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
5-
rev: v4.6.0
5+
rev: v5.0.0
66
hooks:
77
- id: check-ast
88
- id: check-added-large-files

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55

66
## [Unreleased]
77
### Changed
8-
- Transition to uv.
8+
- Transition to `uv`.
9+
- Added more `ruff` rules.
910

1011
## [0.2.19] - 2024-04-27
1112
### Fixed

docs/control_index.ipynb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@
9999
"import df2img\n",
100100
"\n",
101101
"df = pd.DataFrame(\n",
102-
" data=dict(\n",
103-
" value_col_1=[1.4, float(\"NaN\"), 250, 24.65],\n",
104-
" str_col=(\"string1\", \"string2\", float(\"NaN\"), \"string4\"),\n",
105-
" ),\n",
102+
" data={\n",
103+
" \"value_col_1\": [1.4, float(\"NaN\"), 250, 24.65],\n",
104+
" \"str_col\": (\"string1\", \"string2\", float(\"NaN\"), \"string4\"),\n",
105+
" },\n",
106106
" index=[\"row1\", \"row2\", \"row3\", \"row4\"],\n",
107107
")\n",
108108
"\n",
@@ -225,9 +225,7 @@
225225
],
226226
"source": [
227227
"fig = df2img.plot_dataframe(\n",
228-
" df,\n",
229-
" title=dict(text=\"Table with index header\"),\n",
230-
" fig_size=(600, 140)\n",
228+
" df, title={\"text\": \"Table with index header\"}, fig_size=(600, 140)\n",
231229
")"
232230
]
233231
},
@@ -268,8 +266,8 @@
268266
"fig = df2img.plot_dataframe(\n",
269267
" df,\n",
270268
" print_index=False,\n",
271-
" title=dict(text=\"Table without index column\"),\n",
272-
" fig_size=(600, 140)\n",
269+
" title={\"text\": \"Table without index column\"},\n",
270+
" fig_size=(600, 140),\n",
273271
")"
274272
]
275273
}

docs/formatting.ipynb

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,11 @@
117117
"\n",
118118
"import df2img\n",
119119
"\n",
120-
"\n",
121120
"df = pd.DataFrame(\n",
122-
" data=dict(\n",
123-
" float_col=[1.4, float(\"NaN\"), 250, 24.65],\n",
124-
" str_col=(\"string1\", \"string2\", float(\"NaN\"), \"string4\"),\n",
125-
" ),\n",
121+
" data={\n",
122+
" \"float_col\": [1.4, float(\"NaN\"), 250, 24.65],\n",
123+
" \"str_col\": (\"string1\", \"string2\", float(\"NaN\"), \"string4\"),\n",
124+
" },\n",
126125
" index=[\"row1\", \"row2\", \"row3\", \"row4\"],\n",
127126
")\n",
128127
"\n",
@@ -175,16 +174,16 @@
175174
}
176175
],
177176
"source": [
178-
"fig=df2img.plot_dataframe(\n",
177+
"fig = df2img.plot_dataframe(\n",
179178
" df,\n",
180-
" title=dict(\n",
181-
" text=\"This is a title starting at the x-value x=0.1\",\n",
182-
" font_color=\"darkred\",\n",
183-
" font_size=20,\n",
184-
" font_family=\"Times New Roman\",\n",
185-
" x=0.1,\n",
186-
" xanchor=\"left\",\n",
187-
" ),\n",
179+
" title={\n",
180+
" \"text\": \"This is a title starting at the x-value x=0.1\",\n",
181+
" \"font_color\": \"darkred\",\n",
182+
" \"font_size\": 20,\n",
183+
" \"font_family\": \"Times New Roman\",\n",
184+
" \"x\": 0.1,\n",
185+
" \"xanchor\": \"left\",\n",
186+
" },\n",
188187
" fig_size=(600, 140),\n",
189188
")"
190189
]
@@ -222,10 +221,10 @@
222221
}
223222
],
224223
"source": [
225-
"fig=df2img.plot_dataframe(\n",
224+
"fig = df2img.plot_dataframe(\n",
226225
" df,\n",
227226
" tbl_header_visible=True, # default value\n",
228-
" fig_size=(600, 140)\n",
227+
" fig_size=(600, 140),\n",
229228
")"
230229
]
231230
},
@@ -248,11 +247,7 @@
248247
}
249248
],
250249
"source": [
251-
"fig=df2img.plot_dataframe(\n",
252-
" df,\n",
253-
" tbl_header_visible=False,\n",
254-
" fig_size=(600, 140)\n",
255-
")"
250+
"fig = df2img.plot_dataframe(df, tbl_header_visible=False, fig_size=(600, 140))"
256251
]
257252
},
258253
{
@@ -294,12 +289,12 @@
294289
"source": [
295290
"fig = df2img.plot_dataframe(\n",
296291
" df,\n",
297-
" tbl_header=dict(\n",
298-
" align=\"left\",\n",
299-
" fill_color=\"blue\",\n",
300-
" font_color=\"white\",\n",
301-
" font_size=14,\n",
302-
" ),\n",
292+
" tbl_header={\n",
293+
" \"align\": \"left\",\n",
294+
" \"fill_color\": \"blue\",\n",
295+
" \"font_color\": \"white\",\n",
296+
" \"font_size\": 14,\n",
297+
" },\n",
303298
" fig_size=(600, 140),\n",
304299
")"
305300
]
@@ -342,11 +337,11 @@
342337
"source": [
343338
"fig = df2img.plot_dataframe(\n",
344339
" df,\n",
345-
" tbl_cells=dict(\n",
346-
" align=\"right\",\n",
347-
" fill_color=\"green\",\n",
348-
" font_color=\"yellow\",\n",
349-
" ),\n",
340+
" tbl_cells={\n",
341+
" \"align\": \"right\",\n",
342+
" \"fill_color\": \"green\",\n",
343+
" \"font_color\": \"yellow\",\n",
344+
" },\n",
350345
" fig_size=(600, 140),\n",
351346
")"
352347
]
@@ -384,19 +379,19 @@
384379
"source": [
385380
"fig = df2img.plot_dataframe(\n",
386381
" df,\n",
387-
" tbl_header=dict(\n",
388-
" height=50,\n",
389-
" line_width=3,\n",
390-
" ),\n",
391-
" tbl_cells=dict(\n",
392-
" align=\"right\",\n",
393-
" fill_color=\"green\",\n",
394-
" font_color=\"yellow\",\n",
395-
" height=40,\n",
396-
" line_width=3,\n",
397-
" ),\n",
382+
" tbl_header={\n",
383+
" \"height\": 50,\n",
384+
" \"line_width\": 3,\n",
385+
" },\n",
386+
" tbl_cells={\n",
387+
" \"align\": \"right\",\n",
388+
" \"fill_color\": \"green\",\n",
389+
" \"font_color\": \"yellow\",\n",
390+
" \"height\": 40,\n",
391+
" \"line_width\": 3,\n",
392+
" },\n",
398393
" fig_size=(600, 140),\n",
399-
")\n"
394+
")"
400395
]
401396
},
402397
{
@@ -437,7 +432,7 @@
437432
" df,\n",
438433
" row_fill_color=(\"#ffffff\", \"#d7d8d6\"),\n",
439434
" fig_size=(600, 140),\n",
440-
")\n"
435+
")"
441436
]
442437
},
443438
{
@@ -479,7 +474,7 @@
479474
" df,\n",
480475
" col_width=[3, 2, 1],\n",
481476
" fig_size=(600, 140),\n",
482-
")\n"
477+
")"
483478
]
484479
},
485480
{
@@ -519,7 +514,7 @@
519514
" df,\n",
520515
" col_width=[0.5, 1, 0.5],\n",
521516
" fig_size=(600, 140),\n",
522-
")\n"
517+
")"
523518
]
524519
},
525520
{

docs/getting_started.ipynb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@
8787
"\n",
8888
"import df2img\n",
8989
"\n",
90-
"\n",
9190
"df = pd.DataFrame(\n",
92-
" data=dict(\n",
93-
" float_col=[1.4, float(\"NaN\"), 250, 24.65],\n",
94-
" str_col=(\"string1\", \"string2\", float(\"NaN\"), \"string4\"),\n",
95-
" ),\n",
91+
" data={\n",
92+
" \"float_col\": [1.4, float(\"NaN\"), 250, 24.65],\n",
93+
" \"str_col\": (\"string1\", \"string2\", float(\"NaN\"), \"string4\"),\n",
94+
" },\n",
9695
" index=[\"row1\", \"row2\", \"row3\", \"row4\"],\n",
9796
")\n",
9897
"\n",

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "df2img"
3-
version = "0.2.19"
3+
version = "0.2.20"
44
description = "Save a Pandas DataFrame as image"
55
authors = [
66
{name = "Andreas Vester", email = "[email protected]"},

ruff.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ ignore = [
4545
"ISC001", # checks for implicitly concatenated strings on a single line
4646
"PD010", # checks for .unstack or .pivot
4747
"PD013", # checks for .melt
48+
"PD901", # checks for dataframes names 'df'
4849
"PLR0913", # too many arguments in function definition
4950
"S101", # checks for ussage of assert
5051
"SLF001", # checks for private member access

src/df2img/main.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Collection of functions to plot and save a `pd.DataFrame`."""
22

3-
from __future__ import annotations
4-
53
from math import floor
64
from pathlib import Path
75
from typing import Any
@@ -12,13 +10,14 @@
1210

1311
def plot_dataframe(
1412
df: pd.Series | pd.DataFrame,
13+
*,
1514
print_index: bool = True,
1615
title: dict | None = None,
1716
tbl_header_visible: bool = True,
1817
tbl_header: dict | None = None,
1918
tbl_cells: dict | None = None,
2019
row_fill_color: tuple[str, str] | None = None,
21-
col_width: int | float | list[int | float] | None = None,
20+
col_width: float | list[int | float] | None = None,
2221
fig_size: tuple[int, int] | None = None,
2322
show_fig: bool = True,
2423
plotly_renderer: str = "png",
@@ -211,5 +210,3 @@ def save_dataframe(fig: plotly.graph_objects.Figure, filename: Path) -> None:
211210
212211
"""
213212
fig.write_image(filename)
214-
215-
return None

tests/test_df2img.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
"""Test functions in df2img.py."""
22

3-
from __future__ import annotations
4-
53
from pathlib import Path
64

7-
import df2img
85
import pandas as pd
96
import plotly.graph_objects as go
107
import pytest
118

9+
import df2img
10+
1211

1312
@pytest.fixture(scope="module")
1413
def df_without_index() -> pd.DataFrame:
@@ -52,7 +51,7 @@ def test_plot_dataframe_without_args(df_without_index: pd.DataFrame) -> None:
5251

5352
assert isinstance(fig, go.Figure)
5453
assert fig.layout.title.__getattribute__("text") is None
55-
assert fig.data[0].header.values == ("<b>float_col<b>", "<b>str_col<b>")
54+
assert fig.data[0].header.values == ("<b>float_col<b>", "<b>str_col<b>") # noqa: PD011
5655

5756

5857
# noinspection PyUnresolvedReferences
@@ -66,7 +65,7 @@ def test_plot_dataframe_without_args_print_index(
6665

6766
assert isinstance(fig, go.Figure)
6867
assert fig.layout.title.__getattribute__("text") is None
69-
assert fig.data[0].header.values == ("<b><b>", "<b>float_col<b>", "<b>str_col<b>")
68+
assert fig.data[0].header.values == ("<b><b>", "<b>float_col<b>", "<b>str_col<b>") # noqa: PD011
7069

7170

7271
def test_plot_dataframe_title_text(df_with_index_even_row_count: pd.DataFrame) -> None:
@@ -85,11 +84,14 @@ def test_plot_dataframe_title_text(df_with_index_even_row_count: pd.DataFrame) -
8584
show_fig=False,
8685
)
8786

87+
font_size = 20
88+
horizontal_title_pos_from_left_edge = 0.1
89+
8890
assert fig.layout.title.font.color == "red"
8991
assert fig.layout.title.font.family == "Arial"
90-
assert fig.layout.title.font.size == 20
92+
assert fig.layout.title.font.size == font_size
9193
assert fig.layout.title.text == "This is a title"
92-
assert fig.layout.title.x == 0.1
94+
assert fig.layout.title.x == horizontal_title_pos_from_left_edge
9395
assert fig.layout.title.xanchor == "right"
9496

9597

@@ -109,13 +111,17 @@ def test_plot_dataframe_header(df_with_index_even_row_count: pd.DataFrame) -> No
109111
show_fig=False,
110112
)
111113

114+
font_size = 14
115+
height = 20
116+
line_width = 2
117+
112118
assert fig.data[0].header.align == "right"
113119
assert fig.data[0].header.fill.color == "blue"
114120
assert fig.data[0].header.font.color == "white"
115121
assert fig.data[0].header.font.family == "Arial"
116-
assert fig.data[0].header.font.size == 14
117-
assert fig.data[0].header.height == 20
118-
assert fig.data[0].header.line.width == 2
122+
assert fig.data[0].header.font.size == font_size
123+
assert fig.data[0].header.height == height
124+
assert fig.data[0].header.line.width == line_width
119125

120126

121127
def test_plot_dataframe_header_invisible(
@@ -160,13 +166,17 @@ def test_plot_dataframe_cells(df_with_index_even_row_count: pd.DataFrame) -> Non
160166
show_fig=False,
161167
)
162168

169+
font_size = 10
170+
height = 30
171+
line_width = 1.5
172+
163173
assert fig.data[0].cells.align == "left"
164174
assert fig.data[0].cells.fill.color == "darkred"
165175
assert fig.data[0].cells.font.color == "lightblue"
166176
assert fig.data[0].cells.font.family == "Times New Roman"
167-
assert fig.data[0].cells.font.size == 10
168-
assert fig.data[0].cells.height == 30
169-
assert fig.data[0].cells.line.width == 1.5
177+
assert fig.data[0].cells.font.size == font_size
178+
assert fig.data[0].cells.height == height
179+
assert fig.data[0].cells.line.width == line_width
170180

171181

172182
def test_plot_dataframe_row_fill_color_even_row_count(
@@ -225,8 +235,11 @@ def test_plot_dataframe_fig_size(df_with_index_even_row_count: pd.DataFrame) ->
225235
show_fig=False,
226236
)
227237

228-
assert fig.layout.width == 400
229-
assert fig.layout.height == 200
238+
width = 400
239+
height = 200
240+
241+
assert fig.layout.width == width
242+
assert fig.layout.height == height
230243

231244

232245
def test_plot_dataframe_paper_bgcolor_rgba(

0 commit comments

Comments
 (0)