pl2html 0.5.1__tar.gz → 0.6.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pl2html
3
- Version: 0.5.1
3
+ Version: 0.6.1
4
4
  Summary: Add your description here
5
5
  Requires-Dist: polars>=1.42.1
6
6
  Requires-Python: >=3.14
@@ -0,0 +1 @@
1
+ __version__ = '0.6.1'
@@ -1,3 +1,5 @@
1
+ from html import escape as _escape
2
+
1
3
  from polars import (
2
4
  DataFrame as _DataFrame,
3
5
  DataType as _DataType,
@@ -82,19 +84,16 @@ def _build_cell_expr(
82
84
 
83
85
  fmt_expr = fmt_expr.fill_null('')
84
86
 
85
- attr_expr_list = []
86
- if col_name in attrs:
87
- for attr_name, val_expr in attrs[col_name].items():
88
- val_str_expr = val_expr.cast(_String)
89
-
90
- compiled_attr = (
91
- _when(val_str_expr.is_not_null() & (val_str_expr != ''))
92
- .then(_lit(f' {attr_name}="') + val_str_expr + _lit('"'))
93
- .otherwise(_lit(''))
87
+ if col_attrs := attrs.get(col_name):
88
+ attr_expr_list = [
89
+ _when(
90
+ (val_str_expr := val_expr.cast(_String)).is_not_null()
91
+ & (val_str_expr != '')
94
92
  )
95
- attr_expr_list.append(compiled_attr)
96
-
97
- if attr_expr_list:
93
+ .then(_lit(f' {attr_name}="') + val_str_expr + _lit('"'))
94
+ .otherwise(_lit(''))
95
+ for attr_name, val_expr in col_attrs.items()
96
+ ]
98
97
  opening_td = _lit('<td') + _concat_str(attr_expr_list) + _lit('>')
99
98
  else:
100
99
  opening_td = _lit('<td>')
@@ -104,8 +103,13 @@ def _build_cell_expr(
104
103
 
105
104
  def _build_html_skeleton(visible_columns: list[str]) -> tuple[_Expr, _Expr]:
106
105
  header_parts = ['<table>', ' <thead>\n <tr>']
106
+
107
107
  for c in visible_columns:
108
- header_parts.append(f' <th>{c}</th>')
108
+ # NOTE: quote=False allows names like `Alice's "score"` to render naturally.
109
+ # This is safe ONLY because the string is confined strictly inside text nodes (<th>).
110
+ # DO NOT reuse these variables within HTML attributes.
111
+ header_parts.append(f' <th>{_escape(c, quote=False)}</th>')
112
+
109
113
  header_parts.append(' </tr>\n </thead>\n <tbody>')
110
114
 
111
115
  html_header = _lit('\n'.join(header_parts) + '\n')
@@ -148,17 +152,6 @@ def _resolve_attributes(
148
152
  return lf, new_attrs
149
153
 
150
154
 
151
- def _apply_user_formatters(
152
- lf: _LazyFrame, formatters: _Expr | list[_Expr] | None
153
- ) -> _LazyFrame:
154
- """Applies layout transformation modifications/conversions to columns."""
155
- if formatters is None:
156
- return lf
157
- if isinstance(formatters, _Expr):
158
- formatters = [formatters]
159
- return lf.with_columns(formatters)
160
-
161
-
162
155
  def _compile_html(
163
156
  lf: _LazyFrame,
164
157
  visible_columns: list[str],
@@ -251,9 +244,7 @@ def to_html(
251
244
  """
252
245
  lf = df.lazy() if isinstance(df, _DataFrame) else df
253
246
  exclude_columns = exclude_columns or []
254
- attrs = attrs or {}
255
247
 
256
- # Setup the evaluation context data
257
248
  df_eager = lf.collect()
258
249
  lf = df_eager.lazy()
259
250
 
@@ -261,7 +252,12 @@ def to_html(
261
252
  c for c in df_eager.schema.names() if c not in exclude_columns
262
253
  ]
263
254
 
264
- lf, attrs = _resolve_attributes(lf, df_eager, attrs, visible_columns)
265
- lf = _apply_user_formatters(lf, formatters)
255
+ if attrs:
256
+ lf, attrs = _resolve_attributes(lf, df_eager, attrs, visible_columns)
257
+ else:
258
+ attrs = {}
259
+
260
+ if formatters is not None:
261
+ lf = lf.with_columns(formatters)
266
262
 
267
263
  return _compile_html(lf, visible_columns, attrs)
@@ -34,9 +34,24 @@ def _interpolate_segment(
34
34
  local_factor = (norm_val - seg_start) / segment_width
35
35
 
36
36
  # Linear interpolation formula applied directly to Polars Expressions
37
- r_expr = (_lit(r1) + (_lit(r2 - r1) * local_factor)).round(0).cast(_Int32)
38
- g_expr = (_lit(g1) + (_lit(g2 - g1) * local_factor)).round(0).cast(_Int32)
39
- b_expr = (_lit(b1) + (_lit(b2 - b1) * local_factor)).round(0).cast(_Int32)
37
+ r_expr = (
38
+ (_lit(r1) + (_lit(r2 - r1) * local_factor))
39
+ .fill_nan(None)
40
+ .round(0)
41
+ .cast(_Int32)
42
+ )
43
+ g_expr = (
44
+ (_lit(g1) + (_lit(g2 - g1) * local_factor))
45
+ .fill_nan(None)
46
+ .round(0)
47
+ .cast(_Int32)
48
+ )
49
+ b_expr = (
50
+ (_lit(b1) + (_lit(b2 - b1) * local_factor))
51
+ .fill_nan(None)
52
+ .round(0)
53
+ .cast(_Int32)
54
+ )
40
55
 
41
56
  return r_expr, g_expr, b_expr
42
57
 
@@ -4,7 +4,7 @@ build-backend = 'uv_build'
4
4
 
5
5
  [project]
6
6
  name = "pl2html"
7
- version = "0.5.1"
7
+ version = "0.6.1"
8
8
  description = "Add your description here"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.14"
@@ -1 +0,0 @@
1
- __version__ = '0.5.1'
File without changes
File without changes