Skip to content

Commit

Permalink
fix async playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
PaleNeutron committed Jan 30, 2025
1 parent 87eb688 commit ecad1a5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
7 changes: 4 additions & 3 deletions dataframe_image/_pandas_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def export(
"selenium": SeleniumConverter,
"html2image": Html2ImageConverter,
"playwright": PlayWrightConverter,
"async_playwright": AsyncPlayWrightConverter,
"playwright_async": AsyncPlayWrightConverter,
}


Expand All @@ -72,7 +72,7 @@ def prepare_converter(
max_rows=None,
max_cols=None,
table_conversion: Literal[
"chrome", "matplotlib", "html2image", "playwright", "selenium"
"chrome", "matplotlib", "html2image", "playwright", "selenium", "playwright_async"
] = "playwright",
chrome_path=None,
dpi=None,
Expand Down Expand Up @@ -165,7 +165,8 @@ def generate_html(
html = obj.to_html(max_rows=max_rows, max_cols=max_cols, notebook=True)
# wrap html with a div and add id `dfi_table`
html = f'<div id="dfi_table">{html}</div>'
return html
html_template = f"""<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta charset="UTF-8"/></head>{html}</html>"""
return html_template


def save_image(img_str, filename):
Expand Down
3 changes: 1 addition & 2 deletions dataframe_image/pd_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ def styler2html(style):
html = style.to_html()
else:
html = style.render()
html_template = f"""<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta charset="UTF-8"/></head>{html}</html>"""
return html_template
return html
49 changes: 39 additions & 10 deletions tests/test_df_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
)

test_dpi_values = [100, 200, 300]
converters = ["chrome", "selenium", "matplotlib", "html2image", "playwright"]
converters = [
"chrome",
"selenium",
"matplotlib",
"html2image",
"playwright",
]


@pytest.fixture
Expand Down Expand Up @@ -163,9 +169,28 @@ def test_caption_cut(get_df):
)


@pytest.mark.parametrize("converter", converters)
def test_complex_styled_df(document_name, get_df, converter):
df = get_df.copy()
# @pytest.mark.parametrize("converter", converters)
async def test_complex_styled_df(document_name):
converter = "playwright_async"
from matplotlib.colors import LinearSegmentedColormap

custom_cmap = LinearSegmentedColormap.from_list(
"custom_cmap", ["#FFA07A", "white", "#ADD8E6"]
) # Bleu clair, blanc, rouge clair

df = pd.DataFrame(
{
"Fonds": ["Fonds A", "Fonds B", "Fonds C"],
"Performance": [5.4, -2.1, 3.7],
"Volatilité": [12.3, 15.2, 9.8],
}
)

# Fonction pour appliquer le style conditionnel
col_format = {
"Performance": "{:.1f}%",
"Volatilité": "{:.1f}",
}
table_styles = [
{
"selector": "thead th", # Style pour les en-têtes de colonnes
Expand All @@ -190,25 +215,29 @@ def test_complex_styled_df(document_name, get_df, converter):
"props": [("width", "70px")],
},
]

perf_indice_stylised = (
styled_df = (
df.style.set_properties(
**{"text-align": "center"}
) # Centrer le contenu des colonnes
.set_table_styles(table_styles, overwrite=False) # Appliquer les propriétés CSS
.format(
col_format, na_rep=""
) # Appliquer les formats avec valeurs manquantes remplacées par ''
.bar(
align=0,
vmin=0,
vmax=5000,
vmin=-2.5,
vmax=2.5,
height=50,
width=50,
cmap=custom_cmap,
subset=["Performance"],
)
.hide(axis=0) # Masquer l'index
)

image_path = f"tests/test_output/{document_name}.png"
dfi.export(
perf_indice_stylised,
await dfi.export_async(
styled_df,
image_path,
table_conversion=converter,
)

0 comments on commit ecad1a5

Please sign in to comment.