make_hyperlink(text, url, new_tab=True, *, name='hyperlink')
Returns a Polars expression that generates an HTML hyperlink (<a>
tag) for each row.
This function is heavily inspired by the gt-extras package.
Parameters
text : str
-
Column name containing the display text for the hyperlink.
url : str
-
Column name containing the destination URL.
new_tab : bool = True
-
Whether the link opens in a new browser tab (target="_blank"
) or the current tab.
name : str = 'hyperlink'
-
The name of the resulting column.
Returns
: pl.Expr
-
A Polars expression generating the HTML anchor tag.
Examples
DataFrame Context
Create an HTML anchor tag (<a>
) combining link text and URL from two columns:
import polars as pl
import turtle_island as ti
pl.Config.set_fmt_str_lengths(200)
df = pl.DataFrame(
{
"name": ["Turtle Island"],
"url": ["https://github.com/jrycw/turtle-island"],
}
)
new_df = df.with_columns(ti.make_hyperlink("name", "url"))
new_df
shape: (1, 3)name | url | hyperlink |
---|
str | str | str |
"Turtle Island" | "https://github.com/jrycw/turtle-island" | "<a href="https://github.com/jrycw/turtle-island" target="_blank">Turtle Island</a>" |
name |
url |
hyperlink |
Turtle Island |
https://github.com/jrycw/turtle-island |
Turtle Island |