@@ -798,6 +798,7 @@ def format(
798
798
decimal : str = "." ,
799
799
thousands : str | None = None ,
800
800
escape : str | None = None ,
801
+ hyperlinks : str | None = None ,
801
802
) -> StylerRenderer :
802
803
r"""
803
804
Format the text display value of cells.
@@ -842,6 +843,13 @@ def format(
842
843
843
844
.. versionadded:: 1.3.0
844
845
846
+ hyperlinks : {"html", "latex"}, optional
847
+ Convert string patterns containing https://, http://, ftp:// or www. to
848
+ HTML <a> tags as clickable URL hyperlinks if "html", or LaTeX \href
849
+ commands if "latex".
850
+
851
+ .. versionadded:: 1.4.0
852
+
845
853
Returns
846
854
-------
847
855
self : Styler
@@ -958,6 +966,7 @@ def format(
958
966
thousands is None ,
959
967
na_rep is None ,
960
968
escape is None ,
969
+ hyperlinks is None ,
961
970
)
962
971
):
963
972
self ._display_funcs .clear ()
@@ -980,6 +989,7 @@ def format(
980
989
decimal = decimal ,
981
990
thousands = thousands ,
982
991
escape = escape ,
992
+ hyperlinks = hyperlinks ,
983
993
)
984
994
for ri in ris :
985
995
self ._display_funcs [(ri , ci )] = format_func
@@ -996,6 +1006,7 @@ def format_index(
996
1006
decimal : str = "." ,
997
1007
thousands : str | None = None ,
998
1008
escape : str | None = None ,
1009
+ hyperlinks : str | None = None ,
999
1010
) -> StylerRenderer :
1000
1011
r"""
1001
1012
Format the text display value of index labels or column headers.
@@ -1027,6 +1038,10 @@ def format_index(
1027
1038
``{``, ``}``, ``~``, ``^``, and ``\`` in the cell display string with
1028
1039
LaTeX-safe sequences.
1029
1040
Escaping is done before ``formatter``.
1041
+ hyperlinks : {"html", "latex"}, optional
1042
+ Convert string patterns containing https://, http://, ftp:// or www. to
1043
+ HTML <a> tags as clickable URL hyperlinks if "html", or LaTeX \href
1044
+ commands if "latex".
1030
1045
1031
1046
Returns
1032
1047
-------
@@ -1128,6 +1143,7 @@ def format_index(
1128
1143
thousands is None ,
1129
1144
na_rep is None ,
1130
1145
escape is None ,
1146
+ hyperlinks is None ,
1131
1147
)
1132
1148
):
1133
1149
display_funcs_ .clear ()
@@ -1149,6 +1165,7 @@ def format_index(
1149
1165
decimal = decimal ,
1150
1166
thousands = thousands ,
1151
1167
escape = escape ,
1168
+ hyperlinks = hyperlinks ,
1152
1169
)
1153
1170
1154
1171
for idx in [(i , lvl ) if axis == 0 else (lvl , i ) for i in range (len (obj ))]:
@@ -1391,13 +1408,28 @@ def _str_escape(x, escape):
1391
1408
return x
1392
1409
1393
1410
1411
+ def _render_href (x , format ):
1412
+ """uses regex to detect a common URL pattern and converts to href tag in format."""
1413
+ if isinstance (x , str ):
1414
+ if format == "html" :
1415
+ href = '<a href="{0}" target="_blank">{0}</a>'
1416
+ elif format == "latex" :
1417
+ href = r"\href{{{0}}}{{{0}}}"
1418
+ else :
1419
+ raise ValueError ("``hyperlinks`` format can only be 'html' or 'latex'" )
1420
+ pat = r"(https?:\/\/|ftp:\/\/|www.)[\w/\-?=%.]+\.[\w/\-&?=%.]+"
1421
+ return re .sub (pat , lambda m : href .format (m .group (0 )), x )
1422
+ return x
1423
+
1424
+
1394
1425
def _maybe_wrap_formatter (
1395
1426
formatter : BaseFormatter | None = None ,
1396
1427
na_rep : str | None = None ,
1397
1428
precision : int | None = None ,
1398
1429
decimal : str = "." ,
1399
1430
thousands : str | None = None ,
1400
1431
escape : str | None = None ,
1432
+ hyperlinks : str | None = None ,
1401
1433
) -> Callable :
1402
1434
"""
1403
1435
Allows formatters to be expressed as str, callable or None, where None returns
@@ -1431,11 +1463,17 @@ def _maybe_wrap_formatter(
1431
1463
else :
1432
1464
func_2 = func_1
1433
1465
1466
+ # Render links
1467
+ if hyperlinks is not None :
1468
+ func_3 = lambda x : func_2 (_render_href (x , format = hyperlinks ))
1469
+ else :
1470
+ func_3 = func_2
1471
+
1434
1472
# Replace missing values if na_rep
1435
1473
if na_rep is None :
1436
- return func_2
1474
+ return func_3
1437
1475
else :
1438
- return lambda x : na_rep if isna (x ) else func_2 (x )
1476
+ return lambda x : na_rep if isna (x ) else func_3 (x )
1439
1477
1440
1478
1441
1479
def non_reducing_slice (slice_ : Subset ):
0 commit comments