gt-extras
  • Intro
  • API Reference
  1. Icons and Images
  2. add_text_img
  • API Reference
  • Plotting
    • gt_plt_bar
    • gt_plt_bar_pct
    • gt_plt_bar_stack
    • gt_plt_conf_int
    • gt_plt_dot
    • gt_plt_dumbbell
    • gt_plt_winloss
  • Colors
    • gt_color_box
    • gt_data_color_by_group
    • gt_highlight_cols
    • gt_highlight_rows
    • gt_hulk_col_numeric
  • Themes
    • gt_theme_538
    • gt_theme_dark
    • gt_theme_dot_matrix
    • gt_theme_espn
    • gt_theme_excel
    • gt_theme_guardian
    • gt_theme_nytimes
    • gt_theme_pff
  • Icons and Images
    • add_text_img
    • fa_icon_repeat
    • gt_fa_rank_change
    • gt_fa_rating
    • img_header
  • Utilities
    • fmt_pct_extra
    • gt_add_divider
    • gt_duplicate_column
    • gt_merge_stack
    • gt_two_column_layout
    • with_hyperlink
    • with_tooltip

On this page

  • Parameters
  • Returns
  • Examples
  • See Also
  1. Icons and Images
  2. add_text_img

add_text_img

add_text_img(text, img_url, height=30, gap=3.0, left=False, alt_text='')

Create an HTML element with text and an image, displayed inline.

Note that depending on where you are placing the output in the table, you may want to wrap it in GT.html().

Parameters

text : str

The text to display alongside the image.

img_url : str

The URL of the image to display. This can be a filepath or an image on the web.

height : int = 30

The height of the image in pixels.

gap : float = 3.0

The spacing between the text and the image in pixels.

left : bool = False

If True, the image is displayed to the left of the text.

alt_text : str = ''

The alternative text for the image, used for accessibility and displayed if the image cannot be loaded.

Returns

: str

A string with html content of the combined image and text. Depending on where you are placing the output in the table, you may want to wrap it in GT.html().

Examples

import pandas as pd
from great_tables import GT, md, html
import gt_extras as gte

df = pd.DataFrame(
    {
        "Player": ["Josh Hart", "Jalen Brunson"],
        "Points": [1051, 1690],
        "Assists": [453, 475],
    }
)

hart_img = gte.add_text_img(
    text="Josh Hart",
    img_url="https://a.espncdn.com/combiner/i?img=/i/headshots/nba/players/full/3062679.png",
)

brunson_img = gte.add_text_img(
    text="Jalen Brunson",
    img_url="https://a.espncdn.com/combiner/i?img=/i/headshots/nba/players/full/3934672.png",
)

df["Player"] = [hart_img, brunson_img]
gt = (
    GT(df, rowname_col="Player")
    .tab_source_note(md("Images and data courtesy of [ESPN](https://www.espn.com)"))
)

gt
Points Assists
Josh Hart
1051 453
Jalen Brunson
1690 475
Images and data courtesy of ESPN

We can even apply the add_text_img() function to content outside of body/stub cells. We must remember to wrap the output in GT.html() so the table renders the element properly.

points_with_img = gte.add_text_img(
    text="Points",
    img_url="../assets/hoop.png",
    left=True,
)

assists_with_img = gte.add_text_img(
    text="Assists",
    img_url="../assets/pass.png",
    left=True,
)

points_img_html = html(points_with_img)
assists_img_html = html(assists_with_img)

(
    gt
    .cols_label({"Points": points_img_html, "Assists": assists_img_html})
    .cols_align("center")
)
Points
Assists
Josh Hart
1051 453
Jalen Brunson
1690 475
Images and data courtesy of ESPN

See Also

img_header()

gt_theme_pff
fa_icon_repeat