Skip to content

Commit eda45dd

Browse files
committed
Fix the evaluation of JavascriptFunction objects
1 parent 4e25da6 commit eda45dd

File tree

4 files changed

+9
-62
lines changed

4 files changed

+9
-62
lines changed

docs/changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ITables ChangeLog
22
=================
33

4-
2.4.0rc0 (2025-05-11)
4+
2.4.0rc1 (2025-05-11)
55
---------------------
66

77
**Added**

docs/formatting.md

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -109,61 +109,3 @@ Please note that Pandas Style objects are rendered using
109109
their `.to_html()` method, which is less efficient that
110110
the default JS data export used by ITables.
111111
```
112-
113-
## HTML in cells
114-
115-
### A simple example
116-
117-
HTML content is supported, which means that you can have formatted text,
118-
links or even images in your tables:
119-
120-
```{code-cell} ipython3
121-
pd.Series(
122-
[
123-
"<b>bold</b>",
124-
"<i>italic</i>",
125-
'<a href="https://github.com/mwouts/itables">link</a>',
126-
],
127-
name="HTML",
128-
)
129-
```
130-
131-
### Images in a table
132-
133-
```{code-cell} ipython3
134-
:tags: [full-width]
135-
136-
df = itables.sample_dfs.get_countries(html=False)
137-
138-
df["flag"] = [
139-
'<a href="https://flagpedia.net/{code}">'
140-
'<img src="https://flagpedia.net/data/flags/h80/{code}.webp" '
141-
'alt="Flag of {country}"></a>'.format(code=code.lower(), country=country)
142-
for code, country in zip(df.index, df["country"])
143-
]
144-
df["country"] = [
145-
'<a href="https://en.wikipedia.org/wiki/{}">{}</a>'.format(country, country)
146-
for country in df["country"]
147-
]
148-
df["capital"] = [
149-
'<a href="https://en.wikipedia.org/wiki/{}">{}</a>'.format(capital, capital)
150-
for capital in df["capital"]
151-
]
152-
df
153-
```
154-
155-
### Base64 images
156-
157-
[Base64 encoded image](https://stackoverflow.com/a/8499716/9817073) are supported, too:
158-
159-
```{code-cell} ipython3
160-
pd.Series(
161-
{
162-
"url": '<img src="https://storage.googleapis.com/tfds-data/visualization/fig/mnist-3.0.1.png" height="50" alt="MNIST">',
163-
"base64": '<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA'
164-
"AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO"
165-
'9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">',
166-
},
167-
name="Images",
168-
)
169-
```

src/itables/javascript.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from base64 import b64encode
88
from importlib.util import find_spec
99
from pathlib import Path
10-
from typing import Any, Mapping, Optional, Sequence, Union, cast
10+
from typing import Any, Optional, Sequence, Union, cast
1111

1212
import numpy as np
1313
import pandas as pd
@@ -275,8 +275,13 @@ def get_keys_to_be_evaluated(data) -> list[list[Union[int, str]]]:
275275
keys_to_be_evaluated = []
276276
if isinstance(data, Sequence):
277277
data = dict(enumerate(data))
278-
if isinstance(data, Mapping):
278+
if isinstance(data, dict):
279279
for key, value in data.items():
280+
if isinstance(value, JavascriptFunction):
281+
# eval can't evaluate a function,
282+
# but it can evaluate an expression that contains a function
283+
# e.g. eval('(function() {return 5;})') does returns the function
284+
data[key] = f"({value})"
280285
if isinstance(value, (JavascriptCode, JavascriptFunction)):
281286
keys_to_be_evaluated.append([key])
282287
else:

src/itables/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""ITables' version number"""
22

3-
__version__ = "2.4.0rc0"
3+
__version__ = "2.4.0rc1"

0 commit comments

Comments
 (0)