@@ -798,6 +798,7 @@ def format(
798798 decimal : str = "." ,
799799 thousands : str | None = None ,
800800 escape : str | None = None ,
801+ hyperlinks : str | None = None ,
801802 ) -> StylerRenderer :
802803 r"""
803804 Format the text display value of cells.
@@ -842,6 +843,13 @@ def format(
842843
843844 .. versionadded:: 1.3.0
844845
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+
845853 Returns
846854 -------
847855 self : Styler
@@ -958,6 +966,7 @@ def format(
958966 thousands is None ,
959967 na_rep is None ,
960968 escape is None ,
969+ hyperlinks is None ,
961970 )
962971 ):
963972 self ._display_funcs .clear ()
@@ -980,6 +989,7 @@ def format(
980989 decimal = decimal ,
981990 thousands = thousands ,
982991 escape = escape ,
992+ hyperlinks = hyperlinks ,
983993 )
984994 for ri in ris :
985995 self ._display_funcs [(ri , ci )] = format_func
@@ -996,6 +1006,7 @@ def format_index(
9961006 decimal : str = "." ,
9971007 thousands : str | None = None ,
9981008 escape : str | None = None ,
1009+ hyperlinks : str | None = None ,
9991010 ) -> StylerRenderer :
10001011 r"""
10011012 Format the text display value of index labels or column headers.
@@ -1027,6 +1038,10 @@ def format_index(
10271038 ``{``, ``}``, ``~``, ``^``, and ``\`` in the cell display string with
10281039 LaTeX-safe sequences.
10291040 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".
10301045
10311046 Returns
10321047 -------
@@ -1128,6 +1143,7 @@ def format_index(
11281143 thousands is None ,
11291144 na_rep is None ,
11301145 escape is None ,
1146+ hyperlinks is None ,
11311147 )
11321148 ):
11331149 display_funcs_ .clear ()
@@ -1149,6 +1165,7 @@ def format_index(
11491165 decimal = decimal ,
11501166 thousands = thousands ,
11511167 escape = escape ,
1168+ hyperlinks = hyperlinks ,
11521169 )
11531170
11541171 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):
13911408 return x
13921409
13931410
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+
13941425def _maybe_wrap_formatter (
13951426 formatter : BaseFormatter | None = None ,
13961427 na_rep : str | None = None ,
13971428 precision : int | None = None ,
13981429 decimal : str = "." ,
13991430 thousands : str | None = None ,
14001431 escape : str | None = None ,
1432+ hyperlinks : str | None = None ,
14011433) -> Callable :
14021434 """
14031435 Allows formatters to be expressed as str, callable or None, where None returns
@@ -1431,11 +1463,17 @@ def _maybe_wrap_formatter(
14311463 else :
14321464 func_2 = func_1
14331465
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+
14341472 # Replace missing values if na_rep
14351473 if na_rep is None :
1436- return func_2
1474+ return func_3
14371475 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 )
14391477
14401478
14411479def non_reducing_slice (slice_ : Subset ):
0 commit comments